@feizk/logger 1.0.0 → 1.4.0
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.
- package/CHANGELOG.md +30 -0
- package/README.md +37 -5
- package/dist/index.d.mts +31 -11
- package/dist/index.d.ts +31 -11
- package/dist/index.js +84 -18
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +83 -19
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +2 -46
- package/src/logger.ts +83 -0
- package/src/types.ts +8 -0
- package/src/utils.ts +44 -0
- package/tests/logger.test.ts +123 -8
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# @feizk/logger
|
|
2
|
+
|
|
3
|
+
## 1.4.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- 2b7b1eb: - Added `LogLevel` type ('debug' | 'info' | 'warn' | 'error') and `logLevel` option to `LoggerOptions` in `types.ts`
|
|
8
|
+
- Updated `logger.ts` to implement log level filtering with a `LOG_LEVEL_PRIORITIES` constant, `shouldLog` private method, and checks before each log call
|
|
9
|
+
- Added `setLogLevel` method to `logger.ts` for dynamic runtime level changes
|
|
10
|
+
- Exported `LogLevel` type from `index.ts` for external use
|
|
11
|
+
- Added comprehensive test cases in `logger.test.ts` for filtering behavior at different levels, dynamic level changes, and default behavior
|
|
12
|
+
- Updated `README.md` with `logLevel` option documentation, usage examples, and API details for the new method
|
|
13
|
+
|
|
14
|
+
## 1.3.0
|
|
15
|
+
|
|
16
|
+
### Minor Changes
|
|
17
|
+
|
|
18
|
+
- 6e3d7e5: - Created `src/types.ts` with `LoggerOptions` interface for `enableColors`, `timestampFormat`, and `logFormat` options\n- Created `src/utils.ts` with utility functions: `formatTimestamp`, `getColor`, and `formatLog`\n- Moved `Logger` class to `src/logger.ts` and made it configurable via constructor options with defaults\n- Updated `src/index.ts` to export `Logger` class and `LoggerOptions` type instead of containing the class\n- Removed the `success` method from `Logger` class (per user feedback)\n- Removed `[SUCCESS]` color mapping from `utils.ts`\n- Added tests in `tests/logger.test.ts` for disabling colors, locale timestamp, custom timestamp function, and custom log format\n- Removed success-related test from `tests/logger.test.ts`\n- Updated `README.md` with usage examples for new options and removed success method documentation\n- Ensured backward compatibility: existing `new Logger()` usage remains unchanged\n- All tests pass (10 tests) and build succeeds
|
|
19
|
+
|
|
20
|
+
## 1.2.0
|
|
21
|
+
|
|
22
|
+
### Minor Changes
|
|
23
|
+
|
|
24
|
+
- 4b70f01: Accept multiple arguments and Any type of arguments on all Logger methods
|
|
25
|
+
|
|
26
|
+
## 1.1.0
|
|
27
|
+
|
|
28
|
+
### Minor Changes
|
|
29
|
+
|
|
30
|
+
- ebc4e0b: Add success logging method to Logger class
|
package/README.md
CHANGED
|
@@ -21,13 +21,45 @@ logger.error('This is an error');
|
|
|
21
21
|
logger.debug('This is a debug message');
|
|
22
22
|
```
|
|
23
23
|
|
|
24
|
+
### Options
|
|
25
|
+
|
|
26
|
+
Customize the logger with constructor options:
|
|
27
|
+
|
|
28
|
+
```typescript
|
|
29
|
+
const logger = new Logger({
|
|
30
|
+
enableColors: true, // Default: true
|
|
31
|
+
timestampFormat: 'iso', // 'iso' | 'locale' | custom function, Default: 'iso'
|
|
32
|
+
logFormat: undefined, // Custom formatter function, Default: undefined
|
|
33
|
+
logLevel: 'debug', // 'debug' | 'info' | 'warn' | 'error', Default: 'debug'
|
|
34
|
+
});
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
#### Examples
|
|
38
|
+
|
|
39
|
+
```typescript
|
|
40
|
+
// Disable colors
|
|
41
|
+
const noColorLogger = new Logger({ enableColors: false });
|
|
42
|
+
|
|
43
|
+
// Use locale timestamp
|
|
44
|
+
const localeLogger = new Logger({ timestampFormat: 'locale' });
|
|
45
|
+
|
|
46
|
+
// Filter logs below info level
|
|
47
|
+
const infoLogger = new Logger({ logLevel: 'info' });
|
|
48
|
+
infoLogger.debug('Not logged');
|
|
49
|
+
infoLogger.info('Logged'); // and higher
|
|
50
|
+
|
|
51
|
+
// Change level dynamically
|
|
52
|
+
logger.setLogLevel('error');
|
|
53
|
+
```
|
|
54
|
+
|
|
24
55
|
## API
|
|
25
56
|
|
|
26
57
|
### Logger
|
|
27
58
|
|
|
28
|
-
- `info(
|
|
29
|
-
- `warn(
|
|
30
|
-
- `error(
|
|
31
|
-
- `debug(
|
|
59
|
+
- `info(...args: unknown[])`: Logs an info message.
|
|
60
|
+
- `warn(...args: unknown[])`: Logs a warning message.
|
|
61
|
+
- `error(...args: unknown[])`: Logs an error message.
|
|
62
|
+
- `debug(...args: unknown[])`: Logs a debug message.
|
|
63
|
+
- `setLogLevel(level: LogLevel)`: Sets the minimum log level for filtering messages.
|
|
32
64
|
|
|
33
|
-
All messages include a timestamp and are colored accordingly.
|
|
65
|
+
All messages include a timestamp and are colored accordingly (unless disabled via options).
|
package/dist/index.d.mts
CHANGED
|
@@ -1,29 +1,49 @@
|
|
|
1
|
+
type LogLevel = 'debug' | 'info' | 'warn' | 'error';
|
|
2
|
+
interface LoggerOptions {
|
|
3
|
+
enableColors?: boolean;
|
|
4
|
+
timestampFormat?: 'iso' | 'locale' | ((date: Date) => string);
|
|
5
|
+
logFormat?: (level: string, timestamp: string, args: unknown[]) => string;
|
|
6
|
+
logLevel?: LogLevel;
|
|
7
|
+
}
|
|
8
|
+
|
|
1
9
|
/**
|
|
2
10
|
* A simple logger with colored outputs and timestamps.
|
|
3
11
|
*/
|
|
4
12
|
declare class Logger {
|
|
13
|
+
private options;
|
|
14
|
+
private logLevel;
|
|
15
|
+
constructor(options?: LoggerOptions);
|
|
16
|
+
/**
|
|
17
|
+
* Sets the minimum log level for filtering messages.
|
|
18
|
+
* @param level - The log level to set.
|
|
19
|
+
*/
|
|
20
|
+
setLogLevel(level: LogLevel): void;
|
|
21
|
+
/**
|
|
22
|
+
* Checks if a log level should be output based on the current log level.
|
|
23
|
+
* @param level - The log level to check.
|
|
24
|
+
* @returns True if the message should be logged.
|
|
25
|
+
*/
|
|
26
|
+
private shouldLog;
|
|
5
27
|
/**
|
|
6
28
|
* Logs an info message.
|
|
7
|
-
* @param
|
|
29
|
+
* @param args - The arguments to log.
|
|
8
30
|
*/
|
|
9
|
-
info(
|
|
31
|
+
info(...args: unknown[]): void;
|
|
10
32
|
/**
|
|
11
33
|
* Logs a warning message.
|
|
12
|
-
* @param
|
|
34
|
+
* @param args - The arguments to log.
|
|
13
35
|
*/
|
|
14
|
-
warn(
|
|
36
|
+
warn(...args: unknown[]): void;
|
|
15
37
|
/**
|
|
16
38
|
* Logs an error message.
|
|
17
|
-
* @param
|
|
39
|
+
* @param args - The arguments to log.
|
|
18
40
|
*/
|
|
19
|
-
error(
|
|
41
|
+
error(...args: unknown[]): void;
|
|
20
42
|
/**
|
|
21
43
|
* Logs a debug message.
|
|
22
|
-
* @param
|
|
44
|
+
* @param args - The arguments to log.
|
|
23
45
|
*/
|
|
24
|
-
debug(
|
|
25
|
-
private getTimestamp;
|
|
26
|
-
private removeFunction;
|
|
46
|
+
debug(...args: unknown[]): void;
|
|
27
47
|
}
|
|
28
48
|
|
|
29
|
-
export { Logger };
|
|
49
|
+
export { type LogLevel, Logger, type LoggerOptions };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,29 +1,49 @@
|
|
|
1
|
+
type LogLevel = 'debug' | 'info' | 'warn' | 'error';
|
|
2
|
+
interface LoggerOptions {
|
|
3
|
+
enableColors?: boolean;
|
|
4
|
+
timestampFormat?: 'iso' | 'locale' | ((date: Date) => string);
|
|
5
|
+
logFormat?: (level: string, timestamp: string, args: unknown[]) => string;
|
|
6
|
+
logLevel?: LogLevel;
|
|
7
|
+
}
|
|
8
|
+
|
|
1
9
|
/**
|
|
2
10
|
* A simple logger with colored outputs and timestamps.
|
|
3
11
|
*/
|
|
4
12
|
declare class Logger {
|
|
13
|
+
private options;
|
|
14
|
+
private logLevel;
|
|
15
|
+
constructor(options?: LoggerOptions);
|
|
16
|
+
/**
|
|
17
|
+
* Sets the minimum log level for filtering messages.
|
|
18
|
+
* @param level - The log level to set.
|
|
19
|
+
*/
|
|
20
|
+
setLogLevel(level: LogLevel): void;
|
|
21
|
+
/**
|
|
22
|
+
* Checks if a log level should be output based on the current log level.
|
|
23
|
+
* @param level - The log level to check.
|
|
24
|
+
* @returns True if the message should be logged.
|
|
25
|
+
*/
|
|
26
|
+
private shouldLog;
|
|
5
27
|
/**
|
|
6
28
|
* Logs an info message.
|
|
7
|
-
* @param
|
|
29
|
+
* @param args - The arguments to log.
|
|
8
30
|
*/
|
|
9
|
-
info(
|
|
31
|
+
info(...args: unknown[]): void;
|
|
10
32
|
/**
|
|
11
33
|
* Logs a warning message.
|
|
12
|
-
* @param
|
|
34
|
+
* @param args - The arguments to log.
|
|
13
35
|
*/
|
|
14
|
-
warn(
|
|
36
|
+
warn(...args: unknown[]): void;
|
|
15
37
|
/**
|
|
16
38
|
* Logs an error message.
|
|
17
|
-
* @param
|
|
39
|
+
* @param args - The arguments to log.
|
|
18
40
|
*/
|
|
19
|
-
error(
|
|
41
|
+
error(...args: unknown[]): void;
|
|
20
42
|
/**
|
|
21
43
|
* Logs a debug message.
|
|
22
|
-
* @param
|
|
44
|
+
* @param args - The arguments to log.
|
|
23
45
|
*/
|
|
24
|
-
debug(
|
|
25
|
-
private getTimestamp;
|
|
26
|
-
private removeFunction;
|
|
46
|
+
debug(...args: unknown[]): void;
|
|
27
47
|
}
|
|
28
48
|
|
|
29
|
-
export { Logger };
|
|
49
|
+
export { type LogLevel, Logger, type LoggerOptions };
|
package/dist/index.js
CHANGED
|
@@ -33,41 +33,107 @@ __export(index_exports, {
|
|
|
33
33
|
Logger: () => Logger
|
|
34
34
|
});
|
|
35
35
|
module.exports = __toCommonJS(index_exports);
|
|
36
|
+
|
|
37
|
+
// src/utils.ts
|
|
36
38
|
var import_chalk = __toESM(require("chalk"));
|
|
39
|
+
function formatTimestamp(options, date = /* @__PURE__ */ new Date()) {
|
|
40
|
+
const { timestampFormat = "iso" } = options;
|
|
41
|
+
if (typeof timestampFormat === "function") {
|
|
42
|
+
return timestampFormat(date);
|
|
43
|
+
}
|
|
44
|
+
switch (timestampFormat) {
|
|
45
|
+
case "locale":
|
|
46
|
+
return date.toLocaleString();
|
|
47
|
+
case "iso":
|
|
48
|
+
default:
|
|
49
|
+
return date.toISOString();
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
function getColor(level, enableColors) {
|
|
53
|
+
if (!enableColors) return level;
|
|
54
|
+
const colors = {
|
|
55
|
+
"[INFO]": import_chalk.default.blue(level),
|
|
56
|
+
"[WARN]": import_chalk.default.yellow(level),
|
|
57
|
+
"[ERROR]": import_chalk.default.red(level),
|
|
58
|
+
"[DEBUG]": import_chalk.default.gray(level)
|
|
59
|
+
};
|
|
60
|
+
return colors[level] || level;
|
|
61
|
+
}
|
|
62
|
+
function formatLog(level, timestamp, args, options) {
|
|
63
|
+
const { logFormat, enableColors = true } = options;
|
|
64
|
+
const coloredLevel = getColor(level, enableColors);
|
|
65
|
+
if (logFormat) {
|
|
66
|
+
return [logFormat(coloredLevel, timestamp, args)];
|
|
67
|
+
}
|
|
68
|
+
return [`${coloredLevel} ${timestamp}`, ...args];
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// src/logger.ts
|
|
72
|
+
var LOG_LEVEL_PRIORITIES = {
|
|
73
|
+
debug: 0,
|
|
74
|
+
info: 1,
|
|
75
|
+
warn: 2,
|
|
76
|
+
error: 3
|
|
77
|
+
};
|
|
37
78
|
var Logger = class {
|
|
79
|
+
constructor(options = {}) {
|
|
80
|
+
this.options = {
|
|
81
|
+
enableColors: options.enableColors ?? true,
|
|
82
|
+
timestampFormat: options.timestampFormat ?? "iso",
|
|
83
|
+
logFormat: options.logFormat
|
|
84
|
+
};
|
|
85
|
+
this.logLevel = options.logLevel ?? "debug";
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Sets the minimum log level for filtering messages.
|
|
89
|
+
* @param level - The log level to set.
|
|
90
|
+
*/
|
|
91
|
+
setLogLevel(level) {
|
|
92
|
+
this.logLevel = level;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Checks if a log level should be output based on the current log level.
|
|
96
|
+
* @param level - The log level to check.
|
|
97
|
+
* @returns True if the message should be logged.
|
|
98
|
+
*/
|
|
99
|
+
shouldLog(level) {
|
|
100
|
+
return LOG_LEVEL_PRIORITIES[level] >= LOG_LEVEL_PRIORITIES[this.logLevel];
|
|
101
|
+
}
|
|
38
102
|
/**
|
|
39
103
|
* Logs an info message.
|
|
40
|
-
* @param
|
|
104
|
+
* @param args - The arguments to log.
|
|
41
105
|
*/
|
|
42
|
-
info(
|
|
43
|
-
|
|
106
|
+
info(...args) {
|
|
107
|
+
if (!this.shouldLog("info")) return;
|
|
108
|
+
const timestamp = formatTimestamp(this.options);
|
|
109
|
+
console.log(...formatLog("[INFO]", timestamp, args, this.options));
|
|
44
110
|
}
|
|
45
111
|
/**
|
|
46
112
|
* Logs a warning message.
|
|
47
|
-
* @param
|
|
113
|
+
* @param args - The arguments to log.
|
|
48
114
|
*/
|
|
49
|
-
warn(
|
|
50
|
-
|
|
115
|
+
warn(...args) {
|
|
116
|
+
if (!this.shouldLog("warn")) return;
|
|
117
|
+
const timestamp = formatTimestamp(this.options);
|
|
118
|
+
console.log(...formatLog("[WARN]", timestamp, args, this.options));
|
|
51
119
|
}
|
|
52
120
|
/**
|
|
53
121
|
* Logs an error message.
|
|
54
|
-
* @param
|
|
122
|
+
* @param args - The arguments to log.
|
|
55
123
|
*/
|
|
56
|
-
error(
|
|
57
|
-
|
|
124
|
+
error(...args) {
|
|
125
|
+
if (!this.shouldLog("error")) return;
|
|
126
|
+
const timestamp = formatTimestamp(this.options);
|
|
127
|
+
console.log(...formatLog("[ERROR]", timestamp, args, this.options));
|
|
58
128
|
}
|
|
59
129
|
/**
|
|
60
130
|
* Logs a debug message.
|
|
61
|
-
* @param
|
|
131
|
+
* @param args - The arguments to log.
|
|
62
132
|
*/
|
|
63
|
-
debug(
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
return (/* @__PURE__ */ new Date()).toISOString();
|
|
68
|
-
}
|
|
69
|
-
removeFunction() {
|
|
70
|
-
return "Please remove this function";
|
|
133
|
+
debug(...args) {
|
|
134
|
+
if (!this.shouldLog("debug")) return;
|
|
135
|
+
const timestamp = formatTimestamp(this.options);
|
|
136
|
+
console.log(...formatLog("[DEBUG]", timestamp, args, this.options));
|
|
71
137
|
}
|
|
72
138
|
};
|
|
73
139
|
// Annotate the CommonJS export names for ESM import in node:
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import chalk from 'chalk';\n\n/**\n * A simple logger with colored outputs and timestamps.\n */\nexport class Logger {\n /**\n *
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/utils.ts","../src/logger.ts"],"sourcesContent":["export { Logger } from './logger';\nexport type { LoggerOptions, LogLevel } from './types';\n","import chalk from 'chalk';\nimport type { LoggerOptions } from './types';\n\nexport function formatTimestamp(\n options: LoggerOptions,\n date: Date = new Date(),\n): string {\n const { timestampFormat = 'iso' } = options;\n if (typeof timestampFormat === 'function') {\n return timestampFormat(date);\n }\n switch (timestampFormat) {\n case 'locale':\n return date.toLocaleString();\n case 'iso':\n default:\n return date.toISOString();\n }\n}\n\nexport function getColor(level: string, enableColors: boolean): string {\n if (!enableColors) return level;\n const colors: Record<string, string> = {\n '[INFO]': chalk.blue(level),\n '[WARN]': chalk.yellow(level),\n '[ERROR]': chalk.red(level),\n '[DEBUG]': chalk.gray(level),\n };\n return colors[level] || level;\n}\n\nexport function formatLog(\n level: string,\n timestamp: string,\n args: unknown[],\n options: LoggerOptions,\n): [string, ...unknown[]] {\n const { logFormat, enableColors = true } = options;\n const coloredLevel = getColor(level, enableColors);\n if (logFormat) {\n return [logFormat(coloredLevel, timestamp, args)];\n }\n return [`${coloredLevel} ${timestamp}`, ...args];\n}\n","import type { LoggerOptions, LogLevel } from './types';\nimport { formatTimestamp, formatLog } from './utils';\n\nconst LOG_LEVEL_PRIORITIES: Record<LogLevel, number> = {\n debug: 0,\n info: 1,\n warn: 2,\n error: 3,\n};\n\n/**\n * A simple logger with colored outputs and timestamps.\n */\nexport class Logger {\n private options: LoggerOptions;\n private logLevel: LogLevel;\n\n constructor(options: LoggerOptions = {}) {\n this.options = {\n enableColors: options.enableColors ?? true,\n timestampFormat: options.timestampFormat ?? 'iso',\n logFormat: options.logFormat,\n };\n this.logLevel = options.logLevel ?? 'debug';\n }\n\n /**\n * Sets the minimum log level for filtering messages.\n * @param level - The log level to set.\n */\n setLogLevel(level: LogLevel): void {\n this.logLevel = level;\n }\n\n /**\n * Checks if a log level should be output based on the current log level.\n * @param level - The log level to check.\n * @returns True if the message should be logged.\n */\n private shouldLog(level: LogLevel): boolean {\n return LOG_LEVEL_PRIORITIES[level] >= LOG_LEVEL_PRIORITIES[this.logLevel];\n }\n\n /**\n * Logs an info message.\n * @param args - The arguments to log.\n */\n info(...args: unknown[]): void {\n if (!this.shouldLog('info')) return;\n const timestamp = formatTimestamp(this.options);\n console.log(...formatLog('[INFO]', timestamp, args, this.options));\n }\n\n /**\n * Logs a warning message.\n * @param args - The arguments to log.\n */\n warn(...args: unknown[]): void {\n if (!this.shouldLog('warn')) return;\n const timestamp = formatTimestamp(this.options);\n console.log(...formatLog('[WARN]', timestamp, args, this.options));\n }\n\n /**\n * Logs an error message.\n * @param args - The arguments to log.\n */\n error(...args: unknown[]): void {\n if (!this.shouldLog('error')) return;\n const timestamp = formatTimestamp(this.options);\n console.log(...formatLog('[ERROR]', timestamp, args, this.options));\n }\n\n /**\n * Logs a debug message.\n * @param args - The arguments to log.\n */\n debug(...args: unknown[]): void {\n if (!this.shouldLog('debug')) return;\n const timestamp = formatTimestamp(this.options);\n console.log(...formatLog('[DEBUG]', timestamp, args, this.options));\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;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__PURE__ */ new Date()) {
|
|
4
|
+
const { timestampFormat = "iso" } = options;
|
|
5
|
+
if (typeof timestampFormat === "function") {
|
|
6
|
+
return timestampFormat(date);
|
|
7
|
+
}
|
|
8
|
+
switch (timestampFormat) {
|
|
9
|
+
case "locale":
|
|
10
|
+
return date.toLocaleString();
|
|
11
|
+
case "iso":
|
|
12
|
+
default:
|
|
13
|
+
return date.toISOString();
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
function getColor(level, enableColors) {
|
|
17
|
+
if (!enableColors) return level;
|
|
18
|
+
const colors = {
|
|
19
|
+
"[INFO]": chalk.blue(level),
|
|
20
|
+
"[WARN]": chalk.yellow(level),
|
|
21
|
+
"[ERROR]": chalk.red(level),
|
|
22
|
+
"[DEBUG]": chalk.gray(level)
|
|
23
|
+
};
|
|
24
|
+
return colors[level] || level;
|
|
25
|
+
}
|
|
26
|
+
function formatLog(level, timestamp, args, options) {
|
|
27
|
+
const { logFormat, enableColors = true } = options;
|
|
28
|
+
const coloredLevel = getColor(level, enableColors);
|
|
29
|
+
if (logFormat) {
|
|
30
|
+
return [logFormat(coloredLevel, timestamp, args)];
|
|
31
|
+
}
|
|
32
|
+
return [`${coloredLevel} ${timestamp}`, ...args];
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// src/logger.ts
|
|
36
|
+
var LOG_LEVEL_PRIORITIES = {
|
|
37
|
+
debug: 0,
|
|
38
|
+
info: 1,
|
|
39
|
+
warn: 2,
|
|
40
|
+
error: 3
|
|
41
|
+
};
|
|
3
42
|
var Logger = class {
|
|
43
|
+
constructor(options = {}) {
|
|
44
|
+
this.options = {
|
|
45
|
+
enableColors: options.enableColors ?? true,
|
|
46
|
+
timestampFormat: options.timestampFormat ?? "iso",
|
|
47
|
+
logFormat: options.logFormat
|
|
48
|
+
};
|
|
49
|
+
this.logLevel = options.logLevel ?? "debug";
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Sets the minimum log level for filtering messages.
|
|
53
|
+
* @param level - The log level to set.
|
|
54
|
+
*/
|
|
55
|
+
setLogLevel(level) {
|
|
56
|
+
this.logLevel = level;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Checks if a log level should be output based on the current log level.
|
|
60
|
+
* @param level - The log level to check.
|
|
61
|
+
* @returns True if the message should be logged.
|
|
62
|
+
*/
|
|
63
|
+
shouldLog(level) {
|
|
64
|
+
return LOG_LEVEL_PRIORITIES[level] >= LOG_LEVEL_PRIORITIES[this.logLevel];
|
|
65
|
+
}
|
|
4
66
|
/**
|
|
5
67
|
* Logs an info message.
|
|
6
|
-
* @param
|
|
68
|
+
* @param args - The arguments to log.
|
|
7
69
|
*/
|
|
8
|
-
info(
|
|
9
|
-
|
|
70
|
+
info(...args) {
|
|
71
|
+
if (!this.shouldLog("info")) return;
|
|
72
|
+
const timestamp = formatTimestamp(this.options);
|
|
73
|
+
console.log(...formatLog("[INFO]", timestamp, args, this.options));
|
|
10
74
|
}
|
|
11
75
|
/**
|
|
12
76
|
* Logs a warning message.
|
|
13
|
-
* @param
|
|
77
|
+
* @param args - The arguments to log.
|
|
14
78
|
*/
|
|
15
|
-
warn(
|
|
16
|
-
|
|
79
|
+
warn(...args) {
|
|
80
|
+
if (!this.shouldLog("warn")) return;
|
|
81
|
+
const timestamp = formatTimestamp(this.options);
|
|
82
|
+
console.log(...formatLog("[WARN]", timestamp, args, this.options));
|
|
17
83
|
}
|
|
18
84
|
/**
|
|
19
85
|
* Logs an error message.
|
|
20
|
-
* @param
|
|
86
|
+
* @param args - The arguments to log.
|
|
21
87
|
*/
|
|
22
|
-
error(
|
|
23
|
-
|
|
88
|
+
error(...args) {
|
|
89
|
+
if (!this.shouldLog("error")) return;
|
|
90
|
+
const timestamp = formatTimestamp(this.options);
|
|
91
|
+
console.log(...formatLog("[ERROR]", timestamp, args, this.options));
|
|
24
92
|
}
|
|
25
93
|
/**
|
|
26
94
|
* Logs a debug message.
|
|
27
|
-
* @param
|
|
95
|
+
* @param args - The arguments to log.
|
|
28
96
|
*/
|
|
29
|
-
debug(
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
return (/* @__PURE__ */ new Date()).toISOString();
|
|
34
|
-
}
|
|
35
|
-
removeFunction() {
|
|
36
|
-
return "Please remove this function";
|
|
97
|
+
debug(...args) {
|
|
98
|
+
if (!this.shouldLog("debug")) return;
|
|
99
|
+
const timestamp = formatTimestamp(this.options);
|
|
100
|
+
console.log(...formatLog("[DEBUG]", timestamp, args, this.options));
|
|
37
101
|
}
|
|
38
102
|
};
|
|
39
103
|
export {
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/
|
|
1
|
+
{"version":3,"sources":["../src/utils.ts","../src/logger.ts"],"sourcesContent":["import chalk from 'chalk';\nimport type { LoggerOptions } from './types';\n\nexport function formatTimestamp(\n options: LoggerOptions,\n date: Date = new Date(),\n): string {\n const { timestampFormat = 'iso' } = options;\n if (typeof timestampFormat === 'function') {\n return timestampFormat(date);\n }\n switch (timestampFormat) {\n case 'locale':\n return date.toLocaleString();\n case 'iso':\n default:\n return date.toISOString();\n }\n}\n\nexport function getColor(level: string, enableColors: boolean): string {\n if (!enableColors) return level;\n const colors: Record<string, string> = {\n '[INFO]': chalk.blue(level),\n '[WARN]': chalk.yellow(level),\n '[ERROR]': chalk.red(level),\n '[DEBUG]': chalk.gray(level),\n };\n return colors[level] || level;\n}\n\nexport function formatLog(\n level: string,\n timestamp: string,\n args: unknown[],\n options: LoggerOptions,\n): [string, ...unknown[]] {\n const { logFormat, enableColors = true } = options;\n const coloredLevel = getColor(level, enableColors);\n if (logFormat) {\n return [logFormat(coloredLevel, timestamp, args)];\n }\n return [`${coloredLevel} ${timestamp}`, ...args];\n}\n","import type { LoggerOptions, LogLevel } from './types';\nimport { formatTimestamp, formatLog } from './utils';\n\nconst LOG_LEVEL_PRIORITIES: Record<LogLevel, number> = {\n debug: 0,\n info: 1,\n warn: 2,\n error: 3,\n};\n\n/**\n * A simple logger with colored outputs and timestamps.\n */\nexport class Logger {\n private options: LoggerOptions;\n private logLevel: LogLevel;\n\n constructor(options: LoggerOptions = {}) {\n this.options = {\n enableColors: options.enableColors ?? true,\n timestampFormat: options.timestampFormat ?? 'iso',\n logFormat: options.logFormat,\n };\n this.logLevel = options.logLevel ?? 'debug';\n }\n\n /**\n * Sets the minimum log level for filtering messages.\n * @param level - The log level to set.\n */\n setLogLevel(level: LogLevel): void {\n this.logLevel = level;\n }\n\n /**\n * Checks if a log level should be output based on the current log level.\n * @param level - The log level to check.\n * @returns True if the message should be logged.\n */\n private shouldLog(level: LogLevel): boolean {\n return LOG_LEVEL_PRIORITIES[level] >= LOG_LEVEL_PRIORITIES[this.logLevel];\n }\n\n /**\n * Logs an info message.\n * @param args - The arguments to log.\n */\n info(...args: unknown[]): void {\n if (!this.shouldLog('info')) return;\n const timestamp = formatTimestamp(this.options);\n console.log(...formatLog('[INFO]', timestamp, args, this.options));\n }\n\n /**\n * Logs a warning message.\n * @param args - The arguments to log.\n */\n warn(...args: unknown[]): void {\n if (!this.shouldLog('warn')) return;\n const timestamp = formatTimestamp(this.options);\n console.log(...formatLog('[WARN]', timestamp, args, this.options));\n }\n\n /**\n * Logs an error message.\n * @param args - The arguments to log.\n */\n error(...args: unknown[]): void {\n if (!this.shouldLog('error')) return;\n const timestamp = formatTimestamp(this.options);\n console.log(...formatLog('[ERROR]', timestamp, args, this.options));\n }\n\n /**\n * Logs a debug message.\n * @param args - The arguments to log.\n */\n debug(...args: unknown[]): void {\n if (!this.shouldLog('debug')) return;\n const timestamp = formatTimestamp(this.options);\n console.log(...formatLog('[DEBUG]', timestamp, args, this.options));\n }\n}\n"],"mappings":";AAAA,OAAO,WAAW;AAGX,SAAS,gBACd,SACA,OAAa,oBAAI,KAAK,GACd;AACR,QAAM,EAAE,kBAAkB,MAAM,IAAI;AACpC,MAAI,OAAO,oBAAoB,YAAY;AACzC,WAAO,gBAAgB,IAAI;A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AAAA;AAAA,EAMA,QAAQ,MAAuB;AAC7B,QAAI,CAAC,KAAK,UAAU,MAAM,EAAG;AAC7B,UAAM,YAAY,gBAAgB,KAAK,OAAO;AAC9C,YAAQ,IAAI,GAAG,UAAU,UAAU,WAAW,MAAM,KAAK,OAAO,CAAC;AAAA,EACnE;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,QAAQ,MAAuB;AAC7B,QAAI,CAAC,KAAK,UAAU,MAAM,EAAG;AAC7B,UAAM,YAAY,gBAAgB,KAAK,OAAO;AAC9C,YAAQ,IAAI,GAAG,UAAU,UAAU,WAAW,MAAM,KAAK,OAAO,CAAC;AAAA,EACnE;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,SAAS,MAAuB;AAC9B,QAAI,CAAC,KAAK,UAAU,OAAO,EAAG;AAC9B,UAAM,YAAY,gBAAgB,KAAK,OAAO;AAC9C,YAAQ,IAAI,GAAG,UAAU,WAAW,WAAW,MAAM,KAAK,OAAO,CAAC;AAAA,EACpE;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,SAAS,MAAuB;AAC9B,QAAI,CAAC,KAAK,UAAU,OAAO,EAAG;AAC9B,UAAM,YAAY,gBAAgB,KAAK,OAAO;AAC9C,YAAQ,IAAI,GAAG,UAAU,WAAW,WAAW,MAAM,KAAK,OAAO,CAAC;AAAA,EACpE;AACF;","names":[]}
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -1,46 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* A simple logger with colored outputs and timestamps.
|
|
5
|
-
*/
|
|
6
|
-
export class Logger {
|
|
7
|
-
/**
|
|
8
|
-
* Logs an info message.
|
|
9
|
-
* @param message - The message to log.
|
|
10
|
-
*/
|
|
11
|
-
info(message: string): void {
|
|
12
|
-
console.log(`${chalk.blue('[INFO]')} ${this.getTimestamp()} ${message}`);
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* Logs a warning message.
|
|
17
|
-
* @param message - The message to log.
|
|
18
|
-
*/
|
|
19
|
-
warn(message: string): void {
|
|
20
|
-
console.log(`${chalk.yellow('[WARN]')} ${this.getTimestamp()} ${message}`);
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* Logs an error message.
|
|
25
|
-
* @param message - The message to log.
|
|
26
|
-
*/
|
|
27
|
-
error(message: string): void {
|
|
28
|
-
console.log(`${chalk.red('[ERROR]')} ${this.getTimestamp()} ${message}`);
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* Logs a debug message.
|
|
33
|
-
* @param message - The message to log.
|
|
34
|
-
*/
|
|
35
|
-
debug(message: string): void {
|
|
36
|
-
console.log(`${chalk.gray('[DEBUG]')} ${this.getTimestamp()} ${message}`);
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
private getTimestamp(): string {
|
|
40
|
-
return new Date().toISOString();
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
private removeFunction() {
|
|
44
|
-
return 'Please remove this function';
|
|
45
|
-
}
|
|
46
|
-
}
|
|
1
|
+
export { Logger } from './logger';
|
|
2
|
+
export type { LoggerOptions, LogLevel } from './types';
|
package/src/logger.ts
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import type { LoggerOptions, LogLevel } from './types';
|
|
2
|
+
import { formatTimestamp, formatLog } from './utils';
|
|
3
|
+
|
|
4
|
+
const LOG_LEVEL_PRIORITIES: Record<LogLevel, number> = {
|
|
5
|
+
debug: 0,
|
|
6
|
+
info: 1,
|
|
7
|
+
warn: 2,
|
|
8
|
+
error: 3,
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* A simple logger with colored outputs and timestamps.
|
|
13
|
+
*/
|
|
14
|
+
export class Logger {
|
|
15
|
+
private options: LoggerOptions;
|
|
16
|
+
private logLevel: LogLevel;
|
|
17
|
+
|
|
18
|
+
constructor(options: LoggerOptions = {}) {
|
|
19
|
+
this.options = {
|
|
20
|
+
enableColors: options.enableColors ?? true,
|
|
21
|
+
timestampFormat: options.timestampFormat ?? 'iso',
|
|
22
|
+
logFormat: options.logFormat,
|
|
23
|
+
};
|
|
24
|
+
this.logLevel = options.logLevel ?? 'debug';
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Sets the minimum log level for filtering messages.
|
|
29
|
+
* @param level - The log level to set.
|
|
30
|
+
*/
|
|
31
|
+
setLogLevel(level: LogLevel): void {
|
|
32
|
+
this.logLevel = level;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Checks if a log level should be output based on the current log level.
|
|
37
|
+
* @param level - The log level to check.
|
|
38
|
+
* @returns True if the message should be logged.
|
|
39
|
+
*/
|
|
40
|
+
private shouldLog(level: LogLevel): boolean {
|
|
41
|
+
return LOG_LEVEL_PRIORITIES[level] >= LOG_LEVEL_PRIORITIES[this.logLevel];
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Logs an info message.
|
|
46
|
+
* @param args - The arguments to log.
|
|
47
|
+
*/
|
|
48
|
+
info(...args: unknown[]): void {
|
|
49
|
+
if (!this.shouldLog('info')) return;
|
|
50
|
+
const timestamp = formatTimestamp(this.options);
|
|
51
|
+
console.log(...formatLog('[INFO]', timestamp, args, this.options));
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Logs a warning message.
|
|
56
|
+
* @param args - The arguments to log.
|
|
57
|
+
*/
|
|
58
|
+
warn(...args: unknown[]): void {
|
|
59
|
+
if (!this.shouldLog('warn')) return;
|
|
60
|
+
const timestamp = formatTimestamp(this.options);
|
|
61
|
+
console.log(...formatLog('[WARN]', timestamp, args, this.options));
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Logs an error message.
|
|
66
|
+
* @param args - The arguments to log.
|
|
67
|
+
*/
|
|
68
|
+
error(...args: unknown[]): void {
|
|
69
|
+
if (!this.shouldLog('error')) return;
|
|
70
|
+
const timestamp = formatTimestamp(this.options);
|
|
71
|
+
console.log(...formatLog('[ERROR]', timestamp, args, this.options));
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Logs a debug message.
|
|
76
|
+
* @param args - The arguments to log.
|
|
77
|
+
*/
|
|
78
|
+
debug(...args: unknown[]): void {
|
|
79
|
+
if (!this.shouldLog('debug')) return;
|
|
80
|
+
const timestamp = formatTimestamp(this.options);
|
|
81
|
+
console.log(...formatLog('[DEBUG]', timestamp, args, this.options));
|
|
82
|
+
}
|
|
83
|
+
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export type LogLevel = 'debug' | 'info' | 'warn' | 'error';
|
|
2
|
+
|
|
3
|
+
export interface LoggerOptions {
|
|
4
|
+
enableColors?: boolean;
|
|
5
|
+
timestampFormat?: 'iso' | 'locale' | ((date: Date) => string);
|
|
6
|
+
logFormat?: (level: string, timestamp: string, args: unknown[]) => string;
|
|
7
|
+
logLevel?: LogLevel;
|
|
8
|
+
}
|
package/src/utils.ts
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import chalk from 'chalk';
|
|
2
|
+
import type { LoggerOptions } from './types';
|
|
3
|
+
|
|
4
|
+
export function formatTimestamp(
|
|
5
|
+
options: LoggerOptions,
|
|
6
|
+
date: Date = new Date(),
|
|
7
|
+
): string {
|
|
8
|
+
const { timestampFormat = 'iso' } = options;
|
|
9
|
+
if (typeof timestampFormat === 'function') {
|
|
10
|
+
return timestampFormat(date);
|
|
11
|
+
}
|
|
12
|
+
switch (timestampFormat) {
|
|
13
|
+
case 'locale':
|
|
14
|
+
return date.toLocaleString();
|
|
15
|
+
case 'iso':
|
|
16
|
+
default:
|
|
17
|
+
return date.toISOString();
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function getColor(level: string, enableColors: boolean): string {
|
|
22
|
+
if (!enableColors) return level;
|
|
23
|
+
const colors: Record<string, string> = {
|
|
24
|
+
'[INFO]': chalk.blue(level),
|
|
25
|
+
'[WARN]': chalk.yellow(level),
|
|
26
|
+
'[ERROR]': chalk.red(level),
|
|
27
|
+
'[DEBUG]': chalk.gray(level),
|
|
28
|
+
};
|
|
29
|
+
return colors[level] || level;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function formatLog(
|
|
33
|
+
level: string,
|
|
34
|
+
timestamp: string,
|
|
35
|
+
args: unknown[],
|
|
36
|
+
options: LoggerOptions,
|
|
37
|
+
): [string, ...unknown[]] {
|
|
38
|
+
const { logFormat, enableColors = true } = options;
|
|
39
|
+
const coloredLevel = getColor(level, enableColors);
|
|
40
|
+
if (logFormat) {
|
|
41
|
+
return [logFormat(coloredLevel, timestamp, args)];
|
|
42
|
+
}
|
|
43
|
+
return [`${coloredLevel} ${timestamp}`, ...args];
|
|
44
|
+
}
|
package/tests/logger.test.ts
CHANGED
|
@@ -27,33 +27,33 @@ describe('Logger', () => {
|
|
|
27
27
|
|
|
28
28
|
it('should have info method', () => {
|
|
29
29
|
logger.info('test message');
|
|
30
|
-
expect(consoleSpy).toHaveBeenCalledWith(expect.stringContaining('[INFO]'));
|
|
31
30
|
expect(consoleSpy).toHaveBeenCalledWith(
|
|
32
|
-
expect.stringContaining('
|
|
31
|
+
expect.stringContaining('[INFO]'),
|
|
32
|
+
'test message',
|
|
33
33
|
);
|
|
34
34
|
});
|
|
35
35
|
|
|
36
36
|
it('should have warn method', () => {
|
|
37
37
|
logger.warn('test message');
|
|
38
|
-
expect(consoleSpy).toHaveBeenCalledWith(expect.stringContaining('[WARN]'));
|
|
39
38
|
expect(consoleSpy).toHaveBeenCalledWith(
|
|
40
|
-
expect.stringContaining('
|
|
39
|
+
expect.stringContaining('[WARN]'),
|
|
40
|
+
'test message',
|
|
41
41
|
);
|
|
42
42
|
});
|
|
43
43
|
|
|
44
44
|
it('should have error method', () => {
|
|
45
45
|
logger.error('test message');
|
|
46
|
-
expect(consoleSpy).toHaveBeenCalledWith(expect.stringContaining('[ERROR]'));
|
|
47
46
|
expect(consoleSpy).toHaveBeenCalledWith(
|
|
48
|
-
expect.stringContaining('
|
|
47
|
+
expect.stringContaining('[ERROR]'),
|
|
48
|
+
'test message',
|
|
49
49
|
);
|
|
50
50
|
});
|
|
51
51
|
|
|
52
52
|
it('should have debug method', () => {
|
|
53
53
|
logger.debug('test message');
|
|
54
|
-
expect(consoleSpy).toHaveBeenCalledWith(expect.stringContaining('[DEBUG]'));
|
|
55
54
|
expect(consoleSpy).toHaveBeenCalledWith(
|
|
56
|
-
expect.stringContaining('
|
|
55
|
+
expect.stringContaining('[DEBUG]'),
|
|
56
|
+
'test message',
|
|
57
57
|
);
|
|
58
58
|
});
|
|
59
59
|
|
|
@@ -62,4 +62,119 @@ describe('Logger', () => {
|
|
|
62
62
|
const callArgs = consoleSpy.mock.calls[0][0];
|
|
63
63
|
expect(callArgs).toMatch(/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z/);
|
|
64
64
|
});
|
|
65
|
+
|
|
66
|
+
it('should handle multiple arguments', () => {
|
|
67
|
+
logger.info('hello', 'world', 42, { key: 'value' });
|
|
68
|
+
const calls = consoleSpy.mock.calls[0];
|
|
69
|
+
expect(calls[0]).toMatch(/\[INFO\]/);
|
|
70
|
+
expect(calls[1]).toBe('hello');
|
|
71
|
+
expect(calls[2]).toBe('world');
|
|
72
|
+
expect(calls[3]).toBe(42);
|
|
73
|
+
expect(calls[4]).toEqual({ key: 'value' });
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it('should disable colors when enableColors is false', () => {
|
|
77
|
+
const noColorLogger = new Logger({ enableColors: false });
|
|
78
|
+
noColorLogger.info('test message');
|
|
79
|
+
const callArgs = consoleSpy.mock.calls[0][0];
|
|
80
|
+
expect(callArgs).toContain('[INFO]'); // Should not have ANSI codes
|
|
81
|
+
expect(callArgs).not.toContain('\u001b['); // ANSI escape code
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it('should use locale timestamp format', () => {
|
|
85
|
+
const localeLogger = new Logger({ timestampFormat: 'locale' });
|
|
86
|
+
localeLogger.info('test');
|
|
87
|
+
const callArgs = consoleSpy.mock.calls[0][0];
|
|
88
|
+
expect(callArgs).toMatch(/\d{1,2}\/\d{1,2}\/\d{4}/); // Basic locale date check
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it('should use custom timestamp function', () => {
|
|
92
|
+
const customLogger = new Logger({
|
|
93
|
+
timestampFormat: () => 'custom-time',
|
|
94
|
+
});
|
|
95
|
+
customLogger.info('test');
|
|
96
|
+
const callArgs = consoleSpy.mock.calls[0][0];
|
|
97
|
+
expect(callArgs).toContain('custom-time');
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it('should use custom log format', () => {
|
|
101
|
+
const customLogger = new Logger({
|
|
102
|
+
logFormat: (level, timestamp, args) =>
|
|
103
|
+
`${timestamp} ${level}: ${args.join(' ')}`,
|
|
104
|
+
});
|
|
105
|
+
customLogger.warn('hello', 'world');
|
|
106
|
+
const callArgs = consoleSpy.mock.calls[0][0];
|
|
107
|
+
expect(callArgs).toMatch(
|
|
108
|
+
/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z \[WARN\]: hello world/,
|
|
109
|
+
);
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
it('should default to debug log level', () => {
|
|
113
|
+
const defaultLogger = new Logger();
|
|
114
|
+
defaultLogger.debug('debug message');
|
|
115
|
+
expect(consoleSpy).toHaveBeenCalledWith(
|
|
116
|
+
expect.stringContaining('[DEBUG]'),
|
|
117
|
+
'debug message',
|
|
118
|
+
);
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it('should filter logs below the set level', () => {
|
|
122
|
+
const infoLogger = new Logger({ logLevel: 'info' });
|
|
123
|
+
infoLogger.debug('debug message');
|
|
124
|
+
expect(consoleSpy).not.toHaveBeenCalled();
|
|
125
|
+
|
|
126
|
+
infoLogger.info('info message');
|
|
127
|
+
expect(consoleSpy).toHaveBeenCalledWith(
|
|
128
|
+
expect.stringContaining('[INFO]'),
|
|
129
|
+
'info message',
|
|
130
|
+
);
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
it('should allow all levels when set to debug', () => {
|
|
134
|
+
const debugLogger = new Logger({ logLevel: 'debug' });
|
|
135
|
+
debugLogger.debug('debug');
|
|
136
|
+
debugLogger.info('info');
|
|
137
|
+
debugLogger.warn('warn');
|
|
138
|
+
debugLogger.error('error');
|
|
139
|
+
expect(consoleSpy).toHaveBeenCalledTimes(4);
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
it('should filter debug and info when set to warn', () => {
|
|
143
|
+
const warnLogger = new Logger({ logLevel: 'warn' });
|
|
144
|
+
warnLogger.debug('debug');
|
|
145
|
+
warnLogger.info('info');
|
|
146
|
+
expect(consoleSpy).not.toHaveBeenCalled();
|
|
147
|
+
|
|
148
|
+
warnLogger.warn('warn');
|
|
149
|
+
warnLogger.error('error');
|
|
150
|
+
expect(consoleSpy).toHaveBeenCalledTimes(2);
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
it('should only log errors when set to error', () => {
|
|
154
|
+
const errorLogger = new Logger({ logLevel: 'error' });
|
|
155
|
+
errorLogger.debug('debug');
|
|
156
|
+
errorLogger.info('info');
|
|
157
|
+
errorLogger.warn('warn');
|
|
158
|
+
expect(consoleSpy).not.toHaveBeenCalled();
|
|
159
|
+
|
|
160
|
+
errorLogger.error('error');
|
|
161
|
+
expect(consoleSpy).toHaveBeenCalledWith(
|
|
162
|
+
expect.stringContaining('[ERROR]'),
|
|
163
|
+
'error',
|
|
164
|
+
);
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
it('should allow changing log level dynamically', () => {
|
|
168
|
+
const logger = new Logger();
|
|
169
|
+
logger.setLogLevel('error');
|
|
170
|
+
logger.debug('debug');
|
|
171
|
+
expect(consoleSpy).not.toHaveBeenCalled();
|
|
172
|
+
|
|
173
|
+
logger.setLogLevel('debug');
|
|
174
|
+
logger.debug('debug');
|
|
175
|
+
expect(consoleSpy).toHaveBeenCalledWith(
|
|
176
|
+
expect.stringContaining('[DEBUG]'),
|
|
177
|
+
'debug',
|
|
178
|
+
);
|
|
179
|
+
});
|
|
65
180
|
});
|