@idlebox/logger 0.0.21 → 0.0.23

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 (98) hide show
  1. package/lib/common/ansi.d.ts +17 -13
  2. package/lib/common/ansi.d.ts.map +1 -1
  3. package/lib/common/ansi.js +5 -1
  4. package/lib/common/ansi.js.map +1 -1
  5. package/lib/common/create.compability.d.ts +5 -0
  6. package/lib/common/create.compability.d.ts.map +1 -0
  7. package/lib/common/create.compability.js +4 -0
  8. package/lib/common/create.compability.js.map +1 -0
  9. package/lib/common/create.function.d.ts +9 -5
  10. package/lib/common/create.function.d.ts.map +1 -1
  11. package/lib/common/create.function.js +52 -21
  12. package/lib/common/create.function.js.map +1 -1
  13. package/lib/common/create.logger.d.ts +3 -3
  14. package/lib/common/create.logger.d.ts.map +1 -1
  15. package/lib/common/create.logger.js +21 -20
  16. package/lib/common/create.logger.js.map +1 -1
  17. package/lib/common/helpers.d.ts +2 -6
  18. package/lib/common/helpers.d.ts.map +1 -1
  19. package/lib/common/helpers.js +4 -37
  20. package/lib/common/helpers.js.map +1 -1
  21. package/lib/common/logger.create.d.ts +8 -4
  22. package/lib/common/logger.create.d.ts.map +1 -1
  23. package/lib/common/logger.create.js +7 -14
  24. package/lib/common/logger.create.js.map +1 -1
  25. package/lib/common/logger.global.d.ts +4 -3
  26. package/lib/common/logger.global.d.ts.map +1 -1
  27. package/lib/common/logger.global.js +11 -17
  28. package/lib/common/logger.global.js.map +1 -1
  29. package/lib/common/registry.d.ts +8 -0
  30. package/lib/common/registry.d.ts.map +1 -0
  31. package/lib/common/registry.js +36 -0
  32. package/lib/common/registry.js.map +1 -0
  33. package/lib/common/types.d.ts +78 -22
  34. package/lib/common/types.d.ts.map +1 -1
  35. package/lib/common/types.js +2 -11
  36. package/lib/common/types.js.map +1 -1
  37. package/lib/{common/debug.commands.d.ts → functions/builtin-commands.d.ts} +2 -2
  38. package/lib/functions/builtin-commands.d.ts.map +1 -0
  39. package/lib/{common/debug.commands.js → functions/builtin-commands.js} +3 -10
  40. package/lib/functions/builtin-commands.js.map +1 -0
  41. package/lib/loglevels/control.browser.d.ts +2 -0
  42. package/lib/loglevels/control.browser.d.ts.map +1 -0
  43. package/lib/loglevels/control.browser.js +2 -0
  44. package/lib/loglevels/control.browser.js.map +1 -0
  45. package/lib/loglevels/control.nodejs.d.ts +2 -0
  46. package/lib/loglevels/control.nodejs.d.ts.map +1 -0
  47. package/lib/loglevels/control.nodejs.js +2 -0
  48. package/lib/loglevels/control.nodejs.js.map +1 -0
  49. package/lib/loglevels/loglevel.d.ts +12 -0
  50. package/lib/loglevels/loglevel.d.ts.map +1 -0
  51. package/lib/loglevels/loglevel.js +13 -0
  52. package/lib/loglevels/loglevel.js.map +1 -0
  53. package/lib/outputs/browser.d.ts +2 -0
  54. package/lib/outputs/browser.d.ts.map +1 -0
  55. package/lib/outputs/browser.js +23 -0
  56. package/lib/outputs/browser.js.map +1 -0
  57. package/lib/outputs/node-controllers/github-actions.d.ts +3 -0
  58. package/lib/outputs/node-controllers/github-actions.d.ts.map +1 -0
  59. package/lib/outputs/node-controllers/github-actions.js +15 -0
  60. package/lib/outputs/node-controllers/github-actions.js.map +1 -0
  61. package/lib/outputs/node-controllers/index.d.ts +3 -0
  62. package/lib/outputs/node-controllers/index.d.ts.map +1 -0
  63. package/lib/outputs/node-controllers/index.js +9 -0
  64. package/lib/outputs/node-controllers/index.js.map +1 -0
  65. package/lib/outputs/nodejs.d.ts +26 -0
  66. package/lib/outputs/nodejs.d.ts.map +1 -0
  67. package/lib/outputs/nodejs.js +82 -0
  68. package/lib/outputs/nodejs.js.map +1 -0
  69. package/lib/printers/file.d.ts +2 -1
  70. package/lib/printers/file.d.ts.map +1 -1
  71. package/lib/printers/file.js +2 -1
  72. package/lib/printers/file.js.map +1 -1
  73. package/package.json +16 -7
  74. package/src/common/ansi.ts +6 -1
  75. package/src/common/create.compability.ts +7 -0
  76. package/src/common/create.function.ts +60 -27
  77. package/src/common/create.logger.ts +22 -20
  78. package/src/common/helpers.ts +5 -46
  79. package/src/common/logger.create.ts +13 -15
  80. package/src/common/logger.global.ts +13 -18
  81. package/src/common/registry.ts +42 -0
  82. package/src/common/types.ts +100 -23
  83. package/src/{common/debug.commands.ts → functions/builtin-commands.ts} +3 -13
  84. package/src/loglevels/control.browser.ts +0 -0
  85. package/src/loglevels/control.nodejs.ts +0 -0
  86. package/src/loglevels/loglevel.ts +12 -0
  87. package/src/outputs/browser.ts +27 -0
  88. package/src/outputs/node-controllers/github-actions.ts +18 -0
  89. package/src/outputs/node-controllers/index.ts +10 -0
  90. package/src/outputs/nodejs.ts +102 -0
  91. package/src/printers/file.ts +3 -2
  92. package/lib/common/debug.commands.d.ts.map +0 -1
  93. package/lib/common/debug.commands.js.map +0 -1
  94. package/lib/index.d.ts +0 -10
  95. package/lib/index.d.ts.map +0 -1
  96. package/lib/index.js +0 -10
  97. package/lib/index.js.map +0 -1
  98. package/src/index.ts +0 -27
