@vvlad1973/simple-logger 1.2.2 → 2.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. package/CHANGELOG.md +149 -0
  2. package/README.md +91 -41
  3. package/dist/__test__/simple-logger.test.js +54 -101
  4. package/dist/__test__/simple-logger.test.js.map +1 -1
  5. package/dist/classes/simple-logger.d.ts +125 -0
  6. package/dist/classes/simple-logger.js +290 -0
  7. package/dist/classes/simple-logger.js.map +1 -0
  8. package/dist/constants/constants.d.ts +9 -0
  9. package/dist/constants/constants.js +10 -0
  10. package/dist/constants/constants.js.map +1 -0
  11. package/dist/helpers/helpers.d.ts +31 -0
  12. package/dist/helpers/helpers.js +81 -0
  13. package/dist/helpers/helpers.js.map +1 -0
  14. package/dist/helpers/validators.d.ts +17 -0
  15. package/dist/helpers/validators.js +27 -0
  16. package/dist/helpers/validators.js.map +1 -0
  17. package/dist/index.d.ts +5 -2
  18. package/dist/index.js +5 -2
  19. package/dist/index.js.map +1 -1
  20. package/dist/types/simple-logger.types.d.ts +27 -0
  21. package/dist/types/simple-logger.types.js +2 -0
  22. package/dist/types/simple-logger.types.js.map +1 -0
  23. package/docs/assets/highlight.css +9 -2
  24. package/docs/assets/navigation.js +1 -1
  25. package/docs/assets/search.js +1 -1
  26. package/docs/classes/SimpleLogger.html +27 -15
  27. package/docs/index.html +51 -22
  28. package/docs/interfaces/ExternalLogger.html +10 -0
  29. package/docs/interfaces/LogFn.html +1 -0
  30. package/docs/interfaces/LoggerOptions.html +5 -0
  31. package/docs/modules.html +1 -1
  32. package/docs/types/LoggerFactory.html +1 -0
  33. package/docs/types/LoggerLevel.html +1 -1
  34. package/docs/types/PreparedLogCall.html +1 -0
  35. package/docs/variables/LoggerLevels.html +1 -0
  36. package/package.json +20 -7
  37. package/src/__test__/simple-logger.test.ts +67 -55
  38. package/src/classes/simple-logger.ts +354 -0
  39. package/src/constants/constants.ts +9 -0
  40. package/src/helpers/helpers.ts +92 -0
  41. package/src/helpers/validators.ts +36 -0
  42. package/src/index.ts +7 -2
  43. package/src/types/simple-logger.types.ts +40 -0
  44. package/docs/media/index.html +0 -39
  45. package/docs/types/ExternalLogger.html +0 -1
  46. package/jsdoc.json +0 -14
  47. package/src/simple-logger.ts +0 -190
