@vvlad1973/simple-logger 1.2.0 → 1.2.1

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.
@@ -1,51 +1,25 @@
1
- import { describe, it, beforeEach } from 'node:test';
2
- import assert from 'assert';
1
+ import { describe, it, beforeEach, expect, vi } from 'vitest';
3
2
  import SimpleLogger from '../simple-logger.js';
4
3
 
5
- type MockFunction = {
6
- (...args: any[]): void;
7
- mock: { calls: any[][] };
8
- };
9
-
10
- type NoopFunction = {
11
- (...args: any[]): void;
12
- mock: { calls: any[][] };
13
- };
14
-
15
- function createMockLoggerMethod(): MockFunction {
16
- const calls: any[][] = [];
17
- const mockFn = function (...args: any[]) {
18
- calls.push(args);
19
- } as MockFunction;
20
- mockFn.mock = { calls };
21
- return mockFn;
22
- }
23
-
24
- function createNoopMethod(): MockFunction {
25
- const calls: any[][] = [];
26
- const mockFn = function (...args: any[]) {} as MockFunction;
27
- mockFn.mock = { calls };
28
- return mockFn;
29
- }
30
4
  type ExternalLoggerMock = {
31
- trace: MockFunction | NoopFunction;
32
- debug: MockFunction | NoopFunction;
33
- info: MockFunction | NoopFunction;
34
- warn: MockFunction | NoopFunction;
35
- error: MockFunction | NoopFunction;
36
- fatal: MockFunction | NoopFunction;
37
- silent: MockFunction | NoopFunction;
5
+ trace: ReturnType<typeof vi.fn>;
6
+ debug: ReturnType<typeof vi.fn>;
7
+ info: ReturnType<typeof vi.fn>;
8
+ warn: ReturnType<typeof vi.fn>;
9
+ error: ReturnType<typeof vi.fn>;
10
+ fatal: ReturnType<typeof vi.fn>;
11
+ silent: ReturnType<typeof vi.fn>;
38
12
  };
39
13
 
40
14
  function createExternalLoggerMock(): ExternalLoggerMock {
41
15
  return {
42
- trace: createMockLoggerMethod(),
43
- debug: createMockLoggerMethod(),
44
- info: createMockLoggerMethod(),
45
- warn: createMockLoggerMethod(),
46
- error: createMockLoggerMethod(),
47
- fatal: createMockLoggerMethod(),
48
- silent:createMockLoggerMethod(),
16
+ trace: vi.fn(),
17
+ debug: vi.fn(),
18
+ info: vi.fn(),
19
+ warn: vi.fn(),
20
+ error: vi.fn(),
21
+ fatal: vi.fn(),
22
+ silent: vi.fn(),
49
23
  };
50
24
  }
51
25
 
