standard-log 11.5.0 → 11.5.2
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.
- package/cjs/console.d.ts +3 -3
- package/cjs/console.d.ts.map +1 -1
- package/cjs/console.js +17 -8
- package/cjs/console.js.map +1 -1
- package/cjs/errors.d.ts.map +1 -1
- package/cjs/errors.js.map +1 -1
- package/cjs/formatter.d.ts +1 -1
- package/cjs/formatter.d.ts.map +1 -1
- package/cjs/formatter.js.map +1 -1
- package/cjs/index.d.ts.map +1 -1
- package/cjs/index.js.map +1 -1
- package/cjs/logLevels.d.ts.map +1 -1
- package/cjs/logLevels.js.map +1 -1
- package/cjs/logStore.d.ts.map +1 -1
- package/cjs/logStore.js.map +1 -1
- package/cjs/logger.d.ts.map +1 -1
- package/cjs/logger.js +15 -13
- package/cjs/logger.js.map +1 -1
- package/cjs/memory.d.ts +2 -2
- package/cjs/memory.d.ts.map +1 -1
- package/cjs/memory.js +15 -8
- package/cjs/memory.js.map +1 -1
- package/cjs/platform.d.ts.map +1 -1
- package/cjs/platform.js +1 -2
- package/cjs/platform.js.map +1 -1
- package/cjs/standardLog.ctx.d.ts.map +1 -1
- package/cjs/standardLog.ctx.js.map +1 -1
- package/cjs/standardLog.d.ts +2 -2
- package/cjs/standardLog.d.ts.map +1 -1
- package/cjs/standardLog.js +19 -14
- package/cjs/standardLog.js.map +1 -1
- package/cjs/types.d.ts +12 -12
- package/cjs/types.d.ts.map +1 -1
- package/cjs/types.js.map +1 -1
- package/dist/standard-log.es5.js +1190 -801
- package/dist/standard-log.es5.js.gz +0 -0
- package/dist/standard-log.es5.js.map +1 -1
- package/dist/standard-log.es5.js.map.gz +0 -0
- package/dist/standard-log.esm.js +173 -119
- package/dist/standard-log.esm.js.gz +0 -0
- package/dist/standard-log.esm.js.map +1 -1
- package/dist/standard-log.esm.js.map.gz +0 -0
- package/esm/console.d.ts +3 -3
- package/esm/console.d.ts.map +1 -1
- package/esm/console.js +20 -9
- package/esm/console.js.map +1 -1
- package/esm/errors.d.ts.map +1 -1
- package/esm/errors.js.map +1 -1
- package/esm/formatter.d.ts +1 -1
- package/esm/formatter.d.ts.map +1 -1
- package/esm/formatter.js.map +1 -1
- package/esm/index.d.ts.map +1 -1
- package/esm/index.js.map +1 -1
- package/esm/logLevels.d.ts.map +1 -1
- package/esm/logLevels.js.map +1 -1
- package/esm/logStore.d.ts.map +1 -1
- package/esm/logStore.js.map +1 -1
- package/esm/logger.d.ts.map +1 -1
- package/esm/logger.js +1 -1
- package/esm/logger.js.map +1 -1
- package/esm/memory.d.ts +2 -2
- package/esm/memory.d.ts.map +1 -1
- package/esm/memory.js +15 -8
- package/esm/memory.js.map +1 -1
- package/esm/platform.d.ts.map +1 -1
- package/esm/platform.js +1 -2
- package/esm/platform.js.map +1 -1
- package/esm/standardLog.ctx.d.ts.map +1 -1
- package/esm/standardLog.ctx.js.map +1 -1
- package/esm/standardLog.d.ts +2 -2
- package/esm/standardLog.d.ts.map +1 -1
- package/esm/standardLog.js +23 -18
- package/esm/standardLog.js.map +1 -1
- package/esm/types.d.ts +12 -12
- package/esm/types.d.ts.map +1 -1
- package/esm/types.js.map +1 -1
- package/package.json +17 -9
- package/ts/console.ts +54 -45
- package/ts/errors.ts +6 -6
- package/ts/formatter.ts +18 -18
- package/ts/index.ts +21 -6
- package/ts/logLevels.ts +90 -91
- package/ts/logStore.ts +46 -37
- package/ts/logger.ts +67 -57
- package/ts/memory.ts +66 -54
- package/ts/platform.ts +2 -2
- package/ts/standardLog.ctx.ts +2 -2
- package/ts/standardLog.ts +113 -94
- package/ts/types.ts +72 -62
package/ts/logger.ts
CHANGED
|
@@ -4,71 +4,81 @@ import type { LogEntry, LogFunction, Logger, LoggerOptions, LogMethodNames, LogR
|
|
|
4
4
|
import { LogLevel } from './types.js'
|
|
5
5
|
|
|
6
6
|
export function createLogger<N extends string = LogMethodNames>(
|
|
7
|
-
|
|
7
|
+
store: LogStore,
|
|
8
|
+
id: string,
|
|
9
|
+
options?: LoggerOptions
|
|
8
10
|
): Logger<N | LogMethodNames> {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
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
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
+
function write(entry: LogEntry) {
|
|
23
|
+
writeToReporters(reporters, entry, filter)
|
|
24
|
+
}
|
|
22
25
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
26
|
+
const logger = {
|
|
27
|
+
level: options?.level,
|
|
28
|
+
write
|
|
29
|
+
} as any
|
|
27
30
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
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
|
-
|
|
73
|
+
return id.replace(/[`~!#$%^&*()=+[\]{},|<>?]/g, '-')
|
|
68
74
|
}
|
|
69
75
|
|
|
70
|
-
function writeToReporters(
|
|
71
|
-
|
|
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
|
-
|
|
90
|
+
return targetLevel <= (loggerLevel !== undefined ? loggerLevel : store.logLevel)
|
|
81
91
|
}
|
package/ts/memory.ts
CHANGED
|
@@ -5,71 +5,83 @@ import type { LogEntry, LogFormatter, LogReporter, LogReporterOptions } from './
|
|
|
5
5
|
import { createConsoleLogReporter } from './console.js'
|
|
6
6
|
|
|
7
7
|
export type MemoryLogReporter = LogReporter<LogEntry> & {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
8
|
+
logs: LogEntry[]
|
|
9
|
+
/**
|
|
10
|
+
* Gets a simple log message for testing.
|
|
11
|
+
* The message only contains arguments for each log entry.
|
|
12
|
+
*/
|
|
13
|
+
getLogMessage(): string
|
|
14
|
+
getLogMessages(): string[]
|
|
15
|
+
getLogMessageWithLevel(): string
|
|
16
|
+
getLogMessagesWithIdAndLevel(): string[]
|
|
17
|
+
emit(): void
|
|
18
18
|
}
|
|
19
19
|
|
|
20
20
|
export type MemoryLogFormatter = LogFormatter<LogEntry>
|
|
21
21
|
|
|
22
22
|
export function toMessageWithLevel(logs: LogEntry[]) {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
23
|
+
return (
|
|
24
|
+
logs
|
|
25
|
+
.map(toInspectLogEntry)
|
|
26
|
+
.map(log => `${formatLogLevel(log.level)} ${log.args.join(' ')}`)
|
|
27
|
+
.join('\n')
|
|
28
|
+
// if it has more than this log,
|
|
29
|
+
// we assume it is running under tests,
|
|
30
|
+
// and remove this log to simplify testing.
|
|
31
|
+
.replace(/^\(WARN\) already configured for test, ignoring config\(\) call\n/, '')
|
|
32
|
+
)
|
|
30
33
|
}
|
|
31
34
|
|
|
32
35
|
export function createMemoryLogReporter(options?: LogReporterOptions<LogEntry>): MemoryLogReporter {
|
|
33
|
-
|
|
36
|
+
return Object.freeze(buildMemoryReporter(unpartial({ id: 'memory' }, options)))
|
|
34
37
|
}
|
|
35
38
|
|
|
36
39
|
function buildMemoryReporter(options?: LogReporterOptions<LogEntry>) {
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
40
|
+
const { id, formatter, filter } = required({ id: 'memory', formatter: (e: LogEntry) => e }, options)
|
|
41
|
+
const logs: LogEntry[] = []
|
|
42
|
+
return {
|
|
43
|
+
id,
|
|
44
|
+
get formatter() {
|
|
45
|
+
return formatter
|
|
46
|
+
},
|
|
47
|
+
get filter() {
|
|
48
|
+
return filter
|
|
49
|
+
},
|
|
50
|
+
logs,
|
|
51
|
+
write(entry: LogEntry) {
|
|
52
|
+
if (filter && !filter(entry)) return
|
|
53
|
+
logs.push(formatter(entry))
|
|
54
|
+
},
|
|
55
|
+
getLogMessage() {
|
|
56
|
+
return this.getLogMessages().join('\n')
|
|
57
|
+
},
|
|
58
|
+
getLogMessages() {
|
|
59
|
+
return logs.map(toInspectLogEntry).map(log => log.args.join(' '))
|
|
60
|
+
},
|
|
61
|
+
getLogMessagesWithIdAndLevel() {
|
|
62
|
+
return logs
|
|
63
|
+
.map(toInspectLogEntry)
|
|
64
|
+
.map(log => `${log.id} ${formatLogLevel(log.level)} ${log.args.join(' ')}`)
|
|
65
|
+
},
|
|
66
|
+
getLogMessageWithLevel() {
|
|
67
|
+
return toMessageWithLevel(logs)
|
|
68
|
+
},
|
|
69
|
+
/**
|
|
70
|
+
* emit the saved log to console for easy debugging
|
|
71
|
+
*/
|
|
72
|
+
emit() {
|
|
73
|
+
const reporter = createConsoleLogReporter()
|
|
74
|
+
logs.forEach(l => reporter.write(l))
|
|
75
|
+
}
|
|
76
|
+
}
|
|
68
77
|
}
|
|
69
78
|
|
|
70
|
-
export function createMemoryWithConsoleLogReporter(
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
79
|
+
export function createMemoryWithConsoleLogReporter(
|
|
80
|
+
options?: LogReporterOptions<LogEntry>
|
|
81
|
+
): MemoryLogReporter {
|
|
82
|
+
return Object.freeze(
|
|
83
|
+
Object.assign(buildMemoryReporter(unpartial({ id: 'memory-with-console' }, options)), {
|
|
84
|
+
isConsoleReporter: true
|
|
85
|
+
})
|
|
86
|
+
)
|
|
75
87
|
}
|
package/ts/platform.ts
CHANGED
|
@@ -3,7 +3,7 @@ import { inspect } from 'util'
|
|
|
3
3
|
import type { ConsoleLike, LogEntry } from './types.js'
|
|
4
4
|
|
|
5
5
|
export const toInspectLogEntry = (e: LogEntry) => (
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
(e.args = e.args.map(value => (typeof value === 'object' && value !== null ? inspect(value) : value))), e
|
|
7
|
+
)
|
|
8
8
|
|
|
9
9
|
export const polyfill: { console?: ConsoleLike } = record()
|
package/ts/standardLog.ctx.ts
CHANGED
|
@@ -3,6 +3,6 @@ import type { LogStore } from './logStore.js'
|
|
|
3
3
|
import type { LogMethodNames, StandardLogInstance } from './types.js'
|
|
4
4
|
|
|
5
5
|
export const ctx: {
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
configured?: boolean
|
|
7
|
+
gsl?: { store: LogStore; standardLog: StandardLogInstance<LogMethodNames> }
|
|
8
8
|
} = record()
|
package/ts/standardLog.ts
CHANGED
|
@@ -4,88 +4,103 @@ 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 { ctx } from './standardLog.ctx.js'
|
|
7
|
-
import type {
|
|
7
|
+
import type {
|
|
8
|
+
Logger,
|
|
9
|
+
LoggerOptions,
|
|
10
|
+
LogLevel,
|
|
11
|
+
LogMethodNames,
|
|
12
|
+
StandardLogInstance,
|
|
13
|
+
StandardLogOptions
|
|
14
|
+
} from './types.js'
|
|
8
15
|
|
|
9
|
-
export interface StandardLog<N extends string = LogMethodNames> extends Readonly<StandardLogInstance<N>> {
|
|
10
|
-
}
|
|
16
|
+
export interface StandardLog<N extends string = LogMethodNames> extends Readonly<StandardLogInstance<N>> {}
|
|
11
17
|
|
|
12
18
|
export function createStandardLog<N extends string = LogMethodNames>(
|
|
13
|
-
|
|
19
|
+
options?: StandardLogOptions<N>
|
|
14
20
|
): StandardLog<N> {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
21
|
+
const closure = createStandardLogClosure(options)
|
|
22
|
+
if (options?.reporters?.some(r => !r.id)) {
|
|
23
|
+
const log = closure.standardLog.getLogger('standard-log')
|
|
24
|
+
log.critical(
|
|
25
|
+
`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
|
+
}
|
|
28
|
+
|
|
29
|
+
if (!ctx.gsl) ctx.gsl = closure as any
|
|
30
|
+
return closure.standardLog
|
|
23
31
|
}
|
|
24
32
|
|
|
25
|
-
function noop() {
|
|
33
|
+
function noop() {}
|
|
26
34
|
|
|
27
35
|
export function createStandardLogClosure<N extends string = LogMethodNames>(
|
|
28
|
-
|
|
29
|
-
): { store: LogStore
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
36
|
+
options?: StandardLogOptions<N>
|
|
37
|
+
): { store: LogStore; standardLog: StandardLog<N> } {
|
|
38
|
+
// TODO reporter.id must not be empty
|
|
39
|
+
const store = createLogStore(required({ logLevel: logLevels.info }, options))
|
|
40
|
+
const nonConsoleReporters = options?.reporters?.filter(r => !r.isConsoleReporter).map(r => r.id) ?? []
|
|
41
|
+
const logMethods = DEFAULT_LOG_METHOD_NAMES.concat(Object.keys(options?.customLevels ?? {})).concat([
|
|
42
|
+
'on',
|
|
43
|
+
'count',
|
|
44
|
+
'write'
|
|
45
|
+
])
|
|
46
|
+
return {
|
|
47
|
+
store,
|
|
48
|
+
standardLog: {
|
|
49
|
+
get logLevel() {
|
|
50
|
+
return store.logLevel
|
|
51
|
+
},
|
|
52
|
+
|
|
53
|
+
toLogLevelName(level: number) {
|
|
54
|
+
return store.logLevelStore.getName(level)
|
|
55
|
+
},
|
|
56
|
+
toLogLevel(name: string) {
|
|
57
|
+
return store.logLevelStore.getLevel(name)
|
|
58
|
+
},
|
|
59
|
+
getLogger(id: string, options?: LoggerOptions) {
|
|
60
|
+
if (store.loggers[id]) return store.loggers[id] as any
|
|
61
|
+
return (store.loggers[id] = createLogger(store, id, { ssf: this.getLogger, ...options }) as any)
|
|
62
|
+
},
|
|
63
|
+
getNonConsoleLogger(id, options?) {
|
|
64
|
+
if (store.nonConsoleLoggers[id]) return store.nonConsoleLoggers[id] as any
|
|
65
|
+
|
|
66
|
+
if (store.loggers[id]) {
|
|
67
|
+
const log = this.getLogger('standard-log')
|
|
68
|
+
log.warn(`attempt to create a non-console logger with existing id ('${id}'), ignoring`)
|
|
69
|
+
return (store.nonConsoleLoggers[id] = createDummyLogger(logMethods))
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return (store.nonConsoleLoggers[id] =
|
|
73
|
+
nonConsoleReporters.length === 0
|
|
74
|
+
? createDummyLogger(logMethods)
|
|
75
|
+
: createLogger(store, id, {
|
|
76
|
+
ssf: this.getNonConsoleLogger,
|
|
77
|
+
...options,
|
|
78
|
+
writeTo(reporterId) {
|
|
79
|
+
return nonConsoleReporters.indexOf(reporterId) >= 0
|
|
80
|
+
}
|
|
81
|
+
}))
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
72
85
|
}
|
|
73
86
|
|
|
74
87
|
function createDummyLogger(logMethods: string[]) {
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
88
|
+
return logMethods.reduce((p, m) => {
|
|
89
|
+
p[m] = noop
|
|
90
|
+
return p
|
|
91
|
+
}, record()) as Logger
|
|
79
92
|
}
|
|
80
93
|
|
|
81
|
-
export type StandardLogForTest<N extends string = LogMethodNames> = StandardLog<N> & {
|
|
94
|
+
export type StandardLogForTest<N extends string = LogMethodNames> = StandardLog<N> & {
|
|
95
|
+
reporter: MemoryLogReporter
|
|
96
|
+
}
|
|
82
97
|
|
|
83
98
|
export function createStandardLogForTest<N extends string = LogMethodNames>(
|
|
84
|
-
|
|
99
|
+
logLevel: LogLevel = logLevels.debug
|
|
85
100
|
): StandardLogForTest<N> {
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
101
|
+
const reporter = createMemoryLogReporter()
|
|
102
|
+
const closure = createStandardLogClosure<N>({ reporters: [reporter], logLevel })
|
|
103
|
+
return { ...closure.standardLog, reporter }
|
|
89
104
|
}
|
|
90
105
|
|
|
91
106
|
/**
|
|
@@ -93,43 +108,47 @@ export function createStandardLogForTest<N extends string = LogMethodNames>(
|
|
|
93
108
|
*/
|
|
94
109
|
export function suppressLogs<R>(block: () => R, log: Logger, ...logs: Logger[]): R
|
|
95
110
|
export function suppressLogs<R>(block: () => R, ...logs: Logger[]): R {
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
111
|
+
const origLevels = logs.map(l => l.level)
|
|
112
|
+
logs.forEach(l => (l.level = logLevels.none))
|
|
113
|
+
const result = block()
|
|
114
|
+
logs.forEach((l, i) => (l.level = origLevels[i]))
|
|
115
|
+
return result
|
|
101
116
|
}
|
|
102
117
|
|
|
103
|
-
export type GetLogger<N extends string = LogMethodNames> = (
|
|
118
|
+
export type GetLogger<N extends string = LogMethodNames> = (
|
|
119
|
+
id: string,
|
|
120
|
+
options?: LoggerOptions
|
|
121
|
+
) => Logger<LogMethodNames | N>
|
|
104
122
|
|
|
105
123
|
export function getLogger<N extends string = LogMethodNames>(id: string, options?: LoggerOptions) {
|
|
106
|
-
|
|
124
|
+
return getGlobalSL<N>().getLogger(id, options)
|
|
107
125
|
}
|
|
108
126
|
|
|
109
127
|
function getGlobalSL<N extends string = LogMethodNames>(): StandardLogInstance<N> {
|
|
110
|
-
|
|
128
|
+
if (!ctx.gsl) ctx.gsl = createStandardLogClosure()
|
|
111
129
|
|
|
112
|
-
|
|
130
|
+
return ctx.gsl!.standardLog as StandardLogInstance<N>
|
|
113
131
|
}
|
|
114
132
|
|
|
115
133
|
export function configGlobal(options: Omit<StandardLogOptions, 'customLevels'>) {
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
134
|
+
if (ctx.configured) {
|
|
135
|
+
const log = ctx.gsl!.standardLog.getLogger('standard-log')
|
|
136
|
+
log.warn(
|
|
137
|
+
'configGlobal() is being called more than once. Please make sure this is expected. Application should use `createStandardLog()` most of the time.'
|
|
138
|
+
)
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
if (ctx.gsl) {
|
|
142
|
+
if (options.logLevel) {
|
|
143
|
+
ctx.gsl.store.logLevel = options.logLevel
|
|
144
|
+
}
|
|
145
|
+
if (options.reporters) {
|
|
146
|
+
const reporters = ctx.gsl.store.reporters
|
|
147
|
+
reporters.length = 0
|
|
148
|
+
reporters.push(...options.reporters)
|
|
149
|
+
}
|
|
150
|
+
} else {
|
|
151
|
+
ctx.gsl = createStandardLogClosure(options)
|
|
152
|
+
}
|
|
153
|
+
ctx.configured = true
|
|
135
154
|
}
|