standard-log 11.5.1 → 12.0.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.
Files changed (157) hide show
  1. package/README.md +2 -3
  2. package/cjs/console.d.ts +3 -3
  3. package/cjs/console.d.ts.map +1 -1
  4. package/cjs/console.js +18 -9
  5. package/cjs/console.js.map +1 -1
  6. package/cjs/errors.d.ts.map +1 -1
  7. package/cjs/errors.js.map +1 -1
  8. package/cjs/formatter.d.ts +1 -1
  9. package/cjs/formatter.d.ts.map +1 -1
  10. package/cjs/formatter.js +2 -2
  11. package/cjs/formatter.js.map +1 -1
  12. package/cjs/index.d.ts +4 -4
  13. package/cjs/index.d.ts.map +1 -1
  14. package/cjs/index.js +7 -8
  15. package/cjs/index.js.map +1 -1
  16. package/cjs/{logLevels.d.ts → log_levels.d.ts} +1 -1
  17. package/cjs/log_levels.d.ts.map +1 -0
  18. package/cjs/{logLevels.js → log_levels.js} +1 -1
  19. package/cjs/log_levels.js.map +1 -0
  20. package/{esm/logStore.d.ts → cjs/log_store.d.ts} +1 -1
  21. package/cjs/log_store.d.ts.map +1 -0
  22. package/cjs/{logStore.js → log_store.js} +6 -6
  23. package/cjs/log_store.js.map +1 -0
  24. package/cjs/logger.d.ts +1 -1
  25. package/cjs/logger.d.ts.map +1 -1
  26. package/cjs/logger.js +16 -14
  27. package/cjs/logger.js.map +1 -1
  28. package/cjs/memory.d.ts +2 -2
  29. package/cjs/memory.d.ts.map +1 -1
  30. package/cjs/memory.js +15 -8
  31. package/cjs/memory.js.map +1 -1
  32. package/cjs/platform.d.ts.map +1 -1
  33. package/cjs/platform.js +1 -2
  34. package/cjs/platform.js.map +1 -1
  35. package/cjs/{standardLog.ctx.d.ts → standard_log.ctx.d.ts} +2 -2
  36. package/cjs/standard_log.ctx.d.ts.map +1 -0
  37. package/cjs/{standardLog.ctx.js → standard_log.ctx.js} +1 -1
  38. package/cjs/standard_log.ctx.js.map +1 -0
  39. package/{esm/standardLog.d.ts → cjs/standard_log.d.ts} +4 -9
  40. package/cjs/standard_log.d.ts.map +1 -0
  41. package/cjs/{standardLog.js → standard_log.js} +37 -40
  42. package/cjs/standard_log.js.map +1 -0
  43. package/cjs/standard_log.mocks.d.ts +8 -0
  44. package/cjs/standard_log.mocks.d.ts.map +1 -0
  45. package/cjs/standard_log.mocks.js +32 -0
  46. package/cjs/standard_log.mocks.js.map +1 -0
  47. package/cjs/test_utils.d.ts +10 -0
  48. package/cjs/test_utils.d.ts.map +1 -0
  49. package/cjs/test_utils.js +25 -0
  50. package/cjs/test_utils.js.map +1 -0
  51. package/cjs/testing/index.d.ts +2 -0
  52. package/cjs/testing/index.d.ts.map +1 -0
  53. package/cjs/testing/index.js +6 -0
  54. package/cjs/testing/index.js.map +1 -0
  55. package/cjs/types.d.ts +12 -12
  56. package/cjs/types.d.ts.map +1 -1
  57. package/cjs/types.js.map +1 -1
  58. package/dist/standard-log.es5.js +1139 -758
  59. package/dist/standard-log.es5.js.gz +0 -0
  60. package/dist/standard-log.es5.js.map +1 -1
  61. package/dist/standard-log.es5.js.map.gz +0 -0
  62. package/dist/standard-log.esm.js +220 -175
  63. package/dist/standard-log.esm.js.gz +0 -0
  64. package/dist/standard-log.esm.js.map +1 -1
  65. package/dist/standard-log.esm.js.map.gz +0 -0
  66. package/esm/console.d.ts +3 -3
  67. package/esm/console.d.ts.map +1 -1
  68. package/esm/console.js +21 -10
  69. package/esm/console.js.map +1 -1
  70. package/esm/errors.d.ts.map +1 -1
  71. package/esm/errors.js.map +1 -1
  72. package/esm/formatter.d.ts +1 -1
  73. package/esm/formatter.d.ts.map +1 -1
  74. package/esm/formatter.js +1 -1
  75. package/esm/formatter.js.map +1 -1
  76. package/esm/index.d.ts +4 -4
  77. package/esm/index.d.ts.map +1 -1
  78. package/esm/index.js +2 -2
  79. package/esm/index.js.map +1 -1
  80. package/esm/{logLevels.d.ts → log_levels.d.ts} +1 -1
  81. package/esm/log_levels.d.ts.map +1 -0
  82. package/esm/{logLevels.js → log_levels.js} +1 -1
  83. package/esm/log_levels.js.map +1 -0
  84. package/{cjs/logStore.d.ts → esm/log_store.d.ts} +1 -1
  85. package/esm/log_store.d.ts.map +1 -0
  86. package/esm/{logStore.js → log_store.js} +2 -2
  87. package/esm/log_store.js.map +1 -0
  88. package/esm/logger.d.ts +1 -1
  89. package/esm/logger.d.ts.map +1 -1
  90. package/esm/logger.js +2 -2
  91. package/esm/logger.js.map +1 -1
  92. package/esm/memory.d.ts +2 -2
  93. package/esm/memory.d.ts.map +1 -1
  94. package/esm/memory.js +15 -8
  95. package/esm/memory.js.map +1 -1
  96. package/esm/platform.d.ts.map +1 -1
  97. package/esm/platform.js +1 -2
  98. package/esm/platform.js.map +1 -1
  99. package/esm/{standardLog.ctx.d.ts → standard_log.ctx.d.ts} +2 -2
  100. package/esm/standard_log.ctx.d.ts.map +1 -0
  101. package/esm/{standardLog.ctx.js → standard_log.ctx.js} +1 -1
  102. package/esm/standard_log.ctx.js.map +1 -0
  103. package/{cjs/standardLog.d.ts → esm/standard_log.d.ts} +4 -9
  104. package/esm/standard_log.d.ts.map +1 -0
  105. package/esm/{standardLog.js → standard_log.js} +27 -28
  106. package/esm/standard_log.js.map +1 -0
  107. package/esm/standard_log.mocks.d.ts +8 -0
  108. package/esm/standard_log.mocks.d.ts.map +1 -0
  109. package/esm/standard_log.mocks.js +16 -0
  110. package/esm/standard_log.mocks.js.map +1 -0
  111. package/esm/test_utils.d.ts +10 -0
  112. package/esm/test_utils.d.ts.map +1 -0
  113. package/esm/test_utils.js +18 -0
  114. package/esm/test_utils.js.map +1 -0
  115. package/esm/testing/index.d.ts +2 -0
  116. package/esm/testing/index.d.ts.map +1 -0
  117. package/esm/testing/index.js +2 -0
  118. package/esm/testing/index.js.map +1 -0
  119. package/esm/types.d.ts +12 -12
  120. package/esm/types.d.ts.map +1 -1
  121. package/esm/types.js.map +1 -1
  122. package/package.json +14 -8
  123. package/ts/console.ts +55 -46
  124. package/ts/errors.ts +6 -6
  125. package/ts/formatter.ts +19 -19
  126. package/ts/index.ts +19 -10
  127. package/ts/log_levels.ts +107 -0
  128. package/ts/log_store.ts +63 -0
  129. package/ts/logger.ts +69 -59
  130. package/ts/memory.ts +66 -54
  131. package/ts/platform.ts +2 -2
  132. package/ts/standard_log.ctx.ts +8 -0
  133. package/ts/standard_log.mocks.ts +22 -0
  134. package/ts/standard_log.ts +140 -0
  135. package/ts/test_utils.ts +21 -0
  136. package/ts/testing/index.ts +1 -0
  137. package/ts/types.ts +72 -62
  138. package/cjs/logLevels.d.ts.map +0 -1
  139. package/cjs/logLevels.js.map +0 -1
  140. package/cjs/logStore.d.ts.map +0 -1
  141. package/cjs/logStore.js.map +0 -1
  142. package/cjs/standardLog.ctx.d.ts.map +0 -1
  143. package/cjs/standardLog.ctx.js.map +0 -1
  144. package/cjs/standardLog.d.ts.map +0 -1
  145. package/cjs/standardLog.js.map +0 -1
  146. package/esm/logLevels.d.ts.map +0 -1
  147. package/esm/logLevels.js.map +0 -1
  148. package/esm/logStore.d.ts.map +0 -1
  149. package/esm/logStore.js.map +0 -1
  150. package/esm/standardLog.ctx.d.ts.map +0 -1
  151. package/esm/standardLog.ctx.js.map +0 -1
  152. package/esm/standardLog.d.ts.map +0 -1
  153. package/esm/standardLog.js.map +0 -1
  154. package/ts/logLevels.ts +0 -108
  155. package/ts/logStore.ts +0 -54
  156. package/ts/standardLog.ctx.ts +0 -8
  157. package/ts/standardLog.ts +0 -135
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "standard-log",
3
- "version": "11.5.1",
3
+ "version": "12.0.0",
4
4
  "description": "The standard log library",