@@ -1 +1 @@
1
- {"version":3,"file":"file.js","sourceRoot":"","sources":["../../src/printers/file.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,SAAS,CAAC;AAC5C,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACpC,OAAO,EAAE,SAAS,EAA0B,MAAM,aAAa,CAAC;AAEhE,MAAM,UAAU,aAAa,CAAC,QAAgB;IAC7C,MAAM,IAAI,GAAG,OAAO,CAAC,OAAO,CAAC,GAAG,EAAE,EAAE,QAAQ,CAAC,CAAC;IAC9C,OAAO,CAAC,GAAG,CAAC,sBAAsB,IAAI,EAAE,CAAC,CAAC;IAC1C,MAAM,MAAM,GAAG,iBAAiB,CAAC,IAAI,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,QAAQ,EAAE,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC;IAC5F,MAAM,CAAC,EAAE,CAAC,OAAO,EAAE,CAAC,GAAG,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
  "type": "module",
4
- "version": "0.0.21",
4
+ "version": "0.0.23",
5
5
  "description": "A simple logger module for my self use (nodejs).",
6
6
  "exports": {
7
7
  ".": {
8
- "source": "./src/index.ts",
9
- "default": "./lib/index.js"
8
+ "source": "./src/_environments/node.ts",
9
+ "node": "./lib/_environments/node.js",
10
+ "default": "./lib/_environments/browser.js"
11
+ },
12
+ "./node": {
13
+ "default": "./lib/_environments/node.js"
14
+ },
15
+ "./browser": {
16
+ "default": "./lib/_environments/browser.js"
10
17
  },
11
18
  "./package.json": "./package.json"
12
19
  },
13
20
  "sideEffects": false,
14
21
  "dependencies": {
15
22
  "debug": "^4.4.3",
16
- "@idlebox/common": "^1.5.17"
23
+ "@idlebox/terminal-control": "^0.0.3",
24
+ "@idlebox/common": "^1.5.21"
17
25
  },
18
26
  "devDependencies": {
19
27
  "@types/debug": "^4.1.13",
20
28
  "@types/node": "^25.5.0",
21
- "@build-script/baseline-rig": "latest",
22
- "@mpis/run": "^0.0.22"
29
+ "mocha": "^11.7.5",
30
+ "@mpis/run": "^0.0.25",
31
+ "@build-script/baseline-rig": "latest"
23
32
  },
24
33
  "license": "MIT",
25
34
  "author": "GongT <admin@gongt.me>",
@@ -28,7 +37,7 @@
28
37
  "url": "git+https://github.com/GongT/baobao.git",
29
38
  "directory": "@idlebox/logger"
30
39
  },
31
- "main": "./lib/index.js",
40
+ "main": "./lib/_environments/browser.js",
32
41
  "scripts": {
33
42
  "build": "mpis-run build",
34
43
  "watch": "mpis-run watch",
@@ -1,4 +1,4 @@
1
- export const CSI = '\x1b[';
1
+ import { CSI } from '@idlebox/terminal-control/constants';
2
2
 
3
3
  export const Crst = `${CSI}0m`;
4
4
 
@@ -17,3 +17,8 @@ export const CFgreen = `${CSI}38;5;10m`;
17
17
  export const CFyellow = `${CSI}38;5;11m`;
18
18
  export const CFblue = `${CSI}38;5;14m`;
19
19
  export const CFred = `${CSI}38;5;9m`;
20
+
21
+ /**
22
+ * @deprecated
23
+ */
24
+ export { CSI };
@@ -0,0 +1,7 @@
1
+ import type { IAbstractConsole, IMyLogger } from './types.js';
2
+
3
+ type IBondOptions = {};
4
+
5
+ export function consoleCompatibleInternal(_options: IBondOptions, _input: IMyLogger): IAbstractConsole {
6
+ return undefined as any;
7
+ }
@@ -1,55 +1,88 @@
1
1
  import { defineInspectMethod } from '@idlebox/common';
2
+ import assert from 'node:assert';
2
3
  import type { InspectContext } from 'node:util';
4
+ import { call_debug_command, debug_commands, nodeFormat } from '../functions/builtin-commands.js';
3
5
  import { Cdim, Crst, CSI, NCdim } from './ansi.js';
4
6
  import { LogLevel, logLevelPaddingStr, logTagColor } from './colors.js';
5
- import { call_debug_command, debug_commands, nodeFormat } from './debug.commands.js';
6
7
  import { current_error_action, escapeRegExp } from './helpers.js';
7
- import type { IMyDebug, IMyDebugWithControl } from './types.js';
8
+ import type { ILineWriter, IMyDebug, IMyDebugWithControl } from './types.js';
8
9
 
9
10
  interface IDebugOptions {
10
11
  tag: string;
11
12
  level: LogLevel;
12
- color_enabled: boolean;
13
- color_entire_line?: boolean;
14
- stream?: NodeJS.WritableStream;
13
+ colorEnabled: boolean;
14
+ colorWholeLine?: boolean;
15
+ writer: ILineWriter;
15
16
  enabled?: boolean;
16
17
  }
17
- export function createDebug({ tag, level, color_enabled, color_entire_line = false, stream = process.stdout, enabled = true }: IDebugOptions): IMyDebugWithControl {
18
+
19
+ /**
20
+ * 创建一个debug函数
21
+ * 在浏览器中必须指定stream
22
+ */
23
+ export function createDebug({ tag, level, colorEnabled, colorWholeLine = false, writer, enabled = true }: IDebugOptions): IMyDebugWithControl {
18
24
  const color = logTagColor[level];
19
25
  const lineOpt = {
20
26
  tag: tag ? tag : `${LogLevel[level][0].toUpperCase()}`,
21
- stream,
27
+ writer,
22
28
  color,
23
29
  level,
24
30
  };
25
31
 
26
32
  let write_line: IMyDebug;
27
- if (!color_enabled) {
33
+ if (!colorEnabled) {
28
34
  write_line = write_line_monolithic({
29
35
  ...lineOpt,
30
36
  tag: tag ? tag : `$$`,
31
37
  });
32
- } else if (color_entire_line) {
38
+ } else if (colorWholeLine) {
33
39
  write_line = write_line_colored_line(lineOpt);
34
40
  } else {
35
41
  write_line = write_line_colored_tag(lineOpt);
36
42
  }
37
43
 
38
- const r = Object.assign(
44
+ assert.equal(typeof writer, 'function', 'writer must be a function');
45
+
46
+ const r = Object.defineProperties(
39
47
  (m: any, ...args: unknown[]) => {
40
- if (!r.isEnabled) return;
48
+ if (!enabled) return;
41
49
  write_line(m, ...args);
42
50
  },
43
51
  {
44
- enable: () => {
45
- r.isEnabled = true;
52
+ displayName: {
53
+ get() {
54
+ return `writeLine:${LogLevel[level]}|${enabled ? 'enabled' : 'disabled'}`;
55
+ },
56
+ configurable: false,
57
+ },
58
+ enable: {
59
+ value: function enable() {
60
+ enabled = true;
61
+ },
62
+ configurable: false,
63
+ writable: false,
64
+ },
65
+ disable: {
66
+ value: function disable() {
67
+ enabled = false;
68
+ },
69
+ configurable: false,
70
+ writable: false,
71
+ },
72
+ isEnabled: {
73
+ get() {
74
+ return enabled;
75
+ },
76
+ configurable: false,
46
77
  },
47
- disable: () => {
48
- r.isEnabled = false;
78
+ writeLine: {
79
+ value: writer,
80
+ enumerable: false,
81
+ configurable: false,
82
+ writable: false,
49
83
  },
50
- isEnabled: enabled,
51
84
  },
52
- );
85
+ ) as IMyDebugWithControl;
53
86
 
54
87
  return defineInspectMethod(r, (_depth: number, context: InspectContext) => {
55
88
  return `[${context.stylize('Debug', 'special')} "${context.stylize(tag, 'string')}" ${context.stylize(LogLevel[level], 'undefined')} ${context.stylize(r.isEnabled ? 'enabled' : 'disabled', 'boolean')}]`;
@@ -58,7 +91,7 @@ export function createDebug({ tag, level, color_enabled, color_entire_line = fal
58
91
 
59
92
  interface IWriteLineOptions {
60
93
  tag: string;
61
- stream: NodeJS.WritableStream;
94
+ writer: ILineWriter;
62
95
  color: string;
63
96
  level: LogLevel;
64
97
  }
@@ -118,7 +151,7 @@ function format_template(messages: TemplateStringsArray, args: unknown[], color:
118
151
  /**
119
152
  * TAG带颜色
120
153
  */
121
- function write_line_colored_tag({ tag, stream, color }: IWriteLineOptions) {
154
+ function write_line_colored_tag({ tag, writer, color }: IWriteLineOptions) {
122
155
  return (messages: TemplateStringsArray | string, ...args: unknown[]) => {
123
156
  const head = `[${CSI}${color}m${tag}${Crst}]`;
124
157
  let body: string;
@@ -128,14 +161,14 @@ function write_line_colored_tag({ tag, stream, color }: IWriteLineOptions) {
128
161
  body = format_template(messages, args, true);
129
162
  }
130
163
 
131
- write(stream, head, body);
164
+ write(writer, head, body);
132
165
  };
133
166
  }
134
167
 
135
168
  /**
136
169
  * 整行带颜色
137
170
  */
138
- function write_line_colored_line({ tag, stream, color }: IWriteLineOptions) {
171
+ function write_line_colored_line({ tag, writer, color }: IWriteLineOptions) {
139
172
  return (messages: TemplateStringsArray | string, ...args: unknown[]) => {
140
173
  const head = `${CSI}${color}m[${tag}]`;
141
174
  let body: string;
@@ -146,14 +179,14 @@ function write_line_colored_line({ tag, stream, color }: IWriteLineOptions) {
146
179
  }
147
180
  body += Crst;
148
181
 
149
- write(stream, head, body);
182
+ write(writer, head, body);
150
183
  };
151
184
  }
152
185
 
153
186
  /**
154
187
  * 不带颜色
155
188
  */
156
- function write_line_monolithic({ tag, level, stream }: IWriteLineOptions) {
189
+ function write_line_monolithic({ tag, level, writer }: IWriteLineOptions) {
157
190
  const lvlStr = logLevelPaddingStr[level];
158
191
  const head = `[${tag}/${lvlStr}]`;
159
192
 
@@ -166,14 +199,14 @@ function write_line_monolithic({ tag, level, stream }: IWriteLineOptions) {
166
199
  body = format_template(messages, args, false);
167
200
  }
168
201
 
169
- write(stream, head, body);
202
+ write(writer, head, body);
170
203
  };
171
204
  }
172
205
 
173
- function write(stream: NodeJS.WritableStream, head: string, body: string) {
206
+ function write(writer: ILineWriter, head: string, body: string) {
174
207
  if (body[0] === '[') {
175
- stream.write(`${head}${body}\n`);
208
+ writer(`${head}${body}`);
176
209
  } else {
177
- stream.write(`${head} ${body}\n`);
210
+ writer(`${head} ${body}`);
178
211
  }
179
212
  }
@@ -1,28 +1,28 @@
1
1
  import { defineInspectMethod, SoftwareDefectError } from '@idlebox/common';
2
2
  import type { InspectContext } from 'node:util';
3
+ import { EnableLogLevel } from '../loglevels/loglevel.js';
3
4
  import { LogLevel } from './colors.js';
4
5
  import { createDebug } from './create.function.js';
5
- import { defaultLogLevel, detectColorEnable } from './helpers.js';
6
- import { EnableLogLevel, type IMyDebugWithControl, type IMyLogger } from './types.js';
6
+ import { defaultLogLevel } from './helpers.js';
7
+ import { loggersRegistry, loggersRegistrySet } from './registry.js';
8
+ import type { IInstrestedConsole, IMyDebugWithControl, IMyLogger } from './types.js';
7
9
 
8
- const seen_loggers = new Set<string>();
9
- export const all_logger_names: ReadonlySet<string> = seen_loggers;
10
+ export function create(console: IInstrestedConsole, tag: string, defaultLevel = EnableLogLevel.auto, color_enabled: boolean = true): IMyLogger {
11
+ const exists = loggersRegistry?.get(tag);
12
+ if (exists) return exists;
10
13
 
11
- export function create(tag: string, color_enabled: undefined | boolean, stream: NodeJS.ReadWriteStream, defaultLevel = EnableLogLevel.auto): IMyLogger {
14
+ const colorEnabled = color_enabled && console.colorEnabled;
12
15
  let currentLevel = defaultLevel;
13
- seen_loggers.add(tag);
14
16
 
15
- if (color_enabled === undefined) color_enabled = detectColorEnable(stream);
17
+ const log_fatal = createDebug({ tag, colorEnabled, level: LogLevel.fatal, writer: console.error, colorWholeLine: true });
16
18
 
17
- const log_fatal = createDebug({ tag, color_enabled, level: LogLevel.fatal, stream, color_entire_line: true });
18
-
19
- const error = createDebug({ tag, color_enabled, level: LogLevel.error, stream });
20
- const success = createDebug({ tag, color_enabled, level: LogLevel.success, stream });
21
- const warn = createDebug({ tag, color_enabled, level: LogLevel.warn, stream });
22
- const info = createDebug({ tag, color_enabled, level: LogLevel.info, stream });
23
- const log = createDebug({ tag, color_enabled, level: LogLevel.log, stream });
24
- const debug = createDebug({ tag, color_enabled, level: LogLevel.debug, stream, color_entire_line: true });
25
- const verbose = createDebug({ tag, color_enabled, level: LogLevel.verbose, stream, color_entire_line: true });
19
+ const error = createDebug({ tag, colorEnabled, level: LogLevel.error, writer: console.error });
20
+ const success = createDebug({ tag, colorEnabled, level: LogLevel.success, writer: console.log });
21
+ const warn = createDebug({ tag, colorEnabled, level: LogLevel.warn, writer: console.warn });
22
+ const info = createDebug({ tag, colorEnabled, level: LogLevel.info, writer: console.info });
23
+ const log = createDebug({ tag, colorEnabled, level: LogLevel.log, writer: console.log });
24
+ const debug = createDebug({ tag, colorEnabled, level: LogLevel.debug, writer: console.debug, colorWholeLine: true });
25
+ const verbose = createDebug({ tag, colorEnabled, level: LogLevel.verbose, writer: console.debug, colorWholeLine: true });
26
26
 
27
27
  syncEnabled({ error, warn, info, log, success, debug, verbose }, tag, currentLevel);
28
28
 
@@ -33,7 +33,7 @@ export function create(tag: string, color_enabled: undefined | boolean, stream:
33
33
 
34
34
  const result = {
35
35
  tag,
36
- stream,
36
+ console,
37
37
  fatal,
38
38
  error,
39
39
  warn,
@@ -43,7 +43,7 @@ export function create(tag: string, color_enabled: undefined | boolean, stream:
43
43
  debug,
44
44
  verbose,
45
45
 
46
- colorEnabled: color_enabled,
46
+ colorEnabled: colorEnabled,
47
47
 
48
48
  enable(newLevel: EnableLogLevel) {
49
49
  currentLevel = newLevel;
@@ -52,13 +52,15 @@ export function create(tag: string, color_enabled: undefined | boolean, stream:
52
52
 
53
53
  extend: (newTag: string) => {
54
54
  if (tag) {
55
- return create(`${tag}:${newTag}`, color_enabled, stream, currentLevel);
55
+ return create(console, `${tag}:${newTag}`, currentLevel, colorEnabled);
56
56
  } else {
57
- return create(newTag, color_enabled, stream, currentLevel);
57
+ return create(console, newTag, currentLevel, colorEnabled);
58
58
  }
59
59
  },
60
60
  };
61
61
 
62
+ loggersRegistrySet(result);
63
+
62
64
  return defineInspectMethod(result, (_depth: number, context: InspectContext) => {
63
65
  if (_depth < 0) return `${context.stylize(`[Logger ${tag}]`, 'special')}`;
64
66
  return `${context.stylize('Logger', 'name')} { "${context.stylize(tag, 'string')}" ${context.stylize(EnableLogLevel[currentLevel], 'undefined')} }`;
@@ -1,9 +1,9 @@
1
1
  import { prettyFormatError } from '@idlebox/common';
2
2
  import debug from 'debug';
3
- import process from 'node:process';
4
- import { debug_commands } from './debug.commands.js';
5
- import { terminal } from './logger.global.js';
6
- import { EnableLogLevel, type IDebugCommand } from './types.js';
3
+ import { debug_commands } from '../functions/builtin-commands.js';
4
+ import { EnableLogLevel } from '../loglevels/loglevel.js';
5
+ import { globalLogger } from './logger.global.js';
6
+ import type { IDebugCommand } from './types.js';
7
7
 
8
8
  /**
9
9
  * 判断 字符串是否为“真值”
@@ -24,52 +24,11 @@ export function is_string_truthy(value: string | undefined) {
24
24
  } else if (value === '0' || value === 'false' || value === 'off' || value === 'no' || value === 'disabled') {
25
25
  return false;
26
26
  } else {
27
- terminal.warn`invalid boolean string: ${value}, assuming false.`;
27
+ globalLogger.warn`invalid boolean string: ${value}, assuming false.`;
28
28
  return false;
29
29
  }
30
30
  }
31
31
 
32
- interface IWritableStream extends NodeJS.WritableStream {
33
- colorEnabled?: boolean;
34
- isTTY?: boolean;
35
- }
36
-
37
- function _colorEnabled(stream: IWritableStream): boolean {
38
- const colorArg = process.argv.find((e) => e.startsWith('--color=') || e === '--color' || e === '-c');
39
- const noColorArg = process.argv.includes('--no-color');
40
-
41
- // 命令行顶级优先
42
- if (colorArg) return true;
43
- if (noColorArg) return false;
44
-
45
- // 环境变量
46
- if (process.env.NO_COLOR || process.env.NODE_DISABLE_COLORS === '1') {
47
- /**
48
- * https://force-color.org/
49
- * https://nodejs.org/docs/latest/api/cli.html#node_disable_colors1
50
- */
51
- return false;
52
- } else if (process.env.FORCE_COLOR) {
53
- return true;
54
- }
55
-
56
- // 检测其他可能
57
- if (stream.isTTY) {
58
- return true;
59
- }
60
-
61
- // TODO
62
-
63
- return false;
64
- }
65
-
66
- export function detectColorEnable(stream: IWritableStream = process.stderr): boolean {
67
- if (stream.colorEnabled === undefined) {
68
- stream.colorEnabled = _colorEnabled(stream);
69
- }
70
- return stream.colorEnabled;
71
- }
72
-
73
32
  export function escapeRegExp(str: string) {
74
33
  return str.replace(/[-[\]/{}()*+?.\\^$|]/g, '\\$&');
75
34
  }
@@ -1,31 +1,29 @@
1
- import process from 'node:process';
2
- import { PassThrough } from 'node:stream';
1
+ import { EnableLogLevel } from '../loglevels/loglevel.js';
3
2
  import { create } from './create.logger.js';
4
3
  import { debug_enabled, defaultLogLevel } from './helpers.js';
5
- import { terminal } from './logger.global.js';
6
- import { EnableLogLevel, type IMyLogger } from './types.js';
4
+ import { globalLogger } from './logger.global.js';
5
+ import type { IInstrestedConsole, IMyLogger } from './types.js';
6
+
7
+ export interface ILoggerOptionsReq {
8
+ readonly colors?: boolean;
9
+ readonly console: IInstrestedConsole;
10
+ }
7
11
 
8
12
  /**
9
13
  * 创建一个新的logger实例
10
- * @param color_enabled 默认自动
11
- * @param pipeTo 默认是 process.stderr
14
+ * @param tag logger的tag,类似 "app:db:insert"
15
+ * @param colors 是否启用颜色,默认启用
12
16
  * @returns
13
17
  */
14
- export function createLogger(tag: string, color_enabled: boolean | undefined = undefined, pipeTo: undefined | NodeJS.WritableStream = process.stderr): IMyLogger {
15
- const stream = new PassThrough();
16
- if (pipeTo) {
17
- Object.assign(stream, { isTTY: (pipeTo as any).isTTY });
18
- stream.pipe(pipeTo);
19
- }
20
-
18
+ export function createLoggerObject(tag: string, { colors = true, console }: ILoggerOptionsReq): IMyLogger {
21
19
  let level = EnableLogLevel.error;
22
20
  if (debug_enabled(tag)) {
23
21
  level = defaultLogLevel;
24
22
  }
25
23
 
26
- const logger = create(tag, color_enabled, stream, level);
24
+ const logger = create(console, tag, level, colors);
27
25
 
28
- (terminal || logger).verbose`logger "${tag}" created, level = ${EnableLogLevel[level]}`;
26
+ (globalLogger || logger).verbose`logger "${tag}" created, level = ${EnableLogLevel[level]}`;
29
27
 
30
28
  return logger;
31
29
  }
@@ -1,39 +1,34 @@
1
1
  import { globalObject } from '@idlebox/common';
2
- import process from 'node:process';
3
- import { PassThrough } from 'node:stream';
2
+ import type { EnableLogLevel } from '../loglevels/loglevel.js';
4
3
  import { create } from './create.logger.js';
5
- import { EnableLogLevel, type IMyLogger } from './types.js';
6
-
7
- const stream = new PassThrough();
8
- stream.pipe(process.stderr);
9
- Object.assign(stream, { isTTY: process.stderr.isTTY });
4
+ import type { IInstrestedConsole, IMyLogger } from './types.js';
10
5
 
11
6
  const symbol = Symbol.for('@idlebox/logger/global/terminal');
12
7
 
13
8
  /**
14
9
  * 作为logger导出,必须在程序入口调用过 createGlobalLogger() 才能使用
15
10
  */
16
- export let terminal: IMyLogger;
11
+ export let globalLogger: IMyLogger;
17
12
 
18
13
  /**
19
14
  * 创建root-logger,随后logger变量可用
20
15
  */
21
- export function createGlobalLogger(tag: string, defaultLevel: EnableLogLevel = EnableLogLevel.auto): void {
22
- terminal = globalObject[symbol];
23
- if (terminal) {
24
- terminal.error`global logger already created`;
16
+ export function createGlobalLogger(console: IInstrestedConsole, tag: string, defaultLevel: EnableLogLevel): void {
17
+ globalLogger = globalObject[symbol];
18
+ if (globalLogger) {
19
+ globalLogger.error`global logger already created`;
25
20
  return;
26
21
  }
27
22
 
28
- terminal = create(tag, undefined, stream);
29
- globalObject[symbol] = terminal;
23
+ globalLogger = create(console, tag);
24
+ globalObject[symbol] = globalLogger;
30
25
 
31
- terminal.enable(defaultLevel);
26
+ globalLogger.enable(defaultLevel);
32
27
 
33
- if (terminal.verbose.isEnabled) {
34
- terminal.verbose`verbose is enabled`;
28
+ if (globalLogger.verbose.isEnabled) {
29
+ globalLogger.verbose`verbose is enabled`;
35
30
  } else {
36
- terminal.debug`debug is enabled`;
31
+ globalLogger.debug`debug is enabled`;
37
32
  }
38
33
  return;
39
34
  }
@@ -0,0 +1,42 @@
1
+ import { isProductionMode, SoftwareDefectError } from '@idlebox/common';
2
+ import type { IMyLogger } from './types.js';
3
+
4
+ export let loggersRegistry: Map<string, IMyLogger> | undefined;
5
+ let somethingCreated = false;
6
+
7
+ export function loggersRegistrySet(logger: IMyLogger) {
8
+ if (!loggersRegistry) {
9
+ if (!isProductionMode) somethingCreated = true;
10
+ return;
11
+ }
12
+ loggersRegistry.set(logger.tag, logger);
13
+ }
14
+
15
+ export function getAllLoggers(): readonly IMyLogger[] {
16
+ return Array.from(requiresEnabled().values());
17
+ }
18
+
19
+ export function enableLoggerRegistry() {
20
+ if (loggersRegistry !== undefined) return;
21
+
22
+ loggersRegistry = new Map();
23
+
24
+ if (!isProductionMode) {
25
+ if (somethingCreated) console.warn('检测到在启用日志注册表之前已经创建了logger实例,之前创建的实例无法通过注册表访问');
26
+ }
27
+ }
28
+
29
+ export function deleteLoggerInstance(tag: string) {
30
+ requiresEnabled().delete(tag);
31
+ }
32
+
33
+ export function getAllLoggerNames(): readonly string[] {
34
+ return Array.from(requiresEnabled().keys());
35
+ }
36
+
37
+ function requiresEnabled() {
38
+ if (!loggersRegistry) {
39
+ throw new SoftwareDefectError('logger registry is not enabled');
40
+ }
41
+ return loggersRegistry;
42
+ }