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
@@ -1 +0,0 @@
1
- {"version":3,"file":"standardLog.js","sourceRoot":"","sources":["../ts/standardLog.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAA;AAC5C,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAA;AAC1C,OAAO,EAAE,wBAAwB,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAA;AACpE,OAAO,EAAE,cAAc,EAAY,MAAM,eAAe,CAAA;AACxD,OAAO,E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,CAAC,CAAC,CAAA;IAC/C,OAAO,MAAM,CAAA;AACf,CAAC;AAID,MAAM,UAAU,SAAS,CAAoC,EAAU,EAAE,OAAuB;IAC9F,OAAO,WAAW,EAAK,CAAC,SAAS,CAAC,EAAE,EAAE,OAAO,CAAC,CAAA;AAChD,CAAC;AAED,SAAS,WAAW;IAClB,IAAI,CAAC,GAAG,CAAC,GAAG;QAAE,GAAG,CAAC,GAAG,GAAG,wBAAwB,EAAE,CAAA;IAElD,OAAO,GAAG,CAAC,GAAI,CAAC,WAAqC,CAAA;AACvD,CAAC;AAED,MAAM,UAAU,YAAY,CAAC,OAAiD;IAC5E,IAAI,GAAG,CAAC,UAAU,EAAE;QAClB,MAAM,GAAG,GAAG,GAAG,CAAC,GAAI,CAAC,WAAW,CAAC,SAAS,CAAC,cAAc,CAAC,CAAA;QAC1D,GAAG,CAAC,IAAI,CAAC,kJAAkJ,CAAC,CAAA;KAC7J;IAED,IAAI,GAAG,CAAC,GAAG,EAAE;QACX,IAAI,OAAO,CAAC,QAAQ,EAAE;YACpB,GAAG,CAAC,GAAG,CAAC,KAAK,CAAC,QAAQ,GAAG,OAAO,CAAC,QAAQ,CAAA;SAC1C;QACD,IAAI,OAAO,CAAC,SAAS,EAAE;YACrB,MAAM,SAAS,GAAG,GAAG,CAAC,GAAG,CAAC,KAAK,CAAC,SAAS,CAAA;YACzC,SAAS,CAAC,MAAM,GAAG,CAAC,CAAA;YACpB,SAAS,CAAC,IAAI,CAAC,GAAG,OAAO,CAAC,SAAS,CAAC,CAAA;SACrC;KACF;SACI;QACH,GAAG,CAAC,GAAG,GAAG,wBAAwB,CAAC,OAAO,CAAC,CAAA;KAC5C;IACD,GAAG,CAAC,UAAU,GAAG,IAAI,CAAA;AACvB,CAAC","sourcesContent":["import { record, required } from 'type-plus'\nimport { createLogger } from './logger.js'\nimport { DEFAULT_LOG_METHOD_NAMES, logLevels } from './logLevels.js'\nimport { createLogStore, LogStore } from './logStore.js'\nimport { createMemoryLogReporter, MemoryLogReporter } from './memory.js'\nimport { ctx } from './standardLog.ctx.js'\nimport type { Logger, LoggerOptions, LogLevel, LogMethodNames, StandardLogInstance, StandardLogOptions } from './types.js'\n\nexport interface StandardLog<N extends string = LogMethodNames> extends Readonly<StandardLogInstance<N>> {\n}\n\nexport function createStandardLog<N extends string = LogMethodNames>(\n options?: StandardLogOptions<N>\n): StandardLog<N> {\n const closure = createStandardLogClosure(options)\n if (options?.reporters?.some(r => !r.id)) {\n const log = closure.standardLog.getLogger('standard-log')\n log.critical(`Detected reporter with empty id. This is not allowed as it will create problems with feature like 'getNonConsoleLogger()'. Please add an id to your reporter.`)\n }\n\n if (!ctx.gsl) ctx.gsl = closure as any\n return closure.standardLog\n}\n\nfunction noop() { }\n\nexport function createStandardLogClosure<N extends string = LogMethodNames>(\n options?: StandardLogOptions<N>\n): { store: LogStore, standardLog: StandardLog<N> } {\n // TODO reporter.id must not be empty\n const store = createLogStore(required({ logLevel: logLevels.info }, options))\n const nonConsoleReporters = options?.reporters?.filter(r => !r.isConsoleReporter).map(r => r.id) ?? []\n const logMethods = DEFAULT_LOG_METHOD_NAMES\n .concat(Object.keys(options?.customLevels ?? {}))\n .concat(['on', 'count', 'write'])\n return {\n store,\n standardLog: {\n get logLevel() { return store.logLevel },\n\n toLogLevelName(level: number) {\n return store.logLevelStore.getName(level)\n },\n toLogLevel(name: string) {\n return store.logLevelStore.getLevel(name)\n },\n getLogger(id: string, options?: LoggerOptions) {\n if (store.loggers[id]) return store.loggers[id] as any\n return store.loggers[id] = createLogger(store, id, { ssf: this.getLogger, ...options }) as any\n },\n getNonConsoleLogger(id, options?) {\n if (store.nonConsoleLoggers[id]) return store.nonConsoleLoggers[id] as any\n\n if (store.loggers[id]) {\n const log = this.getLogger('standard-log')\n log.warn(`attempt to create a non-console logger with existing id ('${id}'), ignoring`)\n return store.nonConsoleLoggers[id] = createDummyLogger(logMethods)\n }\n\n return store.nonConsoleLoggers[id] = nonConsoleReporters.length === 0\n ? createDummyLogger(logMethods)\n : createLogger(store, id, {\n ssf: this.getNonConsoleLogger,\n ...options,\n writeTo(reporterId) {\n return nonConsoleReporters.indexOf(reporterId) >= 0\n }\n })\n },\n }\n }\n}\n\nfunction createDummyLogger(logMethods: string[]) {\n return logMethods.reduce((p, m) => {\n p[m] = noop\n return p\n }, record()) as Logger\n}\n\nexport type StandardLogForTest<N extends string = LogMethodNames> = StandardLog<N> & { reporter: MemoryLogReporter }\n\nexport function createStandardLogForTest<N extends string = LogMethodNames>(\n logLevel: LogLevel = logLevels.debug\n): StandardLogForTest<N> {\n const reporter = createMemoryLogReporter()\n const closure = createStandardLogClosure<N>({ reporters: [reporter], logLevel })\n return { ...closure.standardLog, reporter }\n}\n\n/**\n * Suppress logging within the code block.\n */\nexport function suppressLogs<R>(block: () => R, log: Logger, ...logs: Logger[]): R\nexport function suppressLogs<R>(block: () => R, ...logs: Logger[]): R {\n const origLevels = logs.map(l => l.level)\n logs.forEach(l => l.level = logLevels.none)\n const result = block()\n logs.forEach((l, i) => l.level = origLevels[i])\n return result\n}\n\nexport type GetLogger<N extends string = LogMethodNames> = (id: string, options?: LoggerOptions) => Logger<LogMethodNames | N>\n\nexport function getLogger<N extends string = LogMethodNames>(id: string, options?: LoggerOptions) {\n return getGlobalSL<N>().getLogger(id, options)\n}\n\nfunction getGlobalSL<N extends string = LogMethodNames>(): StandardLogInstance<N> {\n if (!ctx.gsl) ctx.gsl = createStandardLogClosure()\n\n return ctx.gsl!.standardLog as StandardLogInstance<N>\n}\n\nexport function configGlobal(options: Omit<StandardLogOptions, 'customLevels'>) {\n if (ctx.configured) {\n const log = ctx.gsl!.standardLog.getLogger('standard-log')\n log.warn('configGlobal() is being called more than once. Please make sure this is expected. Application should use `createStandardLog()` most of the time.')\n }\n\n if (ctx.gsl) {\n if (options.logLevel) {\n ctx.gsl.store.logLevel = options.logLevel\n }\n if (options.reporters) {\n const reporters = ctx.gsl.store.reporters\n reporters.length = 0\n reporters.push(...options.reporters)\n }\n }\n else {\n ctx.gsl = createStandardLogClosure(options)\n }\n ctx.configured = true\n}\n"]}
package/ts/logLevels.ts DELETED
@@ -1,108 +0,0 @@
1
-
2
- export const logLevels = {
3
- /**
4
- * none: 0
5
- */
6
- none: 0 as const,
7
- /**
8
- * emergency: 100
9
- *
10
- * emergency -> alert
11
- */
12
- emergency: 100 as const,
13
- /**
14
- * alert: 200
15
- *
16
- * emergency -> alert -> critical
17
- */
18
- alert: 200 as const,
19
- /**
20
- * critical: 300
21
- *
22
- * alert -> critical -> error
23
- */
24
- critical: 300 as const,
25
- /**
26
- * error: 400
27
- *
28
- * critical -> error -> warn
29
- */
30
- error: 400 as const,
31
- /**
32
- * warn: 500
33
- *
34
- * error -> warn -> notice
35
- */
36
- warn: 500 as const,
37
- /**
38
- * notice: 600
39
- *
40
- * warn -> notice -> info
41
- */
42
- notice: 600 as const,
43
- /**
44
- * info: 700
45
- *
46
- * notice -> info -> debug
47
- */
48
- info: 700 as const,
49
- /**
50
- * debug: 800
51
- *
52
- * info -> debug -> trace
53
- */
54
- debug: 800 as const,
55
- /**
56
- * trace: 900
57
- *
58
- * debug -> trace -> planck
59
- */
60
- trace: 900 as const,
61
- /**
62
- * planck: Infinity
63
- * `planck unit` is a very small unit <https://en.wikipedia.org/wiki/Planck_units>.
64
- *
65
- * trace -> planck
66
- */
67
- planck: Infinity,
68
- /**
69
- * all: Infinity
70
- */
71
- all: Infinity
72
- }
73
-
74
- /**
75
- * Convert default log level value to log level name.
76
- */
77
- export function toLogLevelName(level: number) {
78
- if (level <= 100) return 'emergency'
79
- if (level <= 200) return 'alert'
80
- if (level <= 300) return 'critical'
81
- if (level <= 400) return 'error'
82
- if (level <= 500) return 'warn'
83
- if (level <= 600) return 'notice'
84
- if (level <= 700) return 'info'
85
- if (level <= 800) return 'debug'
86
- if (level <= 900) return 'trace'
87
- return 'planck'
88
- }
89
-
90
- /**
91
- * Convert default log level name to log level value.
92
- */
93
- export function toLogLevel(name: string): number | undefined {
94
- return (logLevels as any)[name.toLocaleLowerCase()]
95
- }
96
-
97
- export const DEFAULT_LOG_METHOD_NAMES: string[] = [
98
- 'emergency',
99
- 'alert',
100
- 'critical',
101
- 'error',
102
- 'warn',
103
- 'notice',
104
- 'info',
105
- 'debug',
106
- 'trace',
107
- 'planck'
108
- ]
package/ts/logStore.ts DELETED
@@ -1,54 +0,0 @@
1
-
2
- import { mapKey, record, reduceByKey, reduceKey } from 'type-plus'
3
- import { createConsoleLogReporter } from './console.js'
4
- import { logLevels, toLogLevel, toLogLevelName } from './logLevels.js'
5
- import type { Logger, LogLevel, LogMethodNames, LogReporter, StandardLogOptions } from './types.js'
6
-
7
- export function createLogStore<N extends string = LogMethodNames>(options: Required<StandardLogOptions<N>>): LogStore {
8
- return {
9
- loggers: record(),
10
- nonConsoleLoggers: record(),
11
- reporters: options.reporters || [createConsoleLogReporter()],
12
- logLevel: options.logLevel,
13
- logLevelStore: logLevelStore(options)
14
- }
15
- }
16
-
17
- export interface LogStore {
18
- loggers: Record<string, Logger>,
19
- nonConsoleLoggers: Record<string, Logger>,
20
- reporters: LogReporter[],
21
- logLevel: LogLevel,
22
- logLevelStore: ReturnType<typeof logLevelStore>
23
- }
24
-
25
- export interface LogLevelStoreOptions {
26
- customLevels?: Record<string, number>
27
- }
28
-
29
- export function logLevelStore(options: LogLevelStoreOptions) {
30
- const customLevels = options.customLevels || record()
31
-
32
- const store = {
33
- customLevels,
34
- customLevelsReverse: reduceByKey(customLevels, (p, k) => {
35
- p[customLevels[k]] = k
36
- return p
37
- }, {} as Record<number, string>)
38
- }
39
- return {
40
- getName(level: number) {
41
- return store.customLevelsReverse[level] ?? toLogLevelName(level)
42
- },
43
- getLevel(name: string) {
44
- return store.customLevels[name] ?? toLogLevel(name)
45
- },
46
- getAllLevels() {
47
- return reduceKey(logLevels, (result, name) => {
48
- if (name !== 'none' && name !== 'all') result.push({ name, level: logLevels[name] })
49
-
50
- return result
51
- }, mapKey(store.customLevels, name => ({ name, level: store.customLevels[name] })))
52
- }
53
- }
54
- }
@@ -1,8 +0,0 @@
1
- import { record } from 'type-plus'
2
- import type { LogStore } from './logStore.js'
3
- import type { LogMethodNames, StandardLogInstance } from './types.js'
4
-
5
- export const ctx: {
6
- configured?: boolean,
7
- gsl?: { store: LogStore, standardLog: StandardLogInstance<LogMethodNames> }
8
- } = record()
package/ts/standardLog.ts DELETED
@@ -1,135 +0,0 @@
1
- import { record, required } from 'type-plus'
2
- import { createLogger } from './logger.js'
3
- import { DEFAULT_LOG_METHOD_NAMES, logLevels } from './logLevels.js'
4
- import { createLogStore, LogStore } from './logStore.js'
5
- import { createMemoryLogReporter, MemoryLogReporter } from './memory.js'
6
- import { ctx } from './standardLog.ctx.js'
7
- import type { Logger, LoggerOptions, LogLevel, LogMethodNames, StandardLogInstance, StandardLogOptions } from './types.js'
8
-
9
- export interface StandardLog<N extends string = LogMethodNames> extends Readonly<StandardLogInstance<N>> {
10
- }
11
-
12
- export function createStandardLog<N extends string = LogMethodNames>(
13
- options?: StandardLogOptions<N>
14
- ): StandardLog<N> {
15
- const closure = createStandardLogClosure(options)
16
- if (options?.reporters?.some(r => !r.id)) {
17
- const log = closure.standardLog.getLogger('standard-log')
18
- log.critical(`Detected reporter with empty id. This is not allowed as it will create problems with feature like 'getNonConsoleLogger()'. Please add an id to your reporter.`)
19
- }
20
-
21
- if (!ctx.gsl) ctx.gsl = closure as any
22
- return closure.standardLog
23
- }
24
-
25
- function noop() { }
26
-
27
- export function createStandardLogClosure<N extends string = LogMethodNames>(
28
- options?: StandardLogOptions<N>
29
- ): { store: LogStore, standardLog: StandardLog<N> } {
30
- // TODO reporter.id must not be empty
31
- const store = createLogStore(required({ logLevel: logLevels.info }, options))
32
- const nonConsoleReporters = options?.reporters?.filter(r => !r.isConsoleReporter).map(r => r.id) ?? []
33
- const logMethods = DEFAULT_LOG_METHOD_NAMES
34
- .concat(Object.keys(options?.customLevels ?? {}))
35
- .concat(['on', 'count', 'write'])
36
- return {
37
- store,
38
- standardLog: {
39
- get logLevel() { return store.logLevel },
40
-
41
- toLogLevelName(level: number) {
42
- return store.logLevelStore.getName(level)
43
- },
44
- toLogLevel(name: string) {
45
- return store.logLevelStore.getLevel(name)
46
- },
47
- getLogger(id: string, options?: LoggerOptions) {
48
- if (store.loggers[id]) return store.loggers[id] as any
49
- return store.loggers[id] = createLogger(store, id, { ssf: this.getLogger, ...options }) as any
50
- },
51
- getNonConsoleLogger(id, options?) {
52
- if (store.nonConsoleLoggers[id]) return store.nonConsoleLoggers[id] as any
53
-
54
- if (store.loggers[id]) {
55
- const log = this.getLogger('standard-log')
56
- log.warn(`attempt to create a non-console logger with existing id ('${id}'), ignoring`)
57
- return store.nonConsoleLoggers[id] = createDummyLogger(logMethods)
58
- }
59
-
60
- return store.nonConsoleLoggers[id] = nonConsoleReporters.length === 0
61
- ? createDummyLogger(logMethods)
62
- : createLogger(store, id, {
63
- ssf: this.getNonConsoleLogger,
64
- ...options,
65
- writeTo(reporterId) {
66
- return nonConsoleReporters.indexOf(reporterId) >= 0
67
- }
68
- })
69
- },
70
- }
71
- }
72
- }
73
-
74
- function createDummyLogger(logMethods: string[]) {
75
- return logMethods.reduce((p, m) => {
76
- p[m] = noop
77
- return p
78
- }, record()) as Logger
79
- }
80
-
81
- export type StandardLogForTest<N extends string = LogMethodNames> = StandardLog<N> & { reporter: MemoryLogReporter }
82
-
83
- export function createStandardLogForTest<N extends string = LogMethodNames>(
84
- logLevel: LogLevel = logLevels.debug
85
- ): StandardLogForTest<N> {
86
- const reporter = createMemoryLogReporter()
87
- const closure = createStandardLogClosure<N>({ reporters: [reporter], logLevel })
88
- return { ...closure.standardLog, reporter }
89
- }
90
-
91
- /**
92
- * Suppress logging within the code block.
93
- */
94
- export function suppressLogs<R>(block: () => R, log: Logger, ...logs: Logger[]): R
95
- export function suppressLogs<R>(block: () => R, ...logs: Logger[]): R {
96
- const origLevels = logs.map(l => l.level)
97
- logs.forEach(l => l.level = logLevels.none)
98
- const result = block()
99
- logs.forEach((l, i) => l.level = origLevels[i])
100
- return result
101
- }
102
-
103
- export type GetLogger<N extends string = LogMethodNames> = (id: string, options?: LoggerOptions) => Logger<LogMethodNames | N>
104
-
105
- export function getLogger<N extends string = LogMethodNames>(id: string, options?: LoggerOptions) {
106
- return getGlobalSL<N>().getLogger(id, options)
107
- }
108
-
109
- function getGlobalSL<N extends string = LogMethodNames>(): StandardLogInstance<N> {
110
- if (!ctx.gsl) ctx.gsl = createStandardLogClosure()
111
-
112
- return ctx.gsl!.standardLog as StandardLogInstance<N>
113
- }
114
-
115
- export function configGlobal(options: Omit<StandardLogOptions, 'customLevels'>) {
116
- if (ctx.configured) {
117
- const log = ctx.gsl!.standardLog.getLogger('standard-log')
118
- log.warn('configGlobal() is being called more than once. Please make sure this is expected. Application should use `createStandardLog()` most of the time.')
119
- }
120
-
121
- if (ctx.gsl) {
122
- if (options.logLevel) {
123
- ctx.gsl.store.logLevel = options.logLevel
124
- }
125
- if (options.reporters) {
126
- const reporters = ctx.gsl.store.reporters
127
- reporters.length = 0
128
- reporters.push(...options.reporters)
129
- }
130
- }
131
- else {
132
- ctx.gsl = createStandardLogClosure(options)
133
- }
134
- ctx.configured = true
135
- }