@innei/pretty-logger-nestjs 0.3.4 → 0.3.5

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/dist/index.d.mts CHANGED
@@ -15,20 +15,24 @@ declare class LoggerModule {
15
15
  declare class Logger extends ConsoleLogger {
16
16
  private static loggerInstance;
17
17
  static setLoggerInstance(logger: WrappedConsola$1): void;
18
- constructor(context: string, options: ConsoleLoggerOptions);
19
- private _getColorByLogLevel;
18
+ constructor(context?: string, options?: ConsoleLoggerOptions);
20
19
  private lastTimestampAt;
21
20
  private _updateAndGetTimestampDiff;
22
- protected formatMessage(message: any, logLevel?: string): any;
23
- log(message: any, context?: string, ...argv: any[]): void;
24
- warn(message: any, context?: string, ...argv: any[]): void;
25
- debug(message: any, context?: string, ...argv: any[]): void;
26
- verbose(message: any, context?: string, ...argv: any[]): void;
27
- fatal(message: any, context?: string, ...argv: any[]): void;
28
- error(message: any, context?: string, ...argv: any[]): void;
21
+ private getContextPrefix;
22
+ private get workerPrefix();
23
+ /**
24
+ * Check if the last argument looks like a NestJS context string.
25
+ * NestJS passes context as the last string argument (e.g., 'AppService', 'UserController').
26
+ * Context strings are typically PascalCase class names.
27
+ */
28
+ private isContextString;
29
29
  private print;
30
- private defaultContextPrefix;
31
- }
32
-
33
- //#endregion
30
+ log(...args: any[]): void;
31
+ info(...args: any[]): void;
32
+ warn(...args: any[]): void;
33
+ debug(...args: any[]): void;
34
+ verbose(...args: any[]): void;
35
+ fatal(...args: any[]): void;
36
+ error(...args: any[]): void;
37
+ } //#endregion
34
38
  export { Core, Logger, LoggerConsolaOptions, LoggerModule, WrappedConsola, createLogger };
package/dist/index.d.ts CHANGED
@@ -15,20 +15,24 @@ declare class LoggerModule {
15
15
  declare class Logger extends ConsoleLogger {
16
16
  private static loggerInstance;
17
17
  static setLoggerInstance(logger: WrappedConsola$1): void;
18
- constructor(context: string, options: ConsoleLoggerOptions);
19
- private _getColorByLogLevel;
18
+ constructor(context?: string, options?: ConsoleLoggerOptions);
20
19
  private lastTimestampAt;
21
20
  private _updateAndGetTimestampDiff;
22
- protected formatMessage(message: any, logLevel?: string): any;
23
- log(message: any, context?: string, ...argv: any[]): void;
24
- warn(message: any, context?: string, ...argv: any[]): void;
25
- debug(message: any, context?: string, ...argv: any[]): void;
26
- verbose(message: any, context?: string, ...argv: any[]): void;
27
- fatal(message: any, context?: string, ...argv: any[]): void;
28
- error(message: any, context?: string, ...argv: any[]): void;
21
+ private getContextPrefix;
22
+ private get workerPrefix();
23
+ /**
24
+ * Check if the last argument looks like a NestJS context string.
25
+ * NestJS passes context as the last string argument (e.g., 'AppService', 'UserController').
26
+ * Context strings are typically PascalCase class names.
27
+ */
28
+ private isContextString;
29
29
  private print;
30
- private defaultContextPrefix;
31
- }
32
-
33
- //#endregion
30
+ log(...args: any[]): void;
31
+ info(...args: any[]): void;
32
+ warn(...args: any[]): void;
33
+ debug(...args: any[]): void;
34
+ verbose(...args: any[]): void;
35
+ fatal(...args: any[]): void;
36
+ error(...args: any[]): void;
37
+ } //#endregion
34
38
  export { Core, Logger, LoggerConsolaOptions, LoggerModule, WrappedConsola, createLogger };
package/dist/index.js CHANGED
@@ -35,59 +35,64 @@ var Logger = class Logger extends __nestjs_common.ConsoleLogger {
35
35
  this.loggerInstance = logger;
36
36
  }
37
37
  constructor(context, options) {
38
- super(context, options);
38
+ super(context || "", options || {});
39
39
  this.lastTimestampAt = Date.now();
40
- this.defaultContextPrefix = this.context ? `[${picocolors.default.yellow(this.context)}] ` : `[${picocolors.default.red("System")}] `;
41
- }
42
- _getColorByLogLevel(logLevel) {
43
- switch (logLevel) {
44
- case "debug": return picocolors.default.cyan;
45
- case "warn": return picocolors.default.yellow;
46
- case "error": return picocolors.default.red;
47
- case "verbose": return picocolors.default.gray;
48
- default: return picocolors.default.green;
49
- }
50
40
  }
