standard-log 11.0.1 → 11.1.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 (54) hide show
  1. package/cjs/logStore.d.ts +1 -0
  2. package/cjs/logStore.d.ts.map +1 -1
  3. package/cjs/logStore.js +3 -2
  4. package/cjs/logStore.js.map +1 -1
  5. package/cjs/memory.d.ts +1 -0
  6. package/cjs/memory.d.ts.map +1 -1
  7. package/cjs/memory.js +13 -4
  8. package/cjs/memory.js.map +1 -1
  9. package/cjs/platform.browser.d.ts.map +1 -1
  10. package/cjs/platform.browser.js +2 -1
  11. package/cjs/platform.browser.js.map +1 -1
  12. package/cjs/platform.d.ts.map +1 -1
  13. package/cjs/platform.js +2 -1
  14. package/cjs/platform.js.map +1 -1
  15. package/cjs/standardLog.d.ts +1 -0
  16. package/cjs/standardLog.d.ts.map +1 -1
  17. package/cjs/standardLog.js +37 -6
  18. package/cjs/standardLog.js.map +1 -1
  19. package/cjs/standardLog.spec.js +57 -0
  20. package/cjs/standardLog.spec.js.map +1 -1
  21. package/dist/standard-log.es5.js +56 -14
  22. package/dist/standard-log.es5.js.gz +0 -0
  23. package/dist/standard-log.es5.js.map +1 -1
  24. package/dist/standard-log.es5.js.map.gz +0 -0
  25. package/dist/standard-log.esm.js +74 -26
  26. package/dist/standard-log.esm.js.gz +0 -0
  27. package/dist/standard-log.esm.js.map +1 -1
  28. package/dist/standard-log.esm.js.map.gz +0 -0
  29. package/esm/logStore.d.ts +1 -0
  30. package/esm/logStore.d.ts.map +1 -1
  31. package/esm/logStore.js +4 -3
  32. package/esm/logStore.js.map +1 -1
  33. package/esm/memory.d.ts +1 -0
  34. package/esm/memory.d.ts.map +1 -1
  35. package/esm/memory.js +11 -3
  36. package/esm/memory.js.map +1 -1
  37. package/esm/platform.browser.d.ts.map +1 -1
  38. package/esm/platform.browser.js +2 -1
  39. package/esm/platform.browser.js.map +1 -1
  40. package/esm/platform.d.ts.map +1 -1
  41. package/esm/platform.js +2 -1
  42. package/esm/platform.js.map +1 -1
  43. package/esm/standardLog.d.ts +1 -0
  44. package/esm/standardLog.d.ts.map +1 -1
  45. package/esm/standardLog.js +42 -7
  46. package/esm/standardLog.js.map +1 -1
  47. package/esm/standardLog.spec.js +58 -1
  48. package/esm/standardLog.spec.js.map +1 -1
  49. package/package.json +1 -1
  50. package/ts/logStore.ts +5 -3
  51. package/ts/memory.ts +14 -3
  52. package/ts/platform.browser.ts +2 -1
  53. package/ts/platform.ts +2 -1
  54. package/ts/standardLog.ts +46 -8
package/ts/memory.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { required } from 'type-plus'
1
+ import { required, unpartial } from 'type-plus'
2
2
  import { toInspectLogEntry } from './platform.js'
3
3
  import { formatLogLevel } from './formatter.js'
4
4
  import type { LogEntry, LogFormatter, LogReporter, LogReporterOptions } from './types.js'
@@ -30,9 +30,13 @@ export function toMessageWithLevel(logs: LogEntry[]) {
30
30
  }
31
31
 
32
32
  export function createMemoryLogReporter(options?: LogReporterOptions<LogEntry>): MemoryLogReporter {
33
+ return Object.freeze(buildMemoryReporter(unpartial({ id: 'memory' }, options)))
34
+ }
35
+
36
+ function buildMemoryReporter(options?: LogReporterOptions<LogEntry>) {
33
37
  const { id, formatter, filter } = required({ id: 'memory', formatter: (e: LogEntry) => e }, options)
34
38
  const logs: LogEntry[] = []
35
- return Object.freeze({
39
+ return {
36
40
  id,
37
41
  get formatter() { return formatter },
38
42
  get filter() { return filter },
@@ -60,5 +64,12 @@ export function createMemoryLogReporter(options?: LogReporterOptions<LogEntry>):
60
64
  const reporter = createConsoleLogReporter()
61
65
  logs.forEach(l => reporter.write(l))
62
66
  }
63
- })
67
+ }
68
+ }
69
+
70
+ export function createMemoryWithConsoleLogReporter(options?: LogReporterOptions<LogEntry>): MemoryLogReporter {
71
+ return Object.freeze(
72
+ Object.assign(buildMemoryReporter(unpartial({ id: 'memory-with-console' }, options)), {
73
+ isConsoleReporter: true
74
+ }))
64
75
  }
@@ -1,5 +1,6 @@
1
+ import { record } from 'type-plus'
1
2
  import type { ConsoleLike, LogEntry } from './types.js'
2
3
 
3
4
  export const toInspectLogEntry = (e: LogEntry) => e
4
5
 
5
- export const polyfill: { console?: ConsoleLike } = {}
6
+ export const polyfill: { console?: ConsoleLike } = record()
package/ts/platform.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { record } from 'type-plus'
1
2
  import { inspect } from 'util'
2
3
  import type { ConsoleLike, LogEntry } from './types.js'
3
4
 
@@ -5,4 +6,4 @@ export const toInspectLogEntry = (e: LogEntry) => (
5
6
  e.args = e.args.map(value => typeof value === 'object' && value !== null ? inspect(value) : value),
6
7
  e)
7
8
 