5
5
  "homepage": "https://github.com/unional/standard-log/tree/main/packages/log",
6
6
  "bugs": {
@@ -18,11 +18,17 @@
18
18
  "sideEffects": false,
19
19
  "type": "module",
20
20
  "exports": {
21
- "import": "./esm/index.js",
22
- "require": "./cjs/index.js",
23
- "default": "./cjs/index.js"
21
+ "import": {
22
+ "types": "./esm/index.d.ts",
23
+ "default": "./esm/index.js"
24
+ },
25
+ "require": {
26
+ "types": "./cjs/index.d.ts",
27
+ "default": "./cjs/index.js"
28
+ }
24
29
  },
25
30
  "main": "./cjs/index.js",
31
+ "module": "./esm/index.js",
26
32
  "browser": {
27
33
  "./cjs/platform.js": "./cjs/platform.browser.js",
28
34
  "./esm/platform.js": "./esm/platform.browser.js",
@@ -40,13 +46,13 @@
40
46
  ],
41
47
  "dependencies": {
42
48
  "@just-func/types": "^0.5.0",
43
- "iso-error": "^5.0.0",
49
+ "iso-error": "^6.0.0",
44
50
  "ms": "^2.1.3",
45
- "type-plus": "^5.0.0",
51
+ "type-plus": "^6.5.0",
46
52
  "upper-case": "^2.0.2"
47
53
  },
48
54
  "devDependencies": {
49
- "@repobuddy/jest": "^2.1.0",
55
+ "@repobuddy/jest": "^3.0.0",
50
56
  "@size-limit/preset-small-lib": "^8.1.0",
51
57
  "@types/jest": "^29.1.1",
52
58
  "@types/ms": "^0.7.31",
@@ -73,7 +79,7 @@
73
79
  "ts-jest": "^29.0.3",
74
80
  "ts-loader": "^9.4.1",
75
81
  "tslib": "^2.4.0",
76
- "typescript": "^4.8.4",
82
+ "typescript": "^5.0.0",
77
83
  "webpack": "^5.74.0",
78
84
  "webpack-bundle-analyzer": "^4.6.1",
79
85
  "webpack-cli": "^5.0.0"
package/ts/console.ts CHANGED
@@ -1,70 +1,79 @@
1
1
  import { unpartial } from 'type-plus'
2
2
  import { consoleFormatter } from './formatter.js'
3
- import { logLevels } from './logLevels.js'
3
+ import { logLevels } from './log_levels.js'
4
4
  import { polyfill } from './platform.js'
5
5
  import type { ConsoleLike, LogEntry, LogFormatter, LogReporter, LogReporterOptions } from './types.js'
6
6
 
7
7
  export function toConsoleMethod(level: number) {
8
- switch (true) {
9
- // edge case in case none is somehow written
10
- case (level <= 0): return 'debug'
11
- case (level <= logLevels.error): return 'error'
12
- case (level <= logLevels.warn): return 'warn'
13
- case (level <= logLevels.info): return 'info'
14
- default: return 'debug'
15
- }
8
+ switch (true) {
9
+ // edge case in case none is somehow written
10
+ case level <= 0:
11
+ return 'debug'
12
+ case level <= logLevels.error:
13
+ return 'error'
14
+ case level <= logLevels.warn:
15
+ return 'warn'
16
+ case level <= logLevels.info:
17
+ return 'info'
18
+ default:
19
+ return 'debug'
20
+ }
16
21
  }
17
22
 
18
23
  // istanbul ignore next
19
24
  /* eslint-disable no-console */
20
25
  function buildPolyfillConsole() {
21
- // old phantomjs does not have bind function
22
- const hasBind = !!console.log.bind as boolean
23
- if (hasBind) {
24
- return {
25
- // the `typeof` guards against IE where `console.log.apply()`
26
- // results in error `Object doesn't support property or method 'apply'`
27
- debug: (typeof console.info === 'function' ? console.debug : console.log).bind(console),
28
- info: (typeof console.info === 'function' ? console.info : console.log).bind(console),
29
- warn: (typeof console.warn === 'function' ? console.warn : console.log).bind(console),
30
- error: (typeof console.error === 'function' ? console.error : console.log).bind(console)
31
- }
32
- }
33
- else {
34
- return {
35
- debug: buildFn(typeof console.debug === 'function' ? 'debug' : 'log'),
36
- info: buildFn(typeof console.info === 'function' ? 'info' : 'log'),
37
- warn: buildFn(typeof console.warn === 'function' ? 'warn' : 'log'),
38
- error: buildFn(typeof console.error === 'function' ? 'error' : 'log'),
39
- }
40
- }
26
+ // old phantomjs does not have bind function
27
+ const hasBind = !!console.log.bind as boolean
28
+ if (hasBind) {
29
+ return {
30
+ // the `typeof` guards against IE where `console.log.apply()`
31
+ // results in error `Object doesn't support property or method 'apply'`
32
+ debug: (typeof console.info === 'function' ? console.debug : console.log).bind(console),
33
+ info: (typeof console.info === 'function' ? console.info : console.log).bind(console),
34
+ warn: (typeof console.warn === 'function' ? console.warn : console.log).bind(console),
35
+ error: (typeof console.error === 'function' ? console.error : console.log).bind(console)
36
+ }
37
+ } else {
38
+ return {
39
+ debug: buildFn(typeof console.debug === 'function' ? 'debug' : 'log'),
40
+ info: buildFn(typeof console.info === 'function' ? 'info' : 'log'),
41
+ warn: buildFn(typeof console.warn === 'function' ? 'warn' : 'log'),
42
+ error: buildFn(typeof console.error === 'function' ? 'error' : 'log')
43
+ }
44
+ }
41
45
  }
42
46
 
43
47
  // istanbul ignore next
44
48
  function buildFn(name: 'debug' | 'info' | 'warn' | 'error' | 'log') {
45
- return function (...args: any[]) { console[name](...args) }
49
+ return function (...args: any[]) {
50
+ console[name](...args)
51
+ }
46
52
  }
47
53
 
48
-
49
54
  export type ConsoleLogFormatter = LogFormatter<any[]>
50
55
  export type ConsoleLogReporter = LogReporter<any[]> & { console: ConsoleLike }
51
56
  export type ConsoleLogReporterOptions = LogReporterOptions<any[]>
52
57
 
53
58
  export function createConsoleLogReporter(options?: ConsoleLogReporterOptions): ConsoleLogReporter {
54
- const { id, formatter, filter } = unpartial({ id: 'console', formatter: consoleFormatter }, options)
55
- if (!polyfill.console) polyfill.console = buildPolyfillConsole()
59
+ const { id, formatter, filter } = unpartial({ id: 'console', formatter: consoleFormatter }, options)
60
+ if (!polyfill.console) polyfill.console = buildPolyfillConsole()
56
61
 
57
- return Object.freeze({
58
- id,
59
- isConsoleReporter: true,
60
- get formatter() { return formatter },
61
- get filter() { return filter },
62
- console: polyfill.console,
63
- write(entry: LogEntry) {
64
- if (filter && !filter(entry)) return
65
- const values = formatter(entry)
66
- const method = toConsoleMethod(entry.level)
67
- this.console[method](...values)
68
- }
69
- })
62
+ return Object.freeze({
63
+ id,
64
+ isConsoleReporter: true,
65
+ get formatter() {
66
+ return formatter
67
+ },
68
+ get filter() {
69
+ return filter
70
+ },
71
+ console: polyfill.console,
72
+ write(entry: LogEntry) {
73
+ if (filter && !filter(entry)) return
74
+ const values = formatter(entry)
75
+ const method = toConsoleMethod(entry.level)
76
+ this.console[method](...values)
77
+ }
78
+ })
70
79
  }
package/ts/errors.ts CHANGED
@@ -4,16 +4,16 @@ import { IsoError, ModuleError } from 'iso-error'
4
4
  * @deprecated No longer used since 11.2.0
5
5
  */
6
6
  export class StandardLogError extends ModuleError {
7
- constructor(description: string, options?: IsoError.Options) {
8
- super('standard-log', description, options)
9
- }
7
+ constructor(description: string, options?: IsoError.Options) {
8
+ super('standard-log', description, options)
9
+ }
10
10
  }
11
11
 
12
12
  /**
13
13
  * @deprecated no longer used since 11.2.0
14
14
  */
15
15
  export class InvalidId extends StandardLogError {
16
- constructor(public id: string, options?: IsoError.Options) {
17
- super(`logger id only supports alphanumeric, unicode characters, and [:_-.]. Received ${id}`, options)
18
- }
16
+ constructor(public id: string, options?: IsoError.Options) {
17
+ super(`logger id only supports alphanumeric, unicode characters, and [:_-.]. Received ${id}`, options)
18
+ }
19
19
  }
package/ts/formatter.ts CHANGED
@@ -1,30 +1,30 @@
1
1
  import ms from 'ms'
2
2
  import { upperCase } from 'upper-case'
3
- import { toLogLevelName } from './logLevels.js'
3
+ import { toLogLevelName } from './log_levels.js'
4
4
  import type { LogEntry } from './types.js'
5
5
 
6
6
  export type TimestampFormat = 'none' | 'iso' | 'elapsed'
7
7
 
8
8
  export function createTimestampFormatter(format: TimestampFormat): (timestamp: Date) => string | undefined {
9
- switch (format) {
10
- case 'none':
11
- return (_timestamp: Date) => undefined
12
- case 'iso':
13
- return (timestamp: Date) => timestamp.toISOString()
14
- case 'elapsed': {
15
- let lastTick: number
16
- return (timestamp: Date) => {
17
- const newTick = timestamp.getTime()
18
- const result = ms(lastTick === undefined ? 0 : newTick - lastTick)
19
- lastTick = newTick
20
- return result
21
- }
22
- }
23
- }
9
+ switch (format) {
10
+ case 'none':
11
+ return (_timestamp: Date) => undefined
12
+ case 'iso':
13
+ return (timestamp: Date) => timestamp.toISOString()
14
+ case 'elapsed': {
15
+ let lastTick: number
16
+ return (timestamp: Date) => {
17
+ const newTick = timestamp.getTime()
18
+ const result = ms(lastTick === undefined ? 0 : newTick - lastTick)
19
+ lastTick = newTick
20
+ return result
21
+ }
22
+ }
23
+ }
24
24
  }
25
25
 
26
26
  export function formatLogLevel(level: number) {
27
- return `(${upperCase(toLogLevelName(level))})`
27
+ return `(${upperCase(toLogLevelName(level))})`
28
28
  }
29
29
 
30
30
  /**
@@ -34,12 +34,12 @@ export function formatLogLevel(level: number) {
34
34
  * This is typically useful for writing logs to file
35
35
  */
36
36
  export function plainLogFormatter({ id, level, args, timestamp }: LogEntry) {
37
- return [timestamp.toISOString(), id, formatLogLevel(level), ...args]
37
+ return [timestamp.toISOString(), id, formatLogLevel(level), ...args]
38
38
  }
39
39
 
40
40
  /**
41
41
  * Formats log entry like default console log.
42
42
  */
43
43
  export function consoleFormatter({ args }: LogEntry) {
44
- return args
44
+ return args
45
45
  }
package/ts/index.ts CHANGED
@@ -1,16 +1,25 @@
1
1
  export * from './console.js'
2
2
  export * from './errors.js'
3
3
  export * from './formatter.js'
4
- export * from './logLevels.js'
4
+ export * from './log_levels.js'
5
5
  export * from './memory.js'
6
- export {
7
- configGlobal, createStandardLog, createStandardLogForTest,
8
- getLogger, suppressLogs
9
- } from './standardLog.js'
10
- export type { GetLogger, StandardLog, StandardLogForTest } from './standardLog.js'
6
+ export { configGlobal, createStandardLog, getLogger, suppressLogs } from './standard_log.js'
7
+ export type { GetLogger, StandardLog } from './standard_log.js'
11
8
  export type {
12
- ConsoleLike, LogEntry, LogFilter, LogFormatter, LogFunction, Logger, LoggerOptions, LogLevel,
13
- LogLevelEntry, LogMethod, LogMethodNames, LogReporter, LogReporterOptions, ReporterFilter,
14
- StandardLogInstance, StandardLogOptions
9
+ ConsoleLike,
10
+ LogEntry,
11
+ LogFilter,
12
+ LogFormatter,
13
+ LogFunction,
14
+ LogLevel,
15
+ LogLevelEntry,
16
+ LogMethod,
17
+ LogMethodNames,
18
+ LogReporter,
19
+ LogReporterOptions,
20
+ Logger,
21
+ LoggerOptions,
22
+ ReporterFilter,
23
+ StandardLogInstance,
24
+ StandardLogOptions
15
25
  } from './types.js'
16
-
@@ -0,0 +1,107 @@
1
+ export const logLevels = {
2
+ /**
3
+ * none: 0
4
+ */
5
+ none: 0 as const,
6
+ /**
7
+ * emergency: 100
8
+ *
9
+ * emergency -> alert
10
+ */
11
+ emergency: 100 as const,
12
+ /**
13
+ * alert: 200
14
+ *
15
+ * emergency -> alert -> critical
16
+ */
17
+ alert: 200 as const,
18
+ /**
19
+ * critical: 300
20
+ *
21
+ * alert -> critical -> error
22
+ */
23
+ critical: 300 as const,
24
+ /**
25
+ * error: 400
26
+ *
27
+ * critical -> error -> warn
28
+ */
29
+ error: 400 as const,
30
+ /**
31
+ * warn: 500
32
+ *
33
+ * error -> warn -> notice
34
+ */
35
+ warn: 500 as const,
36
+ /**
37
+ * notice: 600
38
+ *
39
+ * warn -> notice -> info
40
+ */
41
+ notice: 600 as const,
42
+ /**
43
+ * info: 700
44
+ *
45
+ * notice -> info -> debug
46
+ */
47
+ info: 700 as const,
48
+ /**
49
+ * debug: 800
50
+ *
51
+ * info -> debug -> trace
52
+ */
53
+ debug: 800 as const,
54
+ /**
55
+ * trace: 900
56
+ *
57
+ * debug -> trace -> planck
58
+ */
59
+ trace: 900 as const,
60
+ /**
61
+ * planck: Infinity
62
+ * `planck unit` is a very small unit <https://en.wikipedia.org/wiki/Planck_units>.
63
+ *
64
+ * trace -> planck
65
+ */
66
+ planck: Infinity,
67
+ /**
68
+ * all: Infinity
69
+ */
70
+ all: Infinity
71
+ }
72
+
73
+ /**
74
+ * Convert default log level value to log level name.
75
+ */
76
+ export function toLogLevelName(level: number) {
77
+ if (level <= 100) return 'emergency'
78
+ if (level <= 200) return 'alert'
79
+ if (level <= 300) return 'critical'
80
+ if (level <= 400) return 'error'
81
+ if (level <= 500) return 'warn'
82
+ if (level <= 600) return 'notice'
83
+ if (level <= 700) return 'info'
84
+ if (level <= 800) return 'debug'
85
+ if (level <= 900) return 'trace'
86
+ return 'planck'
87
+ }
88
+
89
+ /**
90
+ * Convert default log level name to log level value.
91
+ */
92
+ export function toLogLevel(name: string): number | undefined {
93
+ return (logLevels as any)[name.toLocaleLowerCase()]
94
+ }
95
+
96
+ export const DEFAULT_LOG_METHOD_NAMES: string[] = [
97
+ 'emergency',
98
+ 'alert',
99
+ 'critical',
100
+ 'error',
101
+ 'warn',
102
+ 'notice',
103
+ 'info',
104
+ 'debug',
105
+ 'trace',
106
+ 'planck'
107
+ ]
@@ -0,0 +1,63 @@
1
+ import { mapKey, record, reduceByKey, reduceKey } from 'type-plus'
2
+ import { createConsoleLogReporter } from './console.js'
3
+ import { logLevels, toLogLevel, toLogLevelName } from './log_levels.js'
4
+ import type { Logger, LogLevel, LogMethodNames, LogReporter, StandardLogOptions } from './types.js'
5
+
6
+ export function createLogStore<N extends string = LogMethodNames>(
7
+ options: Required<StandardLogOptions<N>>
8
+ ): LogStore {
9
+ return {
10
+ loggers: record(),
11
+ nonConsoleLoggers: record(),
12
+ reporters: options.reporters || [createConsoleLogReporter()],
13
+ logLevel: options.logLevel,
14
+ logLevelStore: logLevelStore(options)
15
+ }
16
+ }
17
+
18
+ export interface LogStore {
19
+ loggers: Record<string, Logger>
20
+ nonConsoleLoggers: Record<string, Logger>
21
+ reporters: LogReporter[]
22
+ logLevel: LogLevel
23
+ logLevelStore: ReturnType<typeof logLevelStore>
24
+ }
25
+
26
+ export interface LogLevelStoreOptions {
27
+ customLevels?: Record<string, number>
28
+ }
29
+
30
+ export function logLevelStore(options: LogLevelStoreOptions) {
31
+ const customLevels = options.customLevels || record()
32
+
33
+ const store = {
34
+ customLevels,
35
+ customLevelsReverse: reduceByKey(
36
+ customLevels,
37
+ (p, k) => {
38
+ p[customLevels[k]] = k
39
+ return p
40
+ },
41
+ {} as Record<number, string>
42
+ )
43
+ }
44
+ return {
45
+ getName(level: number) {
46
+ return store.customLevelsReverse[level] ?? toLogLevelName(level)
47
+ },
48
+ getLevel(name: string) {
49
+ return store.customLevels[name] ?? toLogLevel(name)
50
+ },
51
+ getAllLevels() {
52
+ return reduceKey(
53
+ logLevels,
54
+ (result, name) => {
55
+ if (name !== 'none' && name !== 'all') result.push({ name, level: logLevels[name] })
56
+
57
+ return result
58
+ },
59
+ mapKey(store.customLevels, name => ({ name, level: store.customLevels[name] }))
60
+ )
61
+ }
62
+ }
63
+ }
package/ts/logger.ts CHANGED
@@ -1,74 +1,84 @@
1
- import { logLevels } from './logLevels.js'
2
- import { LogStore } from './logStore.js'
1
+ import { logLevels } from './log_levels.js'
2
+ import { LogStore } from './log_store.js'
3
3
  import type { LogEntry, LogFunction, Logger, LoggerOptions, LogMethodNames, LogReporter } from './types.js'
4
4
  import { LogLevel } from './types.js'
5
5
 
6
6
  export function createLogger<N extends string = LogMethodNames>(
7
- store: LogStore, id: string, options?: LoggerOptions
7
+ store: LogStore,
8
+ id: string,
9
+ options?: LoggerOptions
8
10
  ): Logger<N | LogMethodNames> {
9
- id = sanitizeId(id)
10
- const writeTo = options?.writeTo ?? (() => true)
11
- const [filter, reporters]: [(reporterId: string) => boolean, LogReporter[]] = typeof writeTo === 'string'
12
- ? [id => id === writeTo, store.reporters]
13
- : writeTo instanceof RegExp
14
- ? [id => writeTo.test(id), store.reporters]
15
- : typeof writeTo === 'function'
16
- ? [writeTo, store.reporters]
17
- : [() => true, [writeTo]]
11
+ id = sanitizeId(id)
12
+ const writeTo = options?.writeTo ?? (() => true)
13
+ const [filter, reporters]: [(reporterId: string) => boolean, LogReporter[]] =
14
+ typeof writeTo === 'string'
15
+ ? [id => id === writeTo, store.reporters]
16
+ : writeTo instanceof RegExp
17
+ ? [id => writeTo.test(id), store.reporters]
18
+ : typeof writeTo === 'function'
19
+ ? [writeTo, store.reporters]
20
+ : [() => true, [writeTo]]
18
21
 
19
- function write(entry: LogEntry) {
20
- writeToReporters(reporters, entry, filter)
21
- }
22
+ function write(entry: LogEntry) {
23
+ writeToReporters(reporters, entry, filter)
24
+ }
22
25
 
23
- const logger = {
24
- level: options?.level,
25
- write
26
- } as any
26
+ const logger = {
27
+ level: options?.level,
28
+ write
29
+ } as any
27
30
 
28
- Object.defineProperties(logger, {
29
- id: { value: id, writable: false },
30
- count: {
31
- writable: false,
32
- value: ((counter) => (...args: any[]) => {
33
- if (shouldLog(store, logLevels.debug, logger.level))
34
- write({
35
- id,
36
- level: logLevels.debug,
37
- args: [++counter, ...args],
38
- timestamp: new Date()
39
- })
40
- })(0)
41
- },
42
- on: {
43
- writable: false,
44
- value: (level: number | N | LogMethodNames, logFn: LogFunction) => {
45
- const logLevel = typeof level === 'string' ? store.logLevelStore.getLevel(level)! : level
46
- if (shouldLog(store, logLevel, logger.level)) {
47
- const methodName = store.logLevelStore.getName(logLevel)
48
- const bindedMethod = logger[methodName].bind(logger)
49
- const result = logFn(bindedMethod, logger.level ?? store.logLevel)
50
- if (result) bindedMethod(result)
51
- }
52
- }
53
- }
54
- })
55
- store.logLevelStore.getAllLevels().forEach(function addMethod({ name, level }) {
56
- Object.defineProperty(logger, name, {
57
- writable: false,
58
- value: (...args: any[]) => {
59
- if (shouldLog(store, level, logger.level)) write({ id, level, args, timestamp: new Date() })
60
- }
61
- })
62
- })
63
- return logger
31
+ Object.defineProperties(logger, {
32
+ id: { value: id, writable: false },
33
+ count: {
34
+ writable: false,
35
+ value: (
36
+ counter =>
37
+ (...args: any[]) => {
38
+ if (shouldLog(store, logLevels.debug, logger.level))
39
+ write({
40
+ id,
41
+ level: logLevels.debug,
42
+ args: [++counter, ...args],
43
+ timestamp: new Date()
44
+ })
45
+ }
46
+ )(0)
47
+ },
48
+ on: {
49
+ writable: false,
50
+ value: (level: number | N | LogMethodNames, logFn: LogFunction) => {
51
+ const logLevel = typeof level === 'string' ? store.logLevelStore.getLevel(level)! : level
52
+ if (shouldLog(store, logLevel, logger.level)) {
53
+ const methodName = store.logLevelStore.getName(logLevel)
54
+ const bindedMethod = logger[methodName].bind(logger)
55
+ const result = logFn(bindedMethod, logger.level ?? store.logLevel)
56
+ if (result) bindedMethod(result)
57
+ }
58
+ }
59
+ }
60
+ })
61
+ store.logLevelStore.getAllLevels().forEach(function addMethod({ name, level }) {
62
+ Object.defineProperty(logger, name, {
63
+ writable: false,
64
+ value: (...args: any[]) => {
65
+ if (shouldLog(store, level, logger.level)) write({ id, level, args, timestamp: new Date() })
66
+ }
67
+ })
68
+ })
69
+ return logger
64
70
  }
65
71
 
66
72
  function sanitizeId(id: string) {
67
- return id.replace(/[`~!#$%^&*()=+[\]{},|<>?]/g, '-')
73
+ return id.replace(/[`~!#$%^&*()=+[\]{},|<>?]/g, '-')
68
74
  }
69
75
 
70
- function writeToReporters(reporters: LogReporter[], logEntry: LogEntry, filter: (reporterId: string) => boolean) {
71
- reporters.filter(r => filter(r.id)).forEach(r => r.write(logEntry!))
76
+ function writeToReporters(
77
+ reporters: LogReporter[],
78
+ logEntry: LogEntry,
79
+ filter: (reporterId: string) => boolean
80
+ ) {
81
+ reporters.filter(r => filter(r.id)).forEach(r => r.write(logEntry!))
72
82
  }
73
83
 
74
84
  /**
@@ -77,5 +87,5 @@ function writeToReporters(reporters: LogReporter[], logEntry: LogEntry, filter:
77
87
  * It can be undefined which the global log level will be used.
78
88
  */
79
89
  function shouldLog(store: LogStore, targetLevel: LogLevel, loggerLevel: LogLevel | undefined) {
80
- return targetLevel <= (loggerLevel !== undefined ? loggerLevel : store.logLevel)
90
+ return targetLevel <= (loggerLevel !== undefined ? loggerLevel : store.logLevel)
81
91
  }