@@ -66,107 +40,20 @@ describe('SimpleLogger', () => {
66
40
  logger.error('error message');
67
41
  logger.fatal('fatal message');
68
42
 
69
- assert.strictEqual(externalLoggerMock.trace.mock.calls.length, 0);
70
- assert.strictEqual(externalLoggerMock.debug.mock.calls.length, 0);
71
- assert.strictEqual(externalLoggerMock.info.mock.calls.length, 1);
72
- assert.strictEqual(
73
- externalLoggerMock.info.mock.calls[0][0],
74
- 'info message'
75
- );
76
- assert.strictEqual(externalLoggerMock.warn.mock.calls.length, 1);
77
- assert.strictEqual(
78
- externalLoggerMock.warn.mock.calls[0][0],
79
- 'warn message'
80
- );
81
- assert.strictEqual(externalLoggerMock.error.mock.calls.length, 1);
82
- assert.strictEqual(
83
- externalLoggerMock.error.mock.calls[0][0],
84
- 'error message'
85
- );
86
- assert.strictEqual(externalLoggerMock.fatal.mock.calls.length, 1);
87
- assert.strictEqual(
88
- externalLoggerMock.fatal.mock.calls[0][0],
89
- 'fatal message'
90
- );
91
- });
92
-
93
- it('should log function start and end', () => {
94
- logger.setLevel('TRACE');
95
-
96
- function testFunction() {
97
- logger.logFunctionStart();
98
- logger.logFunctionEnd();
99
- }
100
-
101
- testFunction();
102
-
103
- assert.strictEqual(externalLoggerMock.trace.mock.calls.length, 2);
104
- assert.strictEqual(
105
- externalLoggerMock.trace.mock.calls[0][0],
106
- 'Function start: testFunction'
107
- );
108
- assert.strictEqual(
109
- externalLoggerMock.trace.mock.calls[1][0],
110
- 'Function end: testFunction'
111
- );
112
- });
113
- });
114
-
115
- describe('SimpleLogger2', () => {
116
- let logger: SimpleLogger;
117
- let externalLoggerMock: ExternalLoggerMock;
118
-
119
- beforeEach(() => {
120
- externalLoggerMock = createExternalLoggerMock();
121
- logger = new SimpleLogger('info', externalLoggerMock);
122
- });
123
-
124
- it('should use console as default external logger', () => {
125
- const defaultLogger = new SimpleLogger();
126
- assert.ok(defaultLogger);
127
- });
128
-
129
- it('should set the correct log level', () => {
130
- logger.setLevel('DEBUG');
131
- assert.notStrictEqual(logger.trace, () => {});
132
- assert.strictEqual(typeof logger.debug, 'function');
133
- assert.strictEqual(typeof logger.info, 'function');
134
- });
135
-
136
- it('should call external logger methods appropriately', () => {
137
- logger.trace('trace message');
138
- logger.debug('debug message');
139
- logger.info('info message');
140
- logger.warn('warn message');
141
- logger.error('error message');
142
- logger.fatal('fatal message');
143
-
144
- assert.strictEqual(externalLoggerMock.trace.mock.calls.length, 0); // because default level is INFO
145
- assert.strictEqual(externalLoggerMock.debug.mock.calls.length, 0); // because default level is INFO
146
- assert.strictEqual(externalLoggerMock.info.mock.calls.length, 1);
147
- assert.strictEqual(
148
- externalLoggerMock.info.mock.calls[0][0],
149
- 'info message'
150
- );
151
- assert.strictEqual(externalLoggerMock.warn.mock.calls.length, 1);
152
- assert.strictEqual(
153
- externalLoggerMock.warn.mock.calls[0][0],
154
- 'warn message'
155
- );
156
- assert.strictEqual(externalLoggerMock.error.mock.calls.length, 1);
157
- assert.strictEqual(
158
- externalLoggerMock.error.mock.calls[0][0],
159
- 'error message'
160
- );
161
- assert.strictEqual(externalLoggerMock.fatal.mock.calls.length, 1);
162
- assert.strictEqual(
163
- externalLoggerMock.fatal.mock.calls[0][0],
164
- 'fatal message'
165
- );
43
+ expect(externalLoggerMock.trace).not.toHaveBeenCalled();
44
+ expect(externalLoggerMock.debug).not.toHaveBeenCalled();
45
+ expect(externalLoggerMock.info).toHaveBeenCalledTimes(1);
46
+ expect(externalLoggerMock.info).toHaveBeenCalledWith('info message');
47
+ expect(externalLoggerMock.warn).toHaveBeenCalledTimes(1);
48
+ expect(externalLoggerMock.warn).toHaveBeenCalledWith('warn message');
49
+ expect(externalLoggerMock.error).toHaveBeenCalledTimes(1);
50
+ expect(externalLoggerMock.error).toHaveBeenCalledWith('error message');
51
+ expect(externalLoggerMock.fatal).toHaveBeenCalledTimes(1);
52
+ expect(externalLoggerMock.fatal).toHaveBeenCalledWith('fatal message');
166
53
  });
167
54
 
168
55
  it('should log function start and end', () => {
169
- logger.setLevel('TRACE');
56
+ logger.setLevel('trace');
170
57
 
171
58
  function testFunction() {
172
59
  logger.logFunctionStart();
@@ -175,14 +62,8 @@ describe('SimpleLogger2', () => {
175
62
 
176
63
  testFunction();
177
64
 
178
- assert.strictEqual(externalLoggerMock.trace.mock.calls.length, 2);
179
- assert.strictEqual(
180
- externalLoggerMock.trace.mock.calls[0][0],
181
- 'Function start: testFunction'
182
- );
183
- assert.strictEqual(
184
- externalLoggerMock.trace.mock.calls[1][0],
185
- 'Function end: testFunction'
186
- );
65
+ expect(externalLoggerMock.trace).toHaveBeenCalledTimes(2);
66
+ expect(externalLoggerMock.trace).toHaveBeenCalledWith('Function start: testFunction');
67
+ expect(externalLoggerMock.trace).toHaveBeenCalledWith('Function end: testFunction');
187
68
  });
188
69
  });
@@ -31,6 +31,7 @@ const loggerLevels = [
31
31
  'fatal',
32
32
  'silent',
33
33
  ] as const;
34
+
34
35
  export type LoggerLevel = (typeof loggerLevels)[number];
35
36
  /**
36
37
  * A simple logger class with various logging levels.
@@ -61,28 +62,23 @@ export class SimpleLogger {
61
62
  fatal: LogFn = (...args: any[]) => {};
62
63
  silent: LogFn = (...args: any[]) => {};
63
64
 
65
+ private static readonly noop: LogFn = (...args: any[]) => {};
66
+
64
67
  /**
65
68
  * Updates the logging methods based on the current logging level.
66
69
  * @private
67
70
  */
68
71
  private updateLoggingMethods() {
69
72
  try {
70
- const noop = (...args: any[]) => {};
71
- this.trace =
72
- this.#currentLevel <= 0 ? this.getExternalLoggerMethod('trace') : noop;
73
- this.debug =
74
- this.#currentLevel <= 1 ? this.getExternalLoggerMethod('debug') : noop;
75
- this.info =
76
- this.#currentLevel <= 2 ? this.getExternalLoggerMethod('info') : noop;
77
- this.warn =
78
- this.#currentLevel <= 3 ? this.getExternalLoggerMethod('warn') : noop;
79
- this.error =
80
- this.#currentLevel <= 4 ? this.getExternalLoggerMethod('error') : noop;
81
- this.fatal =
82
- this.#currentLevel <= 5
83
- ? this.getExternalLoggerMethod('fatal', 'error')
84
- : noop;
85
- this.silent = noop;
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
+ });
86
82
  } catch (error) {
87
83
  console.error(error);
88
84
  }
@@ -95,16 +91,18 @@ export class SimpleLogger {
95
91
  * @returns {boolean} - True if the logger is valid, otherwise false.
96
92
  */
97
93
  private isValidLogger(logger: any): boolean {
98
- return loggerLevels.every((method) => {
99
- return typeof logger[method] === 'function';
100
- });
94
+ return (
95
+ typeof logger === 'object' &&
96
+ logger !== null &&
97
+ loggerLevels.some((level) => typeof logger[level] === 'function')
98
+ );
101
99
  }
102
100
 
103
101
  /**
104
102
  * Gets the external logger method for a specific level.
105
103
  * @param {string} method - The logger method to get.
106
104
  * @param {string} [fallbackMethod] - The fallback logger method to get.
107
- * @returns {LoggerMethod} - The logger method or a no-op function.
105
+ * @returns {LogFn} - The logger method or a no-op function.
108
106
  * @private
109
107
  */
110
108
  private getExternalLoggerMethod(
@@ -125,10 +123,10 @@ export class SimpleLogger {
125
123
  return fallbackLoggerMethod.bind(this.#logger);
126
124
  }
127
125
  }
128
- return () => {};
126
+ return SimpleLogger.noop;
129
127
  } catch (error) {
130
128
  console.error(error);
131
- return () => {};
129
+ return SimpleLogger.noop;
132
130
  }
133
131
  }
134
132
 
@@ -163,7 +161,11 @@ export class SimpleLogger {
163
161
  this.#logger = console;
164
162
  this.isExternal = false;
165
163
  }
166
- this.setLevel(level ?? logger?.level ?? this.#levels[this.#currentLevel]);
164
+ this.setLevel(
165
+ level ??
166
+ (logger?.level as LoggerLevel) ??
167
+ this.#levels[this.#currentLevel]
168
+ );
167
169
  }
168
170
 
169
171
  /**
package/typedoc.json ADDED
@@ -0,0 +1,8 @@
1
+ {
2
+ "entryPoints": [
3
+ "src/index.ts"
4
+ ],
5
+ "out": "docs",
6
+ "includeVersion": true,
7
+
8
+ }