@naturalcycles/js-lib 15.31.0 → 15.32.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.
@@ -3,13 +3,17 @@ import type { MutateOptions } from '../array/array.util.js';
3
3
  * These levels follow console.* naming,
4
4
  * so you can use console[level] safely.
5
5
  *
6
- * `log` is considered default level.
6
+ * `debug` is not enabled by default, and is useful when debugging is needed.
7
7
  *
8
- * For simplicity - only these 3 levels are kept.
8
+ * `log` is considered default level, and is enabled by default.
9
+ *
10
+ * `warn` is for warnings - things that are not super-severe to be an error, but should not happen
11
+ *
12
+ * `error` level would only log errors
9
13
  *
10
14
  * @experimental
11
15
  */
12
- export type CommonLogLevel = 'log' | 'warn' | 'error';
16
+ export type CommonLogLevel = 'debug' | 'log' | 'warn' | 'error';
13
17
  export declare const commonLogLevelNumber: Record<CommonLogLevel, number>;
14
18
  /**
15
19
  * Function that takes any number of arguments and logs them all.
@@ -26,20 +30,19 @@ export type CommonLogWithLevelFunction = (level: CommonLogLevel, args: any[]) =>
26
30
  * @experimental
27
31
  */
28
32
  export interface CommonLogger {
33
+ debug: CommonLogFunction;
29
34
  log: CommonLogFunction;
30
35
  warn: CommonLogFunction;
31
36
  error: CommonLogFunction;
32
37
  }
33
38
  /**
34
39
  * SimpleLogger that does nothing (noop).
35
- *
36
- * @experimental
37
40
  */
38
41
  export declare const commonLoggerNoop: CommonLogger;
39
42
  /**
40
43
  * Creates a "child" logger that is "limited" to the specified CommonLogLevel.
41
44
  */
42
- export declare function commonLoggerMinLevel(logger: CommonLogger, minLevel: CommonLogLevel, opt?: MutateOptions): CommonLogger;
45
+ export declare function createCommonLoggerAtLevel(logger?: CommonLogger, minLevel?: CommonLogLevel, opt?: MutateOptions): CommonLogger;
43
46
  /**
44
47
  * Creates a "proxy" CommonLogger that pipes log messages to all provided sub-loggers.
45
48
  */
@@ -1,15 +1,16 @@
1
+ // copy-pasted to avoid weird circular dependency
1
2
  const _noop = (..._args) => undefined;
2
3
  export const commonLogLevelNumber = {
3
- log: 10,
4
+ debug: 10,
5
+ log: 20,
4
6
  warn: 20,
5
7
  error: 30,
6
8
  };
7
9
  /**
8
10
  * SimpleLogger that does nothing (noop).
9
- *
10
- * @experimental
11
11
  */