51
41
  _updateAndGetTimestampDiff() {
52
- const includeTimestamp = this.lastTimestampAt && this.options.timestamp;
42
+ const includeTimestamp = this.lastTimestampAt && this.options?.timestamp;
53
43
  const now = Date.now();
54
44
  const result = includeTimestamp ? picocolors.default.yellow(` +${now - this.lastTimestampAt}ms`) : "";
55
45
  this.lastTimestampAt = now;
56
46
  return result;
57
47
  }
58
- formatMessage(message, logLevel = "log") {
59
- const formatMessage = typeof message == "string" ? this._getColorByLogLevel(logLevel)(message) : message;
60
- return formatMessage;
48
+ getContextPrefix(context) {
49
+ const ctx = context || this.context;
50
+ return ctx ? `[${picocolors.default.yellow(ctx)}]` : `[${picocolors.default.red("System")}]`;
61
51
  }
62
- log(message, context, ...argv) {
63
- this.print("info", message, context, ...argv);
52
+ get workerPrefix() {
53
+ return node_cluster.default.isWorker ? picocolors.default.yellow(`*Worker - ${node_cluster.default.worker.id}*`) : "";
64
54
  }
65
- warn(message, context, ...argv) {
66
- this.print("warn", message, context, ...argv);
55
+ /**
56
+ * Check if the last argument looks like a NestJS context string.
57
+ * NestJS passes context as the last string argument (e.g., 'AppService', 'UserController').
58
+ * Context strings are typically PascalCase class names.
59
+ */
60
+ isContextString(value) {
61
+ if (typeof value !== "string") return false;
62
+ return /^[A-Z][a-zA-Z0-9]*$/.test(value);
67
63
  }
68
- debug(message, context, ...argv) {
69
- this.print("debug", message, context, ...argv);
64
+ print(level, ...args) {
65
+ const print = Logger.loggerInstance[level];
66
+ const diff = this._updateAndGetTimestampDiff();
67
+ let context;
68
+ let messages = args;
69
+ if (args.length > 0 && this.isContextString(args.at(-1))) {
70
+ context = args.at(-1);
71
+ messages = args.slice(0, -1);
72
+ }
73
+ const prefix = this.workerPrefix ? `${this.workerPrefix} ${this.getContextPrefix(context)}` : this.getContextPrefix(context);
74
+ print(prefix, ...messages, diff);
70
75
  }
71
- verbose(message, context, ...argv) {
72
- this.print("verbose", message, context, ...argv);
76
+ log(...args) {
77
+ this.print("info", ...args);
73
78
  }
74
- fatal(message, context, ...argv) {
75
- this.print("fatal", message, context, ...argv);
79
+ info(...args) {
80
+ this.print("info", ...args);
76
81
  }
77
- error(message, context, ...argv) {
78
- const trace = context;
79
- const _context = argv[0];
80
- if (!trace && _context) this.print("error", message, _context, ...argv.slice(1));
81
- else this.print("error", message, context, ...argv);
82
+ warn(...args) {
83
+ this.print("warn", ...args);
82
84
  }
83
- print(level, message, context, ...argv) {
84
- const print = Logger.loggerInstance[level];
85
- const formatMessage = this.formatMessage(message, level);
86
- const diff = this._updateAndGetTimestampDiff();
87
- const workerPrefix = node_cluster.default.isWorker ? picocolors.default.yellow(`*Worker - ${node_cluster.default.worker.id}*`) : "";
88
- if (context && argv.length === 0) print(`${workerPrefix} [${picocolors.default.yellow(context)}] `, formatMessage, diff);
89
- else if (argv.length === 0) print(`${workerPrefix} ${this.defaultContextPrefix}`, formatMessage, diff);
90
- else print(`${workerPrefix} ${this.defaultContextPrefix}`, message, context, ...argv, diff);
85
+ debug(...args) {
86
+ this.print("debug", ...args);
87
+ }
88
+ verbose(...args) {
89
+ this.print("verbose", ...args);
90
+ }
91
+ fatal(...args) {
92
+ this.print("fatal", ...args);
93
+ }
94
+ error(...args) {
95
+ this.print("error", ...args);
91
96
  }
92
97
  };
93
98
  Logger.loggerInstance = (0, __innei_pretty_logger_core.createLoggerConsola)();
package/dist/index.mjs CHANGED
@@ -36,59 +36,64 @@ var Logger = class Logger extends ConsoleLogger {
36
36
  this.loggerInstance = logger;
37
37
  }
38
38
  constructor(context, options) {
39
- super(context, options);
39
+ super(context || "", options || {});
40
40
  this.lastTimestampAt = Date.now();
41
- this.defaultContextPrefix = this.context ? `[${picocolors.yellow(this.context)}] ` : `[${picocolors.red("System")}] `;
42
- }
43
- _getColorByLogLevel(logLevel) {
44
- switch (logLevel) {
45
- case "debug": return picocolors.cyan;
46
- case "warn": return picocolors.yellow;
47
- case "error": return picocolors.red;
48
- case "verbose": return picocolors.gray;
49
- default: return picocolors.green;
50
- }
51
41
  }
52
42
  _updateAndGetTimestampDiff() {
53
- const includeTimestamp = this.lastTimestampAt && this.options.timestamp;
43
+ const includeTimestamp = this.lastTimestampAt && this.options?.timestamp;
54
44
  const now = Date.now();
55
45
  const result = includeTimestamp ? picocolors.yellow(` +${now - this.lastTimestampAt}ms`) : "";
56
46
  this.lastTimestampAt = now;
57
47
  return result;
58
48
  }
59
- formatMessage(message, logLevel = "log") {
60
- const formatMessage = typeof message == "string" ? this._getColorByLogLevel(logLevel)(message) : message;
61
- return formatMessage;
49
+ getContextPrefix(context) {
50
+ const ctx = context || this.context;
51
+ return ctx ? `[${picocolors.yellow(ctx)}]` : `[${picocolors.red("System")}]`;
62
52
  }
63
- log(message, context, ...argv) {
64
- this.print("info", message, context, ...argv);
53
+ get workerPrefix() {
54
+ return cluster.isWorker ? picocolors.yellow(`*Worker - ${cluster.worker.id}*`) : "";
65
55
  }
66
- warn(message, context, ...argv) {
67
- this.print("warn", message, context, ...argv);
56
+ /**
57
+ * Check if the last argument looks like a NestJS context string.
58
+ * NestJS passes context as the last string argument (e.g., 'AppService', 'UserController').
59
+ * Context strings are typically PascalCase class names.
60
+ */
61
+ isContextString(value) {
62
+ if (typeof value !== "string") return false;
63
+ return /^[A-Z][a-zA-Z0-9]*$/.test(value);
68
64
  }
69
- debug(message, context, ...argv) {
70
- this.print("debug", message, context, ...argv);
65
+ print(level, ...args) {
66
+ const print = Logger.loggerInstance[level];
67
+ const diff = this._updateAndGetTimestampDiff();
68
+ let context;
69
+ let messages = args;
70
+ if (args.length > 0 && this.isContextString(args.at(-1))) {
71
+ context = args.at(-1);
72
+ messages = args.slice(0, -1);
73
+ }
74
+ const prefix = this.workerPrefix ? `${this.workerPrefix} ${this.getContextPrefix(context)}` : this.getContextPrefix(context);
75
+ print(prefix, ...messages, diff);
71
76
  }
72
- verbose(message, context, ...argv) {
73
- this.print("verbose", message, context, ...argv);
77
+ log(...args) {
78
+ this.print("info", ...args);
74
79
  }
75
- fatal(message, context, ...argv) {
76
- this.print("fatal", message, context, ...argv);
80
+ info(...args) {
81
+ this.print("info", ...args);
77
82
  }
78
- error(message, context, ...argv) {
79
- const trace = context;
80
- const _context = argv[0];
81
- if (!trace && _context) this.print("error", message, _context, ...argv.slice(1));
82
- else this.print("error", message, context, ...argv);
83
+ warn(...args) {
84
+ this.print("warn", ...args);
83
85
  }
84
- print(level, message, context, ...argv) {
85
- const print = Logger.loggerInstance[level];
86
- const formatMessage = this.formatMessage(message, level);
87
- const diff = this._updateAndGetTimestampDiff();
88
- const workerPrefix = cluster.isWorker ? picocolors.yellow(`*Worker - ${cluster.worker.id}*`) : "";
89
- if (context && argv.length === 0) print(`${workerPrefix} [${picocolors.yellow(context)}] `, formatMessage, diff);
90
- else if (argv.length === 0) print(`${workerPrefix} ${this.defaultContextPrefix}`, formatMessage, diff);
91
- else print(`${workerPrefix} ${this.defaultContextPrefix}`, message, context, ...argv, diff);
86
+ debug(...args) {
87
+ this.print("debug", ...args);
88
+ }
89
+ verbose(...args) {
90
+ this.print("verbose", ...args);
91
+ }
92
+ fatal(...args) {
93
+ this.print("fatal", ...args);
94
+ }
95
+ error(...args) {
96
+ this.print("error", ...args);
92
97
  }
93
98
  };
94
99
  Logger.loggerInstance = createLoggerConsola();
package/logger.service.ts CHANGED
@@ -1,12 +1,12 @@
1
1
  import cluster from 'node:cluster'
2
-
2
+ import picocolors from 'picocolors'
3
3
  import type { WrappedConsola } from '@innei/pretty-logger-core'
4
- import { createLoggerConsola } from '@innei/pretty-logger-core'
5
4
  import type { ConsoleLoggerOptions } from '@nestjs/common'
5
+
6
+ import { createLoggerConsola } from '@innei/pretty-logger-core'
6
7
  import { ConsoleLogger } from '@nestjs/common'
7
- import picocolors from 'picocolors'
8
8
 
9
- type LoggerType =
9
+ type LogLevel =
10
10
  | 'info'
11
11
  | 'log'
12
12
  | 'error'
@@ -22,32 +22,13 @@ export class Logger extends ConsoleLogger {
22
22
  this.loggerInstance = logger
23
23
  }
24
24
 
25
- constructor(context: string, options: ConsoleLoggerOptions) {
26
- super(context, options)
27
- }
28
- private _getColorByLogLevel(logLevel: string) {
29
- switch (logLevel) {
30
- case 'debug': {
31
- return picocolors.cyan
32
- }
33
- case 'warn': {
34
- return picocolors.yellow
35
- }
36
- case 'error': {
37
- return picocolors.red
38
- }
39
- case 'verbose': {
40
- return picocolors.gray
41
- }
42
- default: {
43
- return picocolors.green
44
- }
45
- }
25
+ constructor(context?: string, options?: ConsoleLoggerOptions) {
26
+ super(context || '', options || {})
46
27
  }
47
28
 
48
29
  private lastTimestampAt: number = Date.now()
49
30
  private _updateAndGetTimestampDiff() {
50
- const includeTimestamp = this.lastTimestampAt && this.options.timestamp
31
+ const includeTimestamp = this.lastTimestampAt && this.options?.timestamp
51
32
  const now = Date.now()
52
33
  const result = includeTimestamp
53
34
  ? picocolors.yellow(` +${now - this.lastTimestampAt}ms`)
@@ -55,77 +36,75 @@ export class Logger extends ConsoleLogger {
55
36
  this.lastTimestampAt = now
56
37
  return result
57
38
  }
58
- protected formatMessage(message: any, logLevel = 'log') {
59
- const formatMessage =
60
- typeof message == 'string'
61
- ? this._getColorByLogLevel(logLevel)(message)
62
- : message
63
- return formatMessage
39
+
40
+ private getContextPrefix(context?: string) {
41
+ const ctx = context || this.context
42
+ return ctx ? `[${picocolors.yellow(ctx)}]` : `[${picocolors.red('System')}]`
64
43
  }
65
44
 
66
- log(message: any, context?: string, ...argv: any[]) {
67
- this.print('info', message, context, ...argv)
45
+ private get workerPrefix() {
46
+ return cluster.isWorker
47
+ ? picocolors.yellow(`*Worker - ${cluster.worker!.id}*`)
48
+ : ''
68
49
  }
69
50
 
70
- warn(message: any, context?: string, ...argv: any[]) {
71
- this.print('warn', message, context, ...argv)
51
+ /**
52
+ * Check if the last argument looks like a NestJS context string.
53
+ * NestJS passes context as the last string argument (e.g., 'AppService', 'UserController').
54
+ * Context strings are typically PascalCase class names.
55
+ */
56
+ private isContextString(value: unknown): value is string {
57
+ if (typeof value !== 'string') return false
58
+ // Context is usually a short PascalCase identifier (class name)
59
+ // Not a sentence, not a path, not containing spaces or special chars
60
+ return /^[A-Z][a-zA-Z0-9]*$/.test(value)
72
61
  }
73
- debug(message: any, context?: string, ...argv: any[]) {
74
- this.print('debug', message, context, ...argv)
62
+
63
+ private print(level: LogLevel, ...args: any[]) {
64
+ const print = Logger.loggerInstance[level]
65
+ const diff = this._updateAndGetTimestampDiff()
66
+
67
+ let context: string | undefined
68
+ let messages = args
69
+
70
+ // Check if the last argument is a context string (NestJS convention)
71
+ if (args.length > 0 && this.isContextString(args.at(-1))) {
72
+ context = args.at(-1)
73
+ messages = args.slice(0, -1)
74
+ }
75
+
76
+ const prefix = this.workerPrefix
77
+ ? `${this.workerPrefix} ${this.getContextPrefix(context)}`
78
+ : this.getContextPrefix(context)
79
+
80
+ print(prefix, ...messages, diff)
75
81
  }
76
82
 
77
- verbose(message: any, context?: string, ...argv: any[]) {
78
- this.print('verbose', message, context, ...argv)
83
+ log(...args: any[]) {
84
+ this.print('info', ...args)
79
85
  }
80
86
 
81
- fatal(message: any, context?: string, ...argv: any[]) {
82
- this.print('fatal', message, context, ...argv)
87
+ info(...args: any[]) {
88
+ this.print('info', ...args)
83
89
  }
84
90
 
85
- error(message: any, context?: string, ...argv: any[]) {
86
- const trace = context
87
- const _context = argv[0]
91
+ warn(...args: any[]) {
92
+ this.print('warn', ...args)
93
+ }
88
94
 
89
- if (!trace && _context) {
90
- this.print('error', message, _context, ...argv.slice(1))
91
- } else {
92
- this.print('error', message, context, ...argv)
93
- }
95
+ debug(...args: any[]) {
96
+ this.print('debug', ...args)
94
97
  }
95
98
 
96
- private print(
97
- level: LoggerType,
98
- message: any,
99
- context?: string,
100
- ...argv: any[]
101
- ) {
102
- const print = Logger.loggerInstance[level]
103
- const formatMessage = this.formatMessage(message, level)
104
- const diff = this._updateAndGetTimestampDiff()
99
+ verbose(...args: any[]) {
100
+ this.print('verbose', ...args)
101
+ }
105
102
 
106
- const workerPrefix = cluster.isWorker
107
- ? picocolors.yellow(`*Worker - ${cluster!.worker!.id}*`)
108
- : ''
109
- if (context && argv.length === 0) {
110
- print(
111
- `${workerPrefix} [${picocolors.yellow(context)}] `,
112
- formatMessage,
113
- diff,
114
- )
115
- } else if (argv.length === 0) {
116
- print(`${workerPrefix} ${this.defaultContextPrefix}`, formatMessage, diff)
117
- } else {
118
- print(
119
- `${workerPrefix} ${this.defaultContextPrefix}`,
120
- message,
121
- context,
122
- ...argv,
123
- diff,
124
- )
125
- }
103
+ fatal(...args: any[]) {
104
+ this.print('fatal', ...args)
126
105
  }
127
106
 
128
- private defaultContextPrefix = this.context
129
- ? `[${picocolors.yellow(this.context)}] `
130
- : `[${picocolors.red('System')}] `
107
+ error(...args: any[]) {
108
+ this.print('error', ...args)
109
+ }
131
110
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@innei/pretty-logger-nestjs",
3
- "version": "0.3.4",
3
+ "version": "0.3.5",
4
4
  "exports": {
5
5
  ".": {
6
6
  "require": "./dist/index.js",
@@ -10,20 +10,20 @@
10
10
  "main": "dist/index.js",
11
11
  "module": "dist/index.mjs",
12
12
  "types": "dist/index.d.ts",
13
+ "scripts": {
14
+ "build": "tsdown"
15
+ },
13
16
  "peerDependencies": {
14
17
  "@nestjs/common": ">=10"
15
18
  },
16
19
  "dependencies": {
20
+ "@innei/pretty-logger-core": "workspace:*",
17
21
  "cron": "4.3.0",
18
22
  "defu": "^6.1.3",
19
23
  "picocolors": "^1.0.0",
20
- "std-env": "^3.5.0",
21
- "@innei/pretty-logger-core": "0.3.4"
24
+ "std-env": "^3.5.0"
22
25
  },
23
26
  "devDependencies": {
24
27
  "@oxc-project/runtime": "^0.70.0"
25
- },
26
- "scripts": {
27
- "build": "tsdown"
28
28
  }
29
- }
29
+ }