8
- export const polyfill: { console?: ConsoleLike } = {}
9
+ export const polyfill: { console?: ConsoleLike } = record()
package/ts/standardLog.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { record, required } from 'type-plus'
2
2
  import { createLogger } from './logger.js'
3
- import { logLevels } from './logLevels.js'
3
+ import { DEFAULT_LOG_METHOD_NAMES, logLevels } from './logLevels.js'
4
4
  import { createLogStore, LogStore } from './logStore.js'
5
5
  import { createMemoryLogReporter, MemoryLogReporter } from './memory.js'
6
6
  import type { Logger, LoggerOptions, LogLevel, LogMethodNames, StandardLogOptions } from './types.js'
@@ -9,7 +9,8 @@ export interface StandardLogInstance<N extends string = LogMethodNames> {
9
9
  logLevel: number,
10
10
  toLogLevelName(level: number): string,
11
11
  toLogLevel(name: N): number,
12
- getLogger(id: string, options?: LoggerOptions): Logger<N | LogMethodNames>
12
+ getLogger(id: string, options?: LoggerOptions): Logger<N | LogMethodNames>,
13
+ getNonConsoleLogger(id: string, options?: LoggerOptions): Logger<N | LogMethodNames>
13
14
  }
14
15
 
15
16
  export interface StandardLog<N extends string = LogMethodNames> extends Readonly<StandardLogInstance<N>> {
@@ -19,15 +20,26 @@ export function createStandardLog<N extends string = LogMethodNames>(
19
20
  options?: StandardLogOptions<N>
20
21
  ): StandardLog<N> {
21
22
  const closure = createStandardLogClosure(options)
23
+ if (options?.reporters?.some(r => !r.id)) {
24
+ const log = closure.standardLog.getLogger('standard-log')
25
+ 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.`)
26
+ }
27
+
22
28
  if (!ctx.gsl) ctx.gsl = closure as any
23
29
  return closure.standardLog
24
30
  }
25
31
 
32
+ function noop() { }
33
+
26
34
  export function createStandardLogClosure<N extends string = LogMethodNames>(
27
35
  options?: StandardLogOptions<N>
28
36
  ): { store: LogStore, standardLog: StandardLog<N> } {
37
+ // TODO reporter.id must not be empty
29
38
  const store = createLogStore(required({ logLevel: logLevels.info }, options))
30
-
39
+ const nonConsoleReporters = options?.reporters?.filter(r => !r.isConsoleReporter).map(r => r.id) ?? []
40
+ const logMethods = DEFAULT_LOG_METHOD_NAMES
41
+ .concat(Object.keys(options?.customLevels ?? {}))
42
+ .concat(['on', 'count', 'write'])
31
43
  return {
32
44
  store,
33
45
  standardLog: {
@@ -42,19 +54,45 @@ export function createStandardLogClosure<N extends string = LogMethodNames>(
42
54
  getLogger(id: string, options?: LoggerOptions) {
43
55
  if (store.loggers[id]) return store.loggers[id] as any
44
56
  return store.loggers[id] = createLogger(store, id, { ssf: this.getLogger, ...options }) as any
45
- }
57
+ },
58
+ getNonConsoleLogger(id, options?) {
59
+ if (store.nonConsoleLoggers[id]) return store.nonConsoleLoggers[id] as any
60
+
61
+ if (store.loggers[id]) {
62
+ const log = this.getLogger('standard-log')
63
+ log.warn(`attempt to create a non-console logger with existing id ('${id}'), ignoring`)
64
+ return store.nonConsoleLoggers[id] = createDummyLogger(logMethods)
65
+ }
66
+
67
+ return store.nonConsoleLoggers[id] = nonConsoleReporters.length === 0
68
+ ? createDummyLogger(logMethods)
69
+ : createLogger(store, id, {
70
+ ssf: this.getNonConsoleLogger,
71
+ ...options,
72
+ writeTo(reporterId) {
73
+ return nonConsoleReporters.indexOf(reporterId) >= 0
74
+ }
75
+ })
76
+ },
46
77
  }
47
78
  }
48
79
  }
49
80
 
81
+ function createDummyLogger(logMethods: string[]) {
82
+ return logMethods.reduce((p, m) => {
83
+ p[m] = noop
84
+ return p
85
+ }, record()) as Logger
86
+ }
87
+
50
88
  export type StandardLogForTest<N extends string = LogMethodNames> = StandardLog<N> & { reporter: MemoryLogReporter }
51
89
 
52
90
  export function createStandardLogForTest<N extends string = LogMethodNames>(
53
91
  logLevel: LogLevel = logLevels.debug
54
92
  ): StandardLogForTest<N> {
55
93
  const reporter = createMemoryLogReporter()
56
- const standardLog = createStandardLog<N>({ reporters: [reporter], logLevel })
57
- return { ...standardLog, reporter }
94
+ const closure = createStandardLogClosure<N>({ reporters: [reporter], logLevel })
95
+ return { ...closure.standardLog, reporter }
58
96
  }
59
97
 
60
98
  /**
@@ -86,8 +124,8 @@ function getGlobalSL<N extends string = LogMethodNames>(): StandardLogInstance<N
86
124
 
87
125
  export function configGlobal(options: Omit<StandardLogOptions, 'customLevels'>) {
88
126
  if (ctx.configured) {
89
- const log = ctx.gsl?.standardLog.getLogger('standard-log')
90
- log?.warn('configGlobal() is being called more than once. Please make sure this is expected. Application should use `createStandardLog()` most of the time.')
127
+ const log = ctx.gsl!.standardLog.getLogger('standard-log')
128
+ log.warn('configGlobal() is being called more than once. Please make sure this is expected. Application should use `createStandardLog()` most of the time.')
91
129
  }
92
130
 
93
131
  if (ctx.gsl) {