@@ -0,0 +1,92 @@
1
+ import chalk from 'chalk';
2
+ import { LoggerLevel, PreparedLogCall } from '../types/simple-logger.types.js';
3
+ import { LoggerLevels } from '../constants/constants.js';
4
+
5
+ /**
6
+ * Extracts the message and rest of the arguments from the provided array.
7
+ *
8
+ * If the first argument is an object and the second argument is a string, the second argument is considered the message.
9
+ * If the first argument is a string, it is considered the message.
10
+ * Otherwise, an empty message is returned.
11
+ *
12
+ * @param {unknown[]} args - The array of arguments to extract the message from.
13
+ * @return {{ msg: string; rest: unknown[] }} An object containing the extracted message and the rest of the arguments.
14
+ */
15
+ export function extractMessage(args: unknown[]): {
16
+ msg: string;
17
+ rest: unknown[];
18
+ } {
19
+ const [first, second, ...rest] = args;
20
+
21
+ if (
22
+ typeof first === 'object' &&
23
+ first !== null &&
24
+ typeof second === 'string'
25
+ ) {
26
+ return { msg: second, rest };
27
+ }
28
+
29
+ if (typeof first === 'string') {
30
+ return {
31
+ msg: first,
32
+ rest: [second, ...rest].filter((value) => value !== undefined),
33
+ };
34
+ }
35
+
36
+ return { msg: '', rest: [] };
37
+ }
38
+
39
+ /**
40
+ * Extracts the context object from the provided array of arguments.
41
+ *
42
+ * @param {unknown[]} args - The array of arguments to extract the context from.
43
+ * @return {Record<string, unknown>} The extracted context object, or an empty object if no context is found.
44
+ */
45
+ export function extractContext(args: unknown[]): Record<string, unknown> {
46
+ const [first] = args;
47
+ if (typeof first === 'object' && first !== null && !Array.isArray(first)) {
48
+ return first as Record<string, unknown>;
49
+ }
50
+ return {};
51
+ }
52
+
53
+ /**
54
+ * Returns a string with the provided label colored according to the specified logging level.
55
+ *
56
+ * @param {LoggerLevel} level - The logging level to determine the color from.
57
+ * @param {string} label - The label to be colored.
58
+ * @return {string} The colored label.
59
+ */
60
+ export function colorizeLevel(level: LoggerLevel, label: string): string {
61
+ switch (level) {
62
+ case LoggerLevels.DEBUG:
63
+ case LoggerLevels.TRACE:
64
+ return chalk.gray(label);
65
+ case LoggerLevels.INFO:
66
+ return chalk.green(label);
67
+ case LoggerLevels.WARN:
68
+ return chalk.yellow(label);
69
+ case LoggerLevels.ERROR:
70
+ case LoggerLevels.FATAL:
71
+ return chalk.red(label);
72
+ default:
73
+ return label;
74
+ }
75
+ /**
76
+ * Prepares the log call by rearranging the provided arguments into a standardized format.
77
+ *
78
+ * @param {unknown[]} args - The array of arguments to prepare for the log call.
79
+ * @return {PreparedLogCall} The prepared log call arguments, or null if the input is invalid.
80
+ */
81
+ }export function prepareLogCall(args: unknown[]): PreparedLogCall {
82
+ if (typeof args[0] === 'string') {
83
+ return [args[0], ...args.slice(1)];
84
+ }
85
+ if (typeof args[0] === 'object' && args[0] !== null) {
86
+ const msg = typeof args[1] === 'string' ? args[1] : undefined;
87
+ const rest = args.slice(msg ? 2 : 1);
88
+ return [args[0], msg, ...rest];
89
+ }
90
+ return null;
91
+ }
92
+
@@ -0,0 +1,36 @@
1
+ import { LoggerLevels } from '../constants/constants';
2
+ import { ExternalLogger } from '../types/simple-logger.types.js';
3
+
4
+ /**
5
+ * Checks if the provided logger is a valid ExternalLogger instance.
6
+ *
7
+ * @param {unknown} logger - The logger to validate.
8
+ * @return {logger is ExternalLogger} True if the logger is a valid ExternalLogger, false otherwise.
9
+ */
10
+ export function isValidLogger(logger: unknown): logger is ExternalLogger {
11
+ const levels = Object.values(LoggerLevels);
12
+
13
+ if (typeof logger !== 'object' || logger === null) {
14
+ return false;
15
+ }
16
+
17
+ const candidate = logger as Record<string, unknown>;
18
+
19
+ return levels.some((level) => typeof candidate[level] === 'function');
20
+ }
21
+
22
+ /**
23
+ * Checks if the provided logger is an ExternalLogger instance with a child method.
24
+ *
25
+ * @param {unknown} logger - The logger to check.
26
+ * @return {logger is ExternalLogger & { child: Function }} True if the logger is an ExternalLogger with a child method, false otherwise.
27
+ */
28
+ export function hasChild(
29
+ logger: unknown
30
+ ): logger is ExternalLogger & { child: Function } {
31
+ return (
32
+ typeof logger === 'object' &&
33
+ logger !== null &&
34
+ typeof (logger as any).child === 'function'
35
+ );
36
+ }
package/src/index.ts CHANGED
@@ -1,2 +1,7 @@
1
- export { default } from './simple-logger.js'
2
- export * from './simple-logger.js'
1
+ import SimpleLogger from './classes/simple-logger';
2
+
3
+ export default SimpleLogger;
4
+ export { SimpleLogger };
5
+
6
+ export * from './types/simple-logger.types';
7
+ export * from './constants/constants.js';
@@ -0,0 +1,40 @@
1
+ import { LoggerLevels } from '../constants/constants';
2
+
3
+ export type LoggerLevel = (typeof LoggerLevels)[keyof typeof LoggerLevels];
4
+
5
+ export interface LogFn {
6
+ <T extends object>(obj: T, msg?: string, ...args: unknown[]): void;
7
+ (obj: unknown, msg?: string, ...args: unknown[]): void;
8
+ (msg: string, ...args: unknown[]): void;
9
+ }
10
+
11
+ export interface ExternalLogger {
12
+ trace?: LogFn;
13
+ debug?: LogFn;
14
+ info?: LogFn;
15
+ warn?: LogFn;
16
+ error?: LogFn;
17
+ fatal?: LogFn;
18
+ silent?: LogFn;
19
+
20
+ level?: string;
21
+
22
+ child?(bindings: Record<string, unknown>): ExternalLogger;
23
+ }
24
+
25
+ export type LoggerFactory = (
26
+ options: Record<string, unknown>
27
+ ) => ExternalLogger;
28
+
29
+ export interface LoggerOptions {
30
+ level?: LoggerLevel;
31
+ bindings?: Record<string, unknown>;
32
+ msgPrefix?: string;
33
+ transport?: unknown;
34
+ [key: string]: unknown;
35
+ }
36
+
37
+ export type PreparedLogCall =
38
+ | [string, ...unknown[]]
39
+ | [object, string?, ...unknown[]]
40
+ | null;
@@ -1,39 +0,0 @@
1
- <!DOCTYPE html><html class="default" lang="en" data-base="."><head><meta charset="utf-8"/><meta http-equiv="x-ua-compatible" content="IE=edge"/><title>@vvlad1973/simple-logger - v1.2.1</title><meta name="description" content="Documentation for @vvlad1973/simple-logger"/><meta name="viewport" content="width=device-width, initial-scale=1"/><link rel="stylesheet" href="assets/style.css"/><link rel="stylesheet" href="assets/highlight.css"/><script defer src="assets/main.js"></script><script async src="assets/icons.js" id="tsd-icons-script"></script><script async src="assets/search.js" id="tsd-search-script"></script><script async src="assets/navigation.js" id="tsd-nav-script"></script></head><body><script>document.documentElement.dataset.theme = localStorage.getItem("tsd-theme") || "os";document.body.style.display="none";setTimeout(() => app?app.showPage():document.body.style.removeProperty("display"),500)</script><header class="tsd-page-toolbar"><div class="tsd-toolbar-contents container"><div class="table-cell" id="tsd-search"><div class="field"><label for="tsd-search-field" class="tsd-widget tsd-toolbar-icon search no-caption"><svg width="16" height="16" viewBox="0 0 16 16" fill="none" aria-hidden="true"><use href="assets/icons.svg#icon-search"></use></svg></label><input type="text" id="tsd-search-field" aria-label="Search"/></div><div class="field"><div id="tsd-toolbar-links"></div></div><ul class="results"><li class="state loading">Preparing search index...</li><li class="state failure">The search index is not available</li></ul><a href="index.html" class="title">@vvlad1973/simple-logger - v1.2.1</a></div><div class="table-cell" id="tsd-widgets"><a href="#" class="tsd-widget tsd-toolbar-icon menu no-caption" data-toggle="menu" aria-label="Menu"><svg width="16" height="16" viewBox="0 0 16 16" fill="none" aria-hidden="true"><use href="assets/icons.svg#icon-menu"></use></svg></a></div></div></header><div class="container container-main"><div class="col-content"><div class="tsd-page-title"><h1>@vvlad1973/simple-logger - v1.2.1</h1></div><div class="tsd-panel tsd-typography"><a id="vvlad1973simple-logger" class="tsd-anchor"></a><h1 class="tsd-anchor-link">@vvlad1973/simple-logger<a href="#vvlad1973simple-logger" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h1><p>A simple logger module for Node.js applications with support for various logging levels.</p>
2
- <a id="installation" class="tsd-anchor"></a><h2 class="tsd-anchor-link">Installation<a href="#installation" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h2><pre><code class="bash"><span class="hl-0">npm</span><span class="hl-1"> </span><span class="hl-2">install</span><span class="hl-1"> </span><span class="hl-2">@vvlad1973/simple-logger</span>
3
- </code><button type="button">Copy</button></pre>
4
-
5
- <a id="features" class="tsd-anchor"></a><h2 class="tsd-anchor-link">Features<a href="#features" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h2><ul>
6
- <li>Supports logging at different levels: TRACE, DEBUG, INFO, WARN, ERROR, FATAL.</li>
7
- <li>Allows integration with external logging libraries like Pino.</li>
8
- </ul>
9
- <a id="usage" class="tsd-anchor"></a><h2 class="tsd-anchor-link">Usage<a href="#usage" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h2><a id="simple-logger" class="tsd-anchor"></a><h3 class="tsd-anchor-link">Simple Logger<a href="#simple-logger" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h3><pre><code class="javascript"><br/><span class="hl-3">import</span><span class="hl-1"> </span><span class="hl-4">SimpleLogger</span><span class="hl-1"> </span><span class="hl-3">from</span><span class="hl-1"> </span><span class="hl-2">&#39;@vvlad1973/simple-logger&#39;</span><span class="hl-1">;</span><br/><br/><span class="hl-5">const</span><span class="hl-1"> </span><span class="hl-6">logger</span><span class="hl-1"> = </span><span class="hl-5">new</span><span class="hl-1"> </span><span class="hl-0">SimpleLogger</span><span class="hl-1">(</span><span class="hl-2">&#39;DEBUG&#39;</span><span class="hl-1">);</span><br/><br/><span class="hl-4">logger</span><span class="hl-1">.</span><span class="hl-0">debug</span><span class="hl-1">(</span><span class="hl-2">&#39;Debug message&#39;</span><span class="hl-1">);</span><br/><span class="hl-4">logger</span><span class="hl-1">.</span><span class="hl-0">info</span><span class="hl-1">(</span><span class="hl-2">&#39;Info message&#39;</span><span class="hl-1">);</span><br/><span class="hl-4">logger</span><span class="hl-1">.</span><span class="hl-0">warn</span><span class="hl-1">(</span><span class="hl-2">&#39;Warning message&#39;</span><span class="hl-1">);</span><br/><span class="hl-4">logger</span><span class="hl-1">.</span><span class="hl-0">error</span><span class="hl-1">(</span><span class="hl-2">&#39;Error message&#39;</span><span class="hl-1">);</span><br/><span class="hl-4">logger</span><span class="hl-1">.</span><span class="hl-0">fatal</span><span class="hl-1">(</span><span class="hl-2">&#39;Fatal error message&#39;</span><span class="hl-1">);</span>
10
- </code><button type="button">Copy</button></pre>
11
-
12
- <a id="integration-with-pino" class="tsd-anchor"></a><h3 class="tsd-anchor-link">Integration with Pino<a href="#integration-with-pino" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h3><pre><code class="javascript"><span class="hl-3">import</span><span class="hl-1"> </span><span class="hl-4">pino</span><span class="hl-1"> </span><span class="hl-3">from</span><span class="hl-1"> </span><span class="hl-2">&#39;pino&#39;</span><span class="hl-1">;</span><br/><span class="hl-3">import</span><span class="hl-1"> </span><span class="hl-4">SimpleLogger</span><span class="hl-1"> </span><span class="hl-3">from</span><span class="hl-1"> </span><span class="hl-2">&#39;@vvlad1973/simple-logger&#39;</span><span class="hl-1">;</span><br/><br/><span class="hl-7">// Initialize Pino logger</span><br/><span class="hl-5">const</span><span class="hl-1"> </span><span class="hl-6">pinoLogger</span><span class="hl-1"> = </span><span class="hl-0">pino</span><span class="hl-1">({</span><br/><span class="hl-1"> </span><span class="hl-4">prettyPrint:</span><span class="hl-1"> </span><span class="hl-5">true</span><span class="hl-1">,</span><br/><span class="hl-1">});</span><br/><br/><span class="hl-7">// Use Pino logger with SimpleLogger</span><br/><span class="hl-5">const</span><span class="hl-1"> </span><span class="hl-6">logger</span><span class="hl-1"> = </span><span class="hl-5">new</span><span class="hl-1"> </span><span class="hl-0">SimpleLogger</span><span class="hl-1">(</span><span class="hl-2">&#39;DEBUG&#39;</span><span class="hl-1">, </span><span class="hl-4">pinoLogger</span><span class="hl-1">);</span><br/><br/><span class="hl-4">logger</span><span class="hl-1">.</span><span class="hl-0">debug</span><span class="hl-1">(</span><span class="hl-2">&#39;Debug message&#39;</span><span class="hl-1">);</span><br/><span class="hl-4">logger</span><span class="hl-1">.</span><span class="hl-0">info</span><span class="hl-1">(</span><span class="hl-2">&#39;Info message&#39;</span><span class="hl-1">);</span><br/><span class="hl-4">logger</span><span class="hl-1">.</span><span class="hl-0">warn</span><span class="hl-1">(</span><span class="hl-2">&#39;Warning message&#39;</span><span class="hl-1">);</span><br/><span class="hl-4">logger</span><span class="hl-1">.</span><span class="hl-0">error</span><span class="hl-1">(</span><span class="hl-2">&#39;Error message&#39;</span><span class="hl-1">);</span><br/><span class="hl-4">logger</span><span class="hl-1">.</span><span class="hl-0">fatal</span><span class="hl-1">(</span><span class="hl-2">&#39;Fatal error message&#39;</span><span class="hl-1">);</span>
13
- </code><button type="button">Copy</button></pre>
14
-
15
- <a id="api" class="tsd-anchor"></a><h2 class="tsd-anchor-link">API<a href="#api" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h2><a id="new-simpleloggerlevel-externallogger" class="tsd-anchor"></a><h3 class="tsd-anchor-link">new SimpleLogger(level, externalLogger)<a href="#new-simpleloggerlevel-externallogger" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h3><p>Creates a new instance of SimpleLogger.</p>
16
- <ul>
17
- <li>level (optional): Initial logging level. Defaults to 'INFO'.</li>
18
- <li>externalLogger (optional): External logger instance. Can be an object with methods (trace, debug, info, warn, error, fatal) or any other logger compatible with these methods.</li>
19
- </ul>
20
- <a id="setlevellevel" class="tsd-anchor"></a><h3 class="tsd-anchor-link">setLevel(level)<a href="#setlevellevel" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h3><p>Sets the logging level of the logger instance.</p>
21
- <ul>
22
- <li>level: The logging level to set ('TRACE', 'DEBUG', 'INFO', 'WARN', 'ERROR', 'FATAL').</li>
23
- </ul>
24
- <a id="setexternalloggerlogger" class="tsd-anchor"></a><h3 class="tsd-anchor-link">setExternalLogger(logger)<a href="#setexternalloggerlogger" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h3><p>Sets an external logger instance to be used by SimpleLogger.</p>
25
- <ul>
26
- <li>logger: External logger instance. Should be an object with methods (trace, debug, info, warn, error, fatal) or any other logger compatible with these methods.</li>
27
- </ul>
28
- <a id="logfunctionstartfunctionname" class="tsd-anchor"></a><h3 class="tsd-anchor-link">logFunctionStart(functionName?)<a href="#logfunctionstartfunctionname" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h3><p>Logs the start of a function.</p>
29
- <ul>
30
- <li>functionName (optional): Name of the function to log. If not provided, attempts to retrieve the caller function name.</li>
31
- </ul>
32
- <a id="logfunctionendfunctionname" class="tsd-anchor"></a><h3 class="tsd-anchor-link">logFunctionEnd(functionName?)<a href="#logfunctionendfunctionname" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h3><p>Logs the end of a function.</p>
33
- <ul>
34
- <li>functionName (optional): Name of the function to log. If not provided, attempts to retrieve the caller function name.</li>
35
- </ul>
36
- <a id="documentation" class="tsd-anchor"></a><h3 class="tsd-anchor-link">Documentation<a href="#documentation" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h3><p>Full information about the package is available in the <a href="media/index.html">./docs</a> directory</p>
37
- <a id="license" class="tsd-anchor"></a><h3 class="tsd-anchor-link">License<a href="#license" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h3><p>This project is licensed under the MIT License with Commercial Use.</p>
38
- <a id="author" class="tsd-anchor"></a><h2 class="tsd-anchor-link">Author<a href="#author" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h2><p>Vladislav Vnukovskiy <a href="mailto:vvlad1973@gmail.com">vvlad1973@gmail.com</a></p>
39
- </div></div><div class="col-sidebar"><div class="page-menu"><div class="tsd-navigation settings"><details class="tsd-accordion"><summary class="tsd-accordion-summary"><h3><svg width="20" height="20" viewBox="0 0 24 24" fill="none" aria-hidden="true"><use href="assets/icons.svg#icon-chevronDown"></use></svg>Settings</h3></summary><div class="tsd-accordion-details"><div class="tsd-filter-visibility"><span class="settings-label">Member Visibility</span><ul id="tsd-filter-options"><li class="tsd-filter-item"><label class="tsd-filter-input"><input type="checkbox" id="tsd-filter-protected" name="protected"/><svg width="32" height="32" viewBox="0 0 32 32" aria-hidden="true"><rect class="tsd-checkbox-background" width="30" height="30" x="1" y="1" rx="6" fill="none"></rect><path class="tsd-checkbox-checkmark" d="M8.35422 16.8214L13.2143 21.75L24.6458 10.25" stroke="none" stroke-width="3.5" stroke-linejoin="round" fill="none"></path></svg><span>Protected</span></label></li><li class="tsd-filter-item"><label class="tsd-filter-input"><input type="checkbox" id="tsd-filter-inherited" name="inherited" checked/><svg width="32" height="32" viewBox="0 0 32 32" aria-hidden="true"><rect class="tsd-checkbox-background" width="30" height="30" x="1" y="1" rx="6" fill="none"></rect><path class="tsd-checkbox-checkmark" d="M8.35422 16.8214L13.2143 21.75L24.6458 10.25" stroke="none" stroke-width="3.5" stroke-linejoin="round" fill="none"></path></svg><span>Inherited</span></label></li><li class="tsd-filter-item"><label class="tsd-filter-input"><input type="checkbox" id="tsd-filter-external" name="external"/><svg width="32" height="32" viewBox="0 0 32 32" aria-hidden="true"><rect class="tsd-checkbox-background" width="30" height="30" x="1" y="1" rx="6" fill="none"></rect><path class="tsd-checkbox-checkmark" d="M8.35422 16.8214L13.2143 21.75L24.6458 10.25" stroke="none" stroke-width="3.5" stroke-linejoin="round" fill="none"></path></svg><span>External</span></label></li></ul></div><div class="tsd-theme-toggle"><label class="settings-label" for="tsd-theme">Theme</label><select id="tsd-theme"><option value="os">OS</option><option value="light">Light</option><option value="dark">Dark</option></select></div></div></details></div><details open class="tsd-accordion tsd-page-navigation"><summary class="tsd-accordion-summary"><h3><svg width="20" height="20" viewBox="0 0 24 24" fill="none" aria-hidden="true"><use href="assets/icons.svg#icon-chevronDown"></use></svg>On This Page</h3></summary><div class="tsd-accordion-details"><a href="#vvlad1973simple-logger"><span>@vvlad1973/simple-<wbr/>logger</span></a><ul><li><a href="#installation"><span>Installation</span></a></li><li><a href="#features"><span>Features</span></a></li><li><a href="#usage"><span>Usage</span></a></li><li><ul><li><a href="#simple-logger"><span>Simple <wbr/>Logger</span></a></li><li><a href="#integration-with-pino"><span>Integration with <wbr/>Pino</span></a></li></ul></li><li><a href="#api"><span>API</span></a></li><li><ul><li><a href="#new-simpleloggerlevel-externallogger"><span>new <wbr/>Simple<wbr/>Logger(level, external<wbr/>Logger)</span></a></li><li><a href="#setlevellevel"><span>set<wbr/>Level(level)</span></a></li><li><a href="#setexternalloggerlogger"><span>set<wbr/>External<wbr/>Logger(logger)</span></a></li><li><a href="#logfunctionstartfunctionname"><span>log<wbr/>Function<wbr/>Start(function<wbr/>Name?)</span></a></li><li><a href="#logfunctionendfunctionname"><span>log<wbr/>Function<wbr/>End(function<wbr/>Name?)</span></a></li><li><a href="#documentation"><span>Documentation</span></a></li><li><a href="#license"><span>License</span></a></li></ul></li><li><a href="#author"><span>Author</span></a></li></ul></div></details></div><div class="site-menu"><nav class="tsd-navigation"><a href="modules.html">@vvlad1973/simple-logger - v1.2.1</a><ul class="tsd-small-nested-navigation" id="tsd-nav-container"><li>Loading...</li></ul></nav></div></div></div><footer><p class="tsd-generator">Generated using <a href="https://typedoc.org/" target="_blank">TypeDoc</a></p></footer><div class="overlay"></div></body></html>
@@ -1 +0,0 @@
1
- <!DOCTYPE html><html class="default" lang="en" data-base=".."><head><meta charset="utf-8"/><meta http-equiv="x-ua-compatible" content="IE=edge"/><title>ExternalLogger | @vvlad1973/simple-logger - v1.2.1</title><meta name="description" content="Documentation for @vvlad1973/simple-logger"/><meta name="viewport" content="width=device-width, initial-scale=1"/><link rel="stylesheet" href="../assets/style.css"/><link rel="stylesheet" href="../assets/highlight.css"/><script defer src="../assets/main.js"></script><script async src="../assets/icons.js" id="tsd-icons-script"></script><script async src="../assets/search.js" id="tsd-search-script"></script><script async src="../assets/navigation.js" id="tsd-nav-script"></script></head><body><script>document.documentElement.dataset.theme = localStorage.getItem("tsd-theme") || "os";document.body.style.display="none";setTimeout(() => app?app.showPage():document.body.style.removeProperty("display"),500)</script><header class="tsd-page-toolbar"><div class="tsd-toolbar-contents container"><div class="table-cell" id="tsd-search"><div class="field"><label for="tsd-search-field" class="tsd-widget tsd-toolbar-icon search no-caption"><svg width="16" height="16" viewBox="0 0 16 16" fill="none" aria-hidden="true"><use href="../assets/icons.svg#icon-search"></use></svg></label><input type="text" id="tsd-search-field" aria-label="Search"/></div><div class="field"><div id="tsd-toolbar-links"></div></div><ul class="results"><li class="state loading">Preparing search index...</li><li class="state failure">The search index is not available</li></ul><a href="../index.html" class="title">@vvlad1973/simple-logger - v1.2.1</a></div><div class="table-cell" id="tsd-widgets"><a href="#" class="tsd-widget tsd-toolbar-icon menu no-caption" data-toggle="menu" aria-label="Menu"><svg width="16" height="16" viewBox="0 0 16 16" fill="none" aria-hidden="true"><use href="../assets/icons.svg#icon-menu"></use></svg></a></div></div></header><div class="container container-main"><div class="col-content"><div class="tsd-page-title"><ul class="tsd-breadcrumb"><li><a href="../modules.html">@vvlad1973/simple-logger</a></li><li><a href="ExternalLogger.html">ExternalLogger</a></li></ul><h1>Type Alias ExternalLogger</h1></div><div class="tsd-signature"><span class="tsd-kind-type-alias">ExternalLogger</span><span class="tsd-signature-symbol">:</span> <span class="tsd-signature-symbol">{</span><br/>    <span class="tsd-kind-property">debug</span><span class="tsd-signature-symbol">?:</span> <span class="tsd-signature-type">LogFn</span><span class="tsd-signature-symbol">;</span><br/>    <span class="tsd-kind-property">error</span><span class="tsd-signature-symbol">?:</span> <span class="tsd-signature-type">LogFn</span><span class="tsd-signature-symbol">;</span><br/>    <span class="tsd-kind-property">fatal</span><span class="tsd-signature-symbol">?:</span> <span class="tsd-signature-type">LogFn</span><span class="tsd-signature-symbol">;</span><br/>    <span class="tsd-kind-property">info</span><span class="tsd-signature-symbol">?:</span> <span class="tsd-signature-type">LogFn</span><span class="tsd-signature-symbol">;</span><br/>    <span class="tsd-kind-property">level</span><span class="tsd-signature-symbol">?:</span> <span class="tsd-signature-type">string</span><span class="tsd-signature-symbol">;</span><br/>    <span class="tsd-kind-property">silent</span><span class="tsd-signature-symbol">?:</span> <span class="tsd-signature-type">LogFn</span><span class="tsd-signature-symbol">;</span><br/>    <span class="tsd-kind-property">trace</span><span class="tsd-signature-symbol">?:</span> <span class="tsd-signature-type">LogFn</span><span class="tsd-signature-symbol">;</span><br/>    <span class="tsd-kind-property">warn</span><span class="tsd-signature-symbol">?:</span> <span class="tsd-signature-type">LogFn</span><span class="tsd-signature-symbol">;</span><br/><span class="tsd-signature-symbol">}</span></div><div class="tsd-type-declaration"><h4>Type declaration</h4><ul class="tsd-parameters"><li class="tsd-parameter"><h5><code class="tsd-tag">Optional</code><span class="tsd-kind-property">debug</span><a id="__typedebug" class="tsd-anchor"></a><span class="tsd-signature-symbol">?: </span><span class="tsd-signature-type">LogFn</span></h5></li><li class="tsd-parameter"><h5><code class="tsd-tag">Optional</code><span class="tsd-kind-property">error</span><a id="__typeerror" class="tsd-anchor"></a><span class="tsd-signature-symbol">?: </span><span class="tsd-signature-type">LogFn</span></h5></li><li class="tsd-parameter"><h5><code class="tsd-tag">Optional</code><span class="tsd-kind-property">fatal</span><a id="__typefatal" class="tsd-anchor"></a><span class="tsd-signature-symbol">?: </span><span class="tsd-signature-type">LogFn</span></h5></li><li class="tsd-parameter"><h5><code class="tsd-tag">Optional</code><span class="tsd-kind-property">info</span><a id="__typeinfo" class="tsd-anchor"></a><span class="tsd-signature-symbol">?: </span><span class="tsd-signature-type">LogFn</span></h5></li><li class="tsd-parameter"><h5><code class="tsd-tag">Optional</code><span class="tsd-kind-property">level</span><a id="__typelevel" class="tsd-anchor"></a><span class="tsd-signature-symbol">?: </span><span class="tsd-signature-type">string</span></h5></li><li class="tsd-parameter"><h5><code class="tsd-tag">Optional</code><span class="tsd-kind-property">silent</span><a id="__typesilent" class="tsd-anchor"></a><span class="tsd-signature-symbol">?: </span><span class="tsd-signature-type">LogFn</span></h5></li><li class="tsd-parameter"><h5><code class="tsd-tag">Optional</code><span class="tsd-kind-property">trace</span><a id="__typetrace" class="tsd-anchor"></a><span class="tsd-signature-symbol">?: </span><span class="tsd-signature-type">LogFn</span></h5></li><li class="tsd-parameter"><h5><code class="tsd-tag">Optional</code><span class="tsd-kind-property">warn</span><a id="__typewarn" class="tsd-anchor"></a><span class="tsd-signature-symbol">?: </span><span class="tsd-signature-type">LogFn</span></h5></li></ul></div><aside class="tsd-sources"><ul><li>Defined in <a href="https://github.com/vvlad1973/vvlad1973-simple-logger/blob/521c6e1ac04147bd4f4bd20eedb2287852cfe54d/src/simple-logger.ts#L8">simple-logger.ts:8</a></li></ul></aside></div><div class="col-sidebar"><div class="page-menu"><div class="tsd-navigation settings"><details class="tsd-accordion"><summary class="tsd-accordion-summary"><h3><svg width="20" height="20" viewBox="0 0 24 24" fill="none" aria-hidden="true"><use href="../assets/icons.svg#icon-chevronDown"></use></svg>Settings</h3></summary><div class="tsd-accordion-details"><div class="tsd-filter-visibility"><span class="settings-label">Member Visibility</span><ul id="tsd-filter-options"><li class="tsd-filter-item"><label class="tsd-filter-input"><input type="checkbox" id="tsd-filter-protected" name="protected"/><svg width="32" height="32" viewBox="0 0 32 32" aria-hidden="true"><rect class="tsd-checkbox-background" width="30" height="30" x="1" y="1" rx="6" fill="none"></rect><path class="tsd-checkbox-checkmark" d="M8.35422 16.8214L13.2143 21.75L24.6458 10.25" stroke="none" stroke-width="3.5" stroke-linejoin="round" fill="none"></path></svg><span>Protected</span></label></li><li class="tsd-filter-item"><label class="tsd-filter-input"><input type="checkbox" id="tsd-filter-inherited" name="inherited" checked/><svg width="32" height="32" viewBox="0 0 32 32" aria-hidden="true"><rect class="tsd-checkbox-background" width="30" height="30" x="1" y="1" rx="6" fill="none"></rect><path class="tsd-checkbox-checkmark" d="M8.35422 16.8214L13.2143 21.75L24.6458 10.25" stroke="none" stroke-width="3.5" stroke-linejoin="round" fill="none"></path></svg><span>Inherited</span></label></li><li class="tsd-filter-item"><label class="tsd-filter-input"><input type="checkbox" id="tsd-filter-external" name="external"/><svg width="32" height="32" viewBox="0 0 32 32" aria-hidden="true"><rect class="tsd-checkbox-background" width="30" height="30" x="1" y="1" rx="6" fill="none"></rect><path class="tsd-checkbox-checkmark" d="M8.35422 16.8214L13.2143 21.75L24.6458 10.25" stroke="none" stroke-width="3.5" stroke-linejoin="round" fill="none"></path></svg><span>External</span></label></li></ul></div><div class="tsd-theme-toggle"><label class="settings-label" for="tsd-theme">Theme</label><select id="tsd-theme"><option value="os">OS</option><option value="light">Light</option><option value="dark">Dark</option></select></div></div></details></div></div><div class="site-menu"><nav class="tsd-navigation"><a href="../modules.html">@vvlad1973/simple-logger - v1.2.1</a><ul class="tsd-small-nested-navigation" id="tsd-nav-container"><li>Loading...</li></ul></nav></div></div></div><footer><p class="tsd-generator">Generated using <a href="https://typedoc.org/" target="_blank">TypeDoc</a></p></footer><div class="overlay"></div></body></html>
package/jsdoc.json DELETED
@@ -1,14 +0,0 @@
1
- {
2
- "source": {
3
- "include": ["./src/"],
4
- "includePattern": "\\.(ts)$",
5
- "excludePattern": "(^|\\/|\\\\)node_modules"
6
- },
7
- "plugins": ["node_modules/better-docs/typescript"],
8
- "opts": {
9
- "destination": "./docs",
10
- "recurse": true,
11
- "template": "node_modules/better-docs"
12
- }
13
- }
14
-
@@ -1,190 +0,0 @@
1
- /**
2
- * A module for simple logging with various logging levels.
3
- * @module SimpleLogger
4
- */
5
-
6
- import { getCallerName } from 'vvlad1973-utils';
7
-
8
- export type ExternalLogger = {
9
- trace?: LogFn;
10
- debug?: LogFn;
11
- info?: LogFn;
12
- warn?: LogFn;
13
- error?: LogFn;
14
- fatal?: LogFn;
15
- silent?: LogFn;
16
- level?: string;
17
- };
18
-
19
- interface LogFn {
20
- <T extends object>(obj: T, msg?: string, ...args: any[]): void;
21
- (obj: unknown, msg?: string, ...args: any[]): void;
22
- (msg: string, ...args: any[]): void;
23
- }
24
-
25
- const loggerLevels = [
26
- 'trace',
27
- 'debug',
28
- 'info',
29
- 'warn',
30
- 'error',
31
- 'fatal',
32
- 'silent',
33
- ] as const;
34
-
35
- export type LoggerLevel = (typeof loggerLevels)[number];
36
- /**
37
- * A simple logger class with various logging levels.
38
- * @class
39
- * @param {string} [level='info'] - Initial logging level.
40
- * @param {ExternalLogger | undefined | null} [externalLogger=null] - Optional external logger to use.
41
- */
42
- export class SimpleLogger {
43
- #levels = loggerLevels;
44
- #currentLevel: number = 2;
45
- #logger: ExternalLogger | Console = console;
46
-
47
- isExternal: boolean = false;
48
-
49
- constructor(level?: LoggerLevel, externalLogger?: ExternalLogger) {
50
- if (externalLogger && this.isValidLogger(externalLogger)) {
51
- this.setExternalLogger(externalLogger, level);
52
- } else {
53
- this.setLevel(level ?? 'info');
54
- }
55
- }
56
-
57
- trace: LogFn = (...args: any[]) => {};
58
- debug: LogFn = (...args: any[]) => {};
59
- info: LogFn = (...args: any[]) => {};
60
- warn: LogFn = (...args: any[]) => {};
61
- error: LogFn = (...args: any[]) => {};
62
- fatal: LogFn = (...args: any[]) => {};
63
- silent: LogFn = (...args: any[]) => {};
64
-
65
- private static readonly noop: LogFn = (...args: any[]) => {};
66
-
67
- /**
68
- * Updates the logging methods based on the current logging level.
69
- * @private
70
- */
71
- private updateLoggingMethods() {
72
- try {
73
- this.#levels.forEach((level, index) => {
74
- this[level] =
75
- this.#currentLevel <= index
76
- ? this.getExternalLoggerMethod(
77
- level,
78
- level === 'fatal' ? 'error' : undefined
79
- )
80
- : SimpleLogger.noop;
81
- });
82
- } catch (error) {
83
- console.error(error);
84
- }
85
- }
86
-
87
- /**
88
- * Checks if the provided logger is valid.
89
- * @private
90
- * @param {any} logger - The logger to check.
91
- * @returns {boolean} - True if the logger is valid, otherwise false.
92
- */
93
- private isValidLogger(logger: any): boolean {
94
- return (
95
- typeof logger === 'object' &&
96
- logger !== null &&
97
- loggerLevels.some((level) => typeof logger[level] === 'function')
98
- );
99
- }
100
-
101
- /**
102
- * Gets the external logger method for a specific level.
103
- * @param {string} method - The logger method to get.
104
- * @param {string} [fallbackMethod] - The fallback logger method to get.
105
- * @returns {LogFn} - The logger method or a no-op function.
106
- * @private
107
- */
108
- private getExternalLoggerMethod(
109
- method: keyof ExternalLogger,
110
- fallbackMethod?: keyof ExternalLogger
111
- ): LogFn {
112
- try {
113
- const loggerMethod = (this.#logger as ExternalLogger)[method];
114
-
115
- if (typeof loggerMethod === 'function') {
116
- return loggerMethod.bind(this.#logger);
117
- } else if (typeof fallbackMethod !== 'undefined') {
118
- const fallbackLoggerMethod = (this.#logger as ExternalLogger)[
119
- fallbackMethod
120
- ];
121
-
122
- if (typeof fallbackLoggerMethod === 'function') {
123
- return fallbackLoggerMethod.bind(this.#logger);
124
- }
125
- }
126
- return SimpleLogger.noop;
127
- } catch (error) {
128
- console.error(error);
129
- return SimpleLogger.noop;
130
- }
131
- }
132
-
133
- /**
134
- * Sets the logging level.
135
- * @param {string} level - The logging level to set.
136
- */
137
- public setLevel(level: string) {
138
- const index = this.#levels.indexOf(level.toLowerCase() as LoggerLevel);
139
- if (index !== -1) {
140
- this.#currentLevel = index;
141
-
142
- if (typeof (this.#logger as ExternalLogger).level === 'string') {
143
- (this.#logger as ExternalLogger).level = level;
144
- }
145
- this.updateLoggingMethods();
146
- }
147
- }
148
-
149
- /**
150
- * Sets an external logger to be used.
151
- * @param {ExternalLogger | undefined | null} logger - The external logger to set.
152
- */
153
- public setExternalLogger(
154
- logger: ExternalLogger | undefined | null,
155
- level?: LoggerLevel
156
- ) {
157
- if (logger && this.isValidLogger(logger)) {
158
- this.#logger = logger;
159
- this.isExternal = true;
160
- } else {
161
- this.#logger = console;
162
- this.isExternal = false;
163
- }
164
- this.setLevel(
165
- level ??
166
- (logger?.level as LoggerLevel) ??
167
- this.#levels[this.#currentLevel]
168
- );
169
- }
170
-
171
- /**
172
- * Logs the start of a function.
173
- * @param {string} [functionName] - Optional function name to log.
174
- */
175
- public logFunctionStart(functionName?: string) {
176
- const callerName = functionName || getCallerName();
177
- this.trace(`Function start: ${callerName}`);
178
- }
179
-
180
- /**
181
- * Logs the end of a function.
182
- * @param {string} [functionName] - Optional function name to log.
183
- */
184
- public logFunctionEnd(functionName?: string) {
185
- const callerName = functionName || getCallerName();
186
- this.trace(`Function end: ${callerName}`);
187
- }
188
- }
189
-
190
- export default SimpleLogger;