12
12
  export const commonLoggerNoop = {
13
+ debug: _noop,
13
14
  log: _noop,
14
15
  warn: _noop,
15
16
  error: _noop,
@@ -17,21 +18,24 @@ export const commonLoggerNoop = {
17
18
  /**
18
19
  * Creates a "child" logger that is "limited" to the specified CommonLogLevel.
19
20
  */
20
- export function commonLoggerMinLevel(logger, minLevel, opt = {}) {
21
+ export function createCommonLoggerAtLevel(logger = console, minLevel = 'log', opt = {}) {
21
22
  const level = commonLogLevelNumber[minLevel];
22
23
  if (opt.mutate) {
23
- if (level > commonLogLevelNumber['log']) {
24
- logger.log = _noop;
25
- if (level > commonLogLevelNumber['warn']) {
26
- logger.warn = _noop;
27
- if (level > commonLogLevelNumber['error']) {
28
- logger.error = _noop;
24
+ if (level > commonLogLevelNumber['debug']) {
25
+ logger.debug = _noop;
26
+ if (level > commonLogLevelNumber['log']) {
27
+ logger.log = _noop;
28
+ if (level > commonLogLevelNumber['warn']) {
29
+ logger.warn = _noop;
30
+ if (level > commonLogLevelNumber['error']) {
31
+ logger.error = _noop;
32
+ }
29
33
  }
30
34
  }
31
35
  }
32
36
  return logger;
33
37
  }
34
- if (level <= commonLogLevelNumber['log']) {
38
+ if (level <= commonLogLevelNumber['debug']) {
35
39
  // All levels are kept
36
40
  return logger;
37
41
  }
@@ -40,7 +44,8 @@ export function commonLoggerMinLevel(logger, minLevel, opt = {}) {
40
44
  return commonLoggerNoop;
41
45
  }
42
46
  return {
43
- log: _noop, // otherwise it is "log everything" logger (same logger as input)
47
+ debug: _noop, // otherwise it is "log everything" logger (same logger as input)
48
+ log: level <= commonLogLevelNumber['log'] ? logger.log.bind(logger) : _noop,
44
49
  warn: level <= commonLogLevelNumber['warn'] ? logger.warn.bind(logger) : _noop,
45
50
  error: logger.error.bind(logger), // otherwise it's "log nothing" logger (same as noopLogger)
46
51
  };
@@ -50,6 +55,7 @@ export function commonLoggerMinLevel(logger, minLevel, opt = {}) {
50
55
  */
51
56
  export function commonLoggerPipe(loggers) {
52
57
  return {
58
+ debug: (...args) => loggers.forEach(logger => logger.debug(...args)),
53
59
  log: (...args) => loggers.forEach(logger => logger.log(...args)),
54
60
  warn: (...args) => loggers.forEach(logger => logger.warn(...args)),
55
61
  error: (...args) => loggers.forEach(logger => logger.error(...args)),
@@ -60,6 +66,7 @@ export function commonLoggerPipe(loggers) {
60
66
  */
61
67
  export function commonLoggerPrefix(logger, ...prefixes) {
62
68
  return {
69
+ debug: (...args) => logger.debug(...prefixes, ...args),
63
70
  log: (...args) => logger.log(...prefixes, ...args),
64
71
  warn: (...args) => logger.warn(...prefixes, ...args),
65
72
  error: (...args) => logger.error(...prefixes, ...args),
@@ -70,6 +77,7 @@ export function commonLoggerPrefix(logger, ...prefixes) {
70
77
  */
71
78
  export function commonLoggerCreate(fn) {
72
79
  return {
80
+ debug: (...args) => fn('debug', args),
73
81
  log: (...args) => fn('log', args),
74
82
  warn: (...args) => fn('warn', args),
75
83
  error: (...args) => fn('error', args),
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@naturalcycles/js-lib",
3
3
  "type": "module",
4
- "version": "15.31.0",
4
+ "version": "15.32.0",
5
5
  "dependencies": {
6
6
  "tslib": "^2",
7
7
  "undici": "^7",
@@ -1,22 +1,27 @@
1
- // copy-pasted to avoid weird circular dependency
2
1
  import type { MutateOptions } from '../array/array.util.js'
3
2
 
3
+ // copy-pasted to avoid weird circular dependency
4
4
  const _noop = (..._args: any[]): undefined => undefined
5
5
 
6
6
  /**
7
7
  * These levels follow console.* naming,
8
8
  * so you can use console[level] safely.
9
9
  *
10
- * `log` is considered default level.
10
+ * `debug` is not enabled by default, and is useful when debugging is needed.
11
+ *
12
+ * `log` is considered default level, and is enabled by default.
11
13
  *
12
- * For simplicity - only these 3 levels are kept.
14
+ * `warn` is for warnings - things that are not super-severe to be an error, but should not happen
15
+ *
16
+ * `error` level would only log errors
13
17
  *
14
18
  * @experimental
15
19
  */
16
- export type CommonLogLevel = 'log' | 'warn' | 'error'
20
+ export type CommonLogLevel = 'debug' | 'log' | 'warn' | 'error'
17
21
 
18
22
  export const commonLogLevelNumber: Record<CommonLogLevel, number> = {
19
- log: 10,
23
+ debug: 10,
24
+ log: 20,
20
25
  warn: 20,
21
26
  error: 30,
22
27
  }
@@ -37,6 +42,7 @@ export type CommonLogWithLevelFunction = (level: CommonLogLevel, args: any[]) =>
37
42
  * @experimental
38
43
  */
39
44
  export interface CommonLogger {
45
+ debug: CommonLogFunction
40
46
  log: CommonLogFunction
41
47
  warn: CommonLogFunction
42
48
  error: CommonLogFunction
@@ -44,10 +50,9 @@ export interface CommonLogger {
44
50
 
45
51
  /**
46
52
  * SimpleLogger that does nothing (noop).
47
- *
48
- * @experimental
49
53
  */
50
54
  export const commonLoggerNoop: CommonLogger = {
55
+ debug: _noop,
51
56
  log: _noop,
52
57
  warn: _noop,
53
58
  error: _noop,
@@ -56,26 +61,29 @@ export const commonLoggerNoop: CommonLogger = {
56
61
  /**
57
62
  * Creates a "child" logger that is "limited" to the specified CommonLogLevel.
58
63
  */
59
- export function commonLoggerMinLevel(
60
- logger: CommonLogger,
61
- minLevel: CommonLogLevel,
64
+ export function createCommonLoggerAtLevel(
65
+ logger: CommonLogger = console,
66
+ minLevel: CommonLogLevel = 'log',
62
67
  opt: MutateOptions = {},
63
68
  ): CommonLogger {
64
69
  const level = commonLogLevelNumber[minLevel]
65
70
  if (opt.mutate) {
66
- if (level > commonLogLevelNumber['log']) {
67
- logger.log = _noop
68
- if (level > commonLogLevelNumber['warn']) {
69
- logger.warn = _noop
70
- if (level > commonLogLevelNumber['error']) {
71
- logger.error = _noop
71
+ if (level > commonLogLevelNumber['debug']) {
72
+ logger.debug = _noop
73
+ if (level > commonLogLevelNumber['log']) {
74
+ logger.log = _noop
75
+ if (level > commonLogLevelNumber['warn']) {
76
+ logger.warn = _noop
77
+ if (level > commonLogLevelNumber['error']) {
78
+ logger.error = _noop
79
+ }
72
80
  }
73
81
  }
74
82
  }
75
83
  return logger
76
84
  }
77
85
 
78
- if (level <= commonLogLevelNumber['log']) {
86
+ if (level <= commonLogLevelNumber['debug']) {
79
87
  // All levels are kept
80
88
  return logger
81
89
  }
@@ -86,7 +94,8 @@ export function commonLoggerMinLevel(
86
94
  }
87
95
 
88
96
  return {
89
- log: _noop, // otherwise it is "log everything" logger (same logger as input)
97
+ debug: _noop, // otherwise it is "log everything" logger (same logger as input)
98
+ log: level <= commonLogLevelNumber['log'] ? logger.log.bind(logger) : _noop,
90
99
  warn: level <= commonLogLevelNumber['warn'] ? logger.warn.bind(logger) : _noop,
91
100
  error: logger.error.bind(logger), // otherwise it's "log nothing" logger (same as noopLogger)
92
101
  }
@@ -97,6 +106,7 @@ export function commonLoggerMinLevel(
97
106
  */
98
107
  export function commonLoggerPipe(loggers: CommonLogger[]): CommonLogger {
99
108
  return {
109
+ debug: (...args) => loggers.forEach(logger => logger.debug(...args)),
100
110
  log: (...args) => loggers.forEach(logger => logger.log(...args)),
101
111
  warn: (...args) => loggers.forEach(logger => logger.warn(...args)),
102
112
  error: (...args) => loggers.forEach(logger => logger.error(...args)),
@@ -108,6 +118,7 @@ export function commonLoggerPipe(loggers: CommonLogger[]): CommonLogger {
108
118
  */
109
119
  export function commonLoggerPrefix(logger: CommonLogger, ...prefixes: any[]): CommonLogger {
110
120
  return {
121
+ debug: (...args) => logger.debug(...prefixes, ...args),
111
122
  log: (...args) => logger.log(...prefixes, ...args),
112
123
  warn: (...args) => logger.warn(...prefixes, ...args),
113
124
  error: (...args) => logger.error(...prefixes, ...args),
@@ -119,6 +130,7 @@ export function commonLoggerPrefix(logger: CommonLogger, ...prefixes: any[]): Co
119
130
  */
120
131
  export function commonLoggerCreate(fn: CommonLogWithLevelFunction): CommonLogger {
121
132
  return {
133
+ debug: (...args) => fn('debug', args),
122
134
  log: (...args) => fn('log', args),
123
135
  warn: (...args) => fn('warn', args),
124
136
  error: (...args) => fn('error', args),