standard-log 9.1.0 → 9.1.3
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/README.md +35 -5
- package/cjs/console.spec.js +1 -1
- package/cjs/console.spec.js.map +1 -1
- package/cjs/index.d.ts +2 -2
- package/cjs/index.d.ts.map +1 -1
- package/cjs/index.js +4 -4
- package/cjs/index.js.map +1 -1
- package/cjs/logger.d.ts +1 -2
- package/cjs/logger.d.ts.map +1 -1
- package/cjs/logger.js +6 -7
- package/cjs/logger.js.map +1 -1
- package/cjs/standardLog.d.ts +11 -3
- package/cjs/standardLog.d.ts.map +1 -1
- package/cjs/standardLog.js +40 -5
- package/cjs/standardLog.js.map +1 -1
- package/cjs/standardLog.spec.js +133 -85
- package/cjs/standardLog.spec.js.map +1 -1
- package/cjs/types.d.ts +2 -1
- package/cjs/types.d.ts.map +1 -1
- package/cjs/types.js.map +1 -1
- package/dist/standard-log.es5.js +50 -59
- 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 +55 -65
- 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.spec.js +1 -1
- package/esm/console.spec.js.map +1 -1
- package/esm/index.d.ts +2 -2
- package/esm/index.d.ts.map +1 -1
- package/esm/index.js +1 -2
- package/esm/index.js.map +1 -1
- package/esm/logger.d.ts +1 -2
- package/esm/logger.d.ts.map +1 -1
- package/esm/logger.js +2 -2
- package/esm/logger.js.map +1 -1
- package/esm/standardLog.d.ts +11 -3
- package/esm/standardLog.d.ts.map +1 -1
- package/esm/standardLog.js +37 -4
- package/esm/standardLog.js.map +1 -1
- package/esm/standardLog.spec.js +93 -46
- package/esm/standardLog.spec.js.map +1 -1
- package/esm/types.d.ts +2 -1
- package/esm/types.d.ts.map +1 -1
- package/esm/types.js.map +1 -1
- package/package.json +1 -1
- package/ts/index.ts +3 -3
- package/ts/logger.ts +2 -3
- package/ts/standardLog.ts +44 -6
- package/ts/types.ts +3 -1
- package/cjs/global.d.ts +0 -13
- package/cjs/global.d.ts.map +0 -1
- package/cjs/global.js +0 -33
- package/cjs/global.js.map +0 -1
- package/cjs/global.spec.d.ts +0 -2
- package/cjs/global.spec.d.ts.map +0 -1
- package/cjs/global.spec.js +0 -32
- package/cjs/global.spec.js.map +0 -1
- package/esm/global.d.ts +0 -13
- package/esm/global.d.ts.map +0 -1
- package/esm/global.js +0 -28
- package/esm/global.js.map +0 -1
- package/esm/global.spec.d.ts +0 -2
- package/esm/global.spec.d.ts.map +0 -1
- package/esm/global.spec.js +0 -30
- package/esm/global.spec.js.map +0 -1
- package/ts/global.ts +0 -34
package/esm/standardLog.js
CHANGED
|
@@ -1,10 +1,13 @@
|
|
|
1
|
-
import { required } from 'type-plus';
|
|
1
|
+
import { record, required } from 'type-plus';
|
|
2
2
|
import { createLogger } from './logger.js';
|
|
3
3
|
import { logLevels } from './logLevels.js';
|
|
4
4
|
import { createLogStore } from './logStore.js';
|
|
5
5
|
import { createMemoryLogReporter } from './memory.js';
|
|
6
6
|
export function createStandardLog(options) {
|
|
7
|
-
|
|
7
|
+
const closure = createStandardLogClosure(options);
|
|
8
|
+
if (!ctx.gsl)
|
|
9
|
+
ctx.gsl = closure;
|
|
10
|
+
return closure.standardLog;
|
|
8
11
|
}
|
|
9
12
|
export function createStandardLogClosure(options) {
|
|
10
13
|
const store = createLogStore(required({ logLevel: logLevels.info }, options));
|
|
@@ -18,10 +21,10 @@ export function createStandardLogClosure(options) {
|
|
|
18
21
|
toLogLevel(name) {
|
|
19
22
|
return store.logLevelStore.getLevel(name);
|
|
20
23
|
},
|
|
21
|
-
getLogger(
|
|
24
|
+
getLogger(id, options) {
|
|
22
25
|
if (store.loggers[id])
|
|
23
26
|
return store.loggers[id];
|
|
24
|
-
return store.loggers[id] = createLogger(
|
|
27
|
+
return store.loggers[id] = createLogger(store, id, { ssf: this.getLogger, ...options });
|
|
25
28
|
}
|
|
26
29
|
}
|
|
27
30
|
};
|
|
@@ -38,4 +41,34 @@ export function suppressLogs(block, ...logs) {
|
|
|
38
41
|
logs.forEach((l, i) => l.level = origLevels[i]);
|
|
39
42
|
return result;
|
|
40
43
|
}
|
|
44
|
+
export function getLogger(id, options) {
|
|
45
|
+
return getGlobalSL().getLogger(id, options);
|
|
46
|
+
}
|
|
47
|
+
export const ctx = record();
|
|
48
|
+
function getGlobalSL() {
|
|
49
|
+
if (!ctx.gsl)
|
|
50
|
+
ctx.gsl = createStandardLogClosure();
|
|
51
|
+
return ctx.gsl.standardLog;
|
|
52
|
+
}
|
|
53
|
+
export function configGlobal(options) {
|
|
54
|
+
if (ctx.configured) {
|
|
55
|
+
const log = ctx.gsl?.standardLog.getLogger('standard-log');
|
|
56
|
+
log?.warn('configGlobal() is being called more than once. Please make sure this is expected. Application should use `createStandardLog()` most of the time.');
|
|
57
|
+
}
|
|
58
|
+
if (ctx.gsl) {
|
|
59
|
+
if (options.logLevel) {
|
|
60
|
+
ctx.gsl.store.logLevel = options.logLevel;
|
|
61
|
+
}
|
|
62
|
+
if (options.reporters) {
|
|
63
|
+
const reporters = ctx.gsl.store.reporters;
|
|
64
|
+
while (reporters.length > 0)
|
|
65
|
+
reporters.pop();
|
|
66
|
+
reporters.push(...options.reporters);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
else {
|
|
70
|
+
ctx.gsl = createStandardLogClosure(options);
|
|
71
|
+
}
|
|
72
|
+
ctx.configured = true;
|
|
73
|
+
}
|
|
41
74
|
//# sourceMappingURL=standardLog.js.map
|
package/esm/standardLog.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"standardLog.js","sourceRoot":"","sources":["../ts/standardLog.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"standardLog.js","sourceRoot":"","sources":["../ts/standardLog.ts"],"names":[],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C,OAAiD;IAC5E,IAAI,GAAG,CAAC,UAAU,EAAE;QAClB,MAAM,GAAG,GAAG,GAAG,CAAC,GAAG,EAAE,WAAW,CAAC,SAAS,CAAC,cAAc,CAAC,CAAA;QAC1D,GAAG,EAAE,IAAI,CAAC,kJAAkJ,CAAC,CAAA;KAC9J;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,OAAO,SAAS,CAAC,MAAM,GAAG,CAAC;gBAAE,SAAS,CAAC,GAAG,EAAE,CAAA;YAC5C,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 { logLevels } from './logLevels.js'\nimport { createLogStore, LogStore } from './logStore.js'\nimport { createMemoryLogReporter } from './memory.js'\nimport type { Logger, LoggerOptions, LogLevel, LogMethodNames, StandardLogOptions } from './types.js'\n\nexport interface StandardLogInstance<N extends string = LogMethodNames> {\n logLevel: number,\n toLogLevelName(level: number): string,\n toLogLevel(name: N): number,\n getLogger(id: string, options?: LoggerOptions): Logger<N | LogMethodNames>\n}\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 (!ctx.gsl) ctx.gsl = closure as any\n return closure.standardLog\n}\n\nexport function createStandardLogClosure<N extends string = LogMethodNames>(\n options?: StandardLogOptions<N>\n): { store: LogStore, standardLog: StandardLog<N> } {\n const store = createLogStore(required({ logLevel: logLevels.info }, options))\n\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 }\n }\n}\n\nexport function createStandardLogForTest(logLevel: LogLevel = logLevels.debug) {\n const reporter = createMemoryLogReporter()\n const standardLog = createStandardLog({ reporters: [reporter], logLevel })\n return { ...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 function getLogger<N extends string = LogMethodNames>(id: string, options?: LoggerOptions) {\n return getGlobalSL<N>().getLogger(id, options)\n}\n\nexport const ctx: {\n configured?: boolean,\n gsl?: { store: LogStore, standardLog: StandardLogInstance<LogMethodNames> }\n} = record()\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 while (reporters.length > 0) reporters.pop()\n reporters.push(...options.reporters)\n }\n }\n else {\n ctx.gsl = createStandardLogClosure(options)\n }\n ctx.configured = true\n}\n"]}
|
package/esm/standardLog.spec.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { assertron as a } from 'assertron';
|
|
2
2
|
import { InvalidId } from './errors.js';
|
|
3
|
+
import { createStandardLog, createStandardLogForTest, getLogger, suppressLogs } from './index.js';
|
|
3
4
|
import { logLevels, toLogLevelName } from './logLevels.js';
|
|
4
5
|
import { createMemoryLogReporter } from './memory.js';
|
|
5
|
-
import {
|
|
6
|
+
import { configGlobal, ctx } from './standardLog.js';
|
|
6
7
|
import { assertSSF, wrapTest } from './testUtil.js';
|
|
7
8
|
describe('createStandardLog()', () => {
|
|
8
9
|
describe('logLevel', () => {
|
|
@@ -29,11 +30,21 @@ describe('createStandardLog()', () => {
|
|
|
29
30
|
it('can specify reporter', () => {
|
|
30
31
|
const mem = createMemoryLogReporter();
|
|
31
32
|
const sl = createStandardLog({ reporters: [mem] });
|
|
32
|
-
const log = sl.getLogger(
|
|
33
|
+
const log = sl.getLogger('x');
|
|
33
34
|
log.info('abc');
|
|
34
35
|
expect(mem.logs.length).toBe(1);
|
|
35
36
|
});
|
|
36
37
|
});
|
|
38
|
+
describe('related to global', () => {
|
|
39
|
+
beforeEach(() => {
|
|
40
|
+
ctx.gsl = undefined;
|
|
41
|
+
ctx.configured = false;
|
|
42
|
+
});
|
|
43
|
+
it('will affect getLogger() if configGlobal() is not called', () => {
|
|
44
|
+
createStandardLog({ logLevel: logLevels.planck });
|
|
45
|
+
expect(ctx.gsl?.standardLog.logLevel).toBe(logLevels.planck);
|
|
46
|
+
});
|
|
47
|
+
});
|
|
37
48
|
});
|
|
38
49
|
describe('createStandardLogForTest()', () => {
|
|
39
50
|
it('returns with memory reporter', () => {
|
|
@@ -45,103 +56,103 @@ describe('createStandardLogForTest()', () => {
|
|
|
45
56
|
expect(store.logLevel).toBe(logLevels.debug);
|
|
46
57
|
});
|
|
47
58
|
});
|
|
48
|
-
describe('getLogger()', () => {
|
|
59
|
+
describe('standardLog.getLogger()', () => {
|
|
49
60
|
it('supports id with alphanumeric and :_-.', () => {
|
|
50
61
|
const sl = createStandardLog();
|
|
51
|
-
sl.getLogger(
|
|
62
|
+
sl.getLogger('abcdefghijklmnopqrstuvwxyz:-_.1234567890');
|
|
52
63
|
});
|
|
53
64
|
it('supports id with unicode', () => {
|
|
54
65
|
const sl = createStandardLog();
|
|
55
|
-
sl.getLogger(
|
|
66
|
+
sl.getLogger('ミク香港');
|
|
56
67
|
});
|
|
57
68
|
it('supports id with @, \\ and /', () => {
|
|
58
69
|
const sl = createStandardLog();
|
|
59
|
-
sl.getLogger(
|
|
60
|
-
sl.getLogger(
|
|
70
|
+
sl.getLogger('@unional/fixture');
|
|
71
|
+
sl.getLogger(`a\\b\\c`);
|
|
61
72
|
});
|
|
62
73
|
it.each('`~!#$%^&*()=+|[]{}<>,?'.split(''))('throws if id has unsupported character %s', (char) => {
|
|
63
74
|
const sl = createStandardLog();
|
|
64
|
-
a.throws(() => sl.getLogger(
|
|
75
|
+
a.throws(() => sl.getLogger(char), InvalidId);
|
|
65
76
|
});
|
|
66
77
|
it('throw InvalidId with ssf to the call site', () => {
|
|
67
78
|
const sl = createStandardLog();
|
|
68
|
-
const err = a.throws(() => sl.getLogger(
|
|
79
|
+
const err = a.throws(() => sl.getLogger('!'), InvalidId);
|
|
69
80
|
assertSSF(err, __filename);
|
|
70
81
|
});
|
|
71
82
|
test('id is readonly', () => {
|
|
72
83
|
const sl = createStandardLog();
|
|
73
|
-
const log = sl.getLogger(
|
|
84
|
+
const log = sl.getLogger('id is readonly');
|
|
74
85
|
a.throws(() => log.id = 'b');
|
|
75
86
|
});
|
|
76
87
|
test('get logger with same name gets the same instance', () => {
|
|
77
88
|
const sl = createStandardLog();
|
|
78
|
-
const expected = sl.getLogger(
|
|
79
|
-
const actual = sl.getLogger(
|
|
89
|
+
const expected = sl.getLogger('same-inst');
|
|
90
|
+
const actual = sl.getLogger('same-inst');
|
|
80
91
|
expect(actual).toBe(expected);
|
|
81
92
|
});
|
|
82
93
|
it('can specify local log level', () => {
|
|
83
94
|
const sl = createStandardLog();
|
|
84
|
-
const log = sl.getLogger(
|
|
95
|
+
const log = sl.getLogger('local', { level: logLevels.trace });
|
|
85
96
|
expect(log.level).toBe(logLevels.trace);
|
|
86
97
|
});
|
|
87
98
|
test('new logger will get custom level', () => {
|
|
88
99
|
const sl = createStandardLog({ customLevels: { 'to_logger': 1234 } });
|
|
89
|
-
const actual = sl.getLogger(
|
|
100
|
+
const actual = sl.getLogger('to_logger_logger');
|
|
90
101
|
expect(typeof actual.to_logger).toBe('function');
|
|
91
102
|
});
|
|
92
103
|
it('Logger have at least the default log method names', () => {
|
|
93
104
|
function wrapper(options) {
|
|
94
105
|
const sl = createStandardLog(options);
|
|
95
106
|
// since `N` is generic, just `Logger<N>` will not have the default log method names.
|
|
96
|
-
const log = sl.getLogger(
|
|
107
|
+
const log = sl.getLogger('local');
|
|
97
108
|
expect(log.info).toBeDefined();
|
|
98
109
|
return sl;
|
|
99
110
|
}
|
|
100
111
|
const sl = wrapper({ customLevels: { 'cust': 123 } });
|
|
101
|
-
const actual = sl.getLogger(
|
|
112
|
+
const actual = sl.getLogger('c');
|
|
102
113
|
expect(actual.cust).toBeDefined();
|
|
103
114
|
});
|
|
104
115
|
describe('log levels', () => {
|
|
105
116
|
async function assertLoggedAtLevel(sl, method, level) {
|
|
106
|
-
const log = sl.getLogger(
|
|
117
|
+
const log = sl.getLogger(method);
|
|
107
118
|
const msg = `should log on level: ${level}`;
|
|
108
119
|
log[method](msg);
|
|
109
120
|
a.satisfies(sl.reporter.logs, [{ id: method, level: sl.toLogLevel(method), args: [msg] }]);
|
|
110
121
|
}
|
|
111
122
|
async function assertNotLoggedAtLevel(sl, method, level) {
|
|
112
|
-
const log = sl.getLogger(
|
|
123
|
+
const log = sl.getLogger(method);
|
|
113
124
|
log[method](`should not log on level: ${level}`);
|
|
114
125
|
expect(sl.reporter.logs.length).toBe(0);
|
|
115
126
|
}
|
|
116
127
|
async function assertLoggedAtLocalLevel(sl, method, localLevel) {
|
|
117
|
-
const log = sl.getLogger(
|
|
128
|
+
const log = sl.getLogger(method, { level: localLevel });
|
|
118
129
|
const msg = `should log on level: ${localLevel}`;
|
|
119
130
|
log[method](msg);
|
|
120
131
|
a.satisfies(sl.reporter.logs, [{ id: log.id, level: sl.toLogLevel(method), args: [msg] }]);
|
|
121
132
|
}
|
|
122
133
|
async function assertNotLoggedAtLocalLevel(sl, method, localLevel) {
|
|
123
|
-
const log = sl.getLogger(
|
|
134
|
+
const log = sl.getLogger(method, { level: localLevel });
|
|
124
135
|
log[method](`should not log on level: ${localLevel}`);
|
|
125
136
|
expect(sl.reporter.logs.length).toBe(0);
|
|
126
137
|
}
|
|
127
138
|
async function assertLoggedAtCallLevel(sl, method, callLevel) {
|
|
128
|
-
const log = sl.getLogger(
|
|
139
|
+
const log = sl.getLogger(method);
|
|
129
140
|
let actual = false;
|
|
130
141
|
log.on(callLevel, () => actual = true);
|
|
131
142
|
expect(actual).toBe(true);
|
|
132
143
|
}
|
|
133
144
|
async function assertNotLoggedAtCallLevel(sl, method, callLevel, actualLevel) {
|
|
134
|
-
const log = sl.getLogger(
|
|
145
|
+
const log = sl.getLogger(method);
|
|
135
146
|
log.on(callLevel, () => { throw new Error(`should not log at level ${actualLevel}`); });
|
|
136
147
|
}
|
|
137
148
|
async function assertLoggedAtCallLevelOverrideLocalLevel(sl, method, localLevel, callLevel) {
|
|
138
|
-
const log = sl.getLogger(
|
|
149
|
+
const log = sl.getLogger(method, { level: localLevel });
|
|
139
150
|
let actual = false;
|
|
140
151
|
log.on(callLevel, () => actual = true);
|
|
141
152
|
expect(actual).toBe(true);
|
|
142
153
|
}
|
|
143
154
|
async function assertNotLoggedAtCallLevelOverrideLocalLevel(sl, method, localLevel, callLevel) {
|
|
144
|
-
const log = sl.getLogger(
|
|
155
|
+
const log = sl.getLogger(method, { level: localLevel });
|
|
145
156
|
log.on(callLevel, () => { throw new Error(`should not log at level ${localLevel}`); });
|
|
146
157
|
}
|
|
147
158
|
const shouldLog = wrapTest((test) => (method, level) => {
|
|
@@ -299,20 +310,20 @@ describe('getLogger()', () => {
|
|
|
299
310
|
});
|
|
300
311
|
it('writes entry directly', () => {
|
|
301
312
|
const sl = createStandardLogForTest();
|
|
302
|
-
const log = sl.getLogger(
|
|
313
|
+
const log = sl.getLogger('write-entry');
|
|
303
314
|
const entry = { id: 'write-entry', level: logLevels.info, args: ['abc'], timestamp: new Date() };
|
|
304
315
|
log.write(entry);
|
|
305
316
|
a.satisfies(sl.reporter.logs, [entry]);
|
|
306
317
|
});
|
|
307
318
|
test.each(['on', 'count', 'error', 'warn', 'info', 'debug'])('method %s is readonly', name => {
|
|
308
319
|
const sl = createStandardLog();
|
|
309
|
-
const log = sl.getLogger(
|
|
320
|
+
const log = sl.getLogger(`${name} is readonly`);
|
|
310
321
|
a.throws(() => log[name] = true);
|
|
311
322
|
});
|
|
312
323
|
describe('on()', () => {
|
|
313
324
|
it('log using log argument', () => {
|
|
314
325
|
const sl = createStandardLogForTest();
|
|
315
|
-
const logger = sl.getLogger(
|
|
326
|
+
const logger = sl.getLogger('log on fn');
|
|
316
327
|
logger.on('error', log => { log('value'); });
|
|
317
328
|
a.satisfies(sl.reporter.logs, [
|
|
318
329
|
{ id: 'log on fn', level: logLevels.error, args: ['value'] }
|
|
@@ -320,14 +331,14 @@ describe('getLogger()', () => {
|
|
|
320
331
|
});
|
|
321
332
|
it('can take log level name in first argument', () => {
|
|
322
333
|
const sl = createStandardLogForTest();
|
|
323
|
-
const log = sl.getLogger(
|
|
334
|
+
const log = sl.getLogger('string-on');
|
|
324
335
|
log.on('debug', () => { return; });
|
|
325
336
|
});
|
|
326
337
|
});
|
|
327
338
|
describe('count()', () => {
|
|
328
339
|
test('will increment the counter', async () => {
|
|
329
340
|
const sl = createStandardLogForTest();
|
|
330
|
-
const logger = sl.getLogger(
|
|
341
|
+
const logger = sl.getLogger('inc counter');
|
|
331
342
|
logger.count();
|
|
332
343
|
logger.count();
|
|
333
344
|
a.satisfies(sl.reporter.logs, [
|
|
@@ -338,7 +349,7 @@ describe('getLogger()', () => {
|
|
|
338
349
|
test('append args after the counter', async () => {
|
|
339
350
|
const sl = createStandardLogForTest(logLevels.debug);
|
|
340
351
|
const id = 'inc with args';
|
|
341
|
-
const logger = sl.getLogger(
|
|
352
|
+
const logger = sl.getLogger(id);
|
|
342
353
|
logger.count('msg1', 'msg2');
|
|
343
354
|
a.satisfies(sl.reporter.logs, [
|
|
344
355
|
{ id, level: logLevels.debug, args: [1, 'msg1', 'msg2'] }
|
|
@@ -347,7 +358,7 @@ describe('getLogger()', () => {
|
|
|
347
358
|
test('not log if log level is less than debug', async () => {
|
|
348
359
|
const sl = createStandardLogForTest(logLevels.debug - 1);
|
|
349
360
|
const id = 'inc with args';
|
|
350
|
-
const logger = sl.getLogger(
|
|
361
|
+
const logger = sl.getLogger(id);
|
|
351
362
|
logger.count('msg1', 'msg2');
|
|
352
363
|
expect(sl.reporter.logs.length).toBe(0);
|
|
353
364
|
});
|
|
@@ -355,41 +366,41 @@ describe('getLogger()', () => {
|
|
|
355
366
|
test('logger with custom level', async () => {
|
|
356
367
|
const mem = createMemoryLogReporter();
|
|
357
368
|
const sl = createStandardLog({ customLevels: { 'cust_lvl': 100 }, reporters: [mem] });
|
|
358
|
-
const logger = sl.getLogger(
|
|
369
|
+
const logger = sl.getLogger('cust');
|
|
359
370
|
logger.cust_lvl('a', 'b');
|
|
360
371
|
a.satisfies(mem.logs, [{ id: 'cust', level: 100, args: ['a', 'b'] }]);
|
|
361
372
|
});
|
|
362
373
|
describe('getLogger([,{ writeTo }])', () => {
|
|
363
|
-
function testWriteTo(
|
|
374
|
+
function testWriteTo(id, options, handler) {
|
|
364
375
|
const memReporter = createMemoryLogReporter();
|
|
365
376
|
const specialReporter = createMemoryLogReporter({ id: 'special' });
|
|
366
377
|
const sl = createStandardLog({ reporters: [memReporter, specialReporter] });
|
|
367
|
-
const log = sl.getLogger(
|
|
378
|
+
const log = sl.getLogger(id, options);
|
|
368
379
|
handler(log, memReporter.logs, specialReporter.logs, sl);
|
|
369
380
|
}
|
|
370
381
|
it('can specify which reporter to use by name', () => {
|
|
371
|
-
testWriteTo(
|
|
382
|
+
testWriteTo('writeTo-string', { writeTo: 'special' }, (log, memLogs, specialLogs) => {
|
|
372
383
|
log.error('error message');
|
|
373
384
|
expect(memLogs.length).toEqual(0);
|
|
374
385
|
expect(specialLogs.length).toEqual(1);
|
|
375
386
|
});
|
|
376
387
|
});
|
|
377
388
|
it('can specify which reporter to use by regex', () => {
|
|
378
|
-
testWriteTo(
|
|
389
|
+
testWriteTo('writeTo-regex', { writeTo: /^spec/ }, (log, memLogs, specialLogs) => {
|
|
379
390
|
log.error('error message');
|
|
380
391
|
expect(memLogs.length).toEqual(0);
|
|
381
392
|
expect(specialLogs.length).toEqual(1);
|
|
382
393
|
});
|
|
383
394
|
});
|
|
384
395
|
it('can specify which reporter to use by RegExp', () => {
|
|
385
|
-
testWriteTo(
|
|
396
|
+
testWriteTo('writeTo-RegExp', { writeTo: new RegExp('^spec') }, (log, memLogs, specialLogs) => {
|
|
386
397
|
log.error('error message');
|
|
387
398
|
expect(memLogs.length).toEqual(0);
|
|
388
399
|
expect(specialLogs.length).toEqual(1);
|
|
389
400
|
});
|
|
390
401
|
});
|
|
391
402
|
it('can specify which reporter to use by function', () => {
|
|
392
|
-
testWriteTo(
|
|
403
|
+
testWriteTo('writeTo-fn', { writeTo: id => id === 'special' }, (log, memLogs, specialLogs) => {
|
|
393
404
|
log.error('error message');
|
|
394
405
|
expect(memLogs.length).toEqual(0);
|
|
395
406
|
expect(specialLogs.length).toEqual(1);
|
|
@@ -397,7 +408,7 @@ describe('getLogger()', () => {
|
|
|
397
408
|
});
|
|
398
409
|
it('can specify custom reporter', () => {
|
|
399
410
|
const reporter = createMemoryLogReporter({ id: 'custom mem' });
|
|
400
|
-
testWriteTo(
|
|
411
|
+
testWriteTo('writeTo-reporter', { writeTo: reporter }, (log, memLogs, specialLogs) => {
|
|
401
412
|
log.error('error message');
|
|
402
413
|
expect(memLogs.length).toEqual(0);
|
|
403
414
|
expect(specialLogs.length).toEqual(0);
|
|
@@ -408,8 +419,8 @@ describe('getLogger()', () => {
|
|
|
408
419
|
// if not, it would be a security issue as the new reporter
|
|
409
420
|
// can capture the logs from other logger and send it elsewhere
|
|
410
421
|
const reporter = createMemoryLogReporter({ id: 'custom mem' });
|
|
411
|
-
testWriteTo(
|
|
412
|
-
const log = sl.getLogger(
|
|
422
|
+
testWriteTo('writeTo-reporter-no-override', { writeTo: reporter }, (_, memLogs, specialLogs, sl) => {
|
|
423
|
+
const log = sl.getLogger('writeTo-reporter-no-override-2');
|
|
413
424
|
log.error('error message');
|
|
414
425
|
expect(reporter.logs.length).toEqual(0);
|
|
415
426
|
});
|
|
@@ -419,7 +430,7 @@ describe('getLogger()', () => {
|
|
|
419
430
|
describe('suppressLogs()', () => {
|
|
420
431
|
test('suppress log', () => {
|
|
421
432
|
const sl = createStandardLogForTest();
|
|
422
|
-
const log = sl.getLogger(
|
|
433
|
+
const log = sl.getLogger('l');
|
|
423
434
|
suppressLogs(() => {
|
|
424
435
|
log.alert('should not see me');
|
|
425
436
|
}, log);
|
|
@@ -427,8 +438,8 @@ describe('suppressLogs()', () => {
|
|
|
427
438
|
});
|
|
428
439
|
test('suppress multiple logs', () => {
|
|
429
440
|
const sl = createStandardLogForTest();
|
|
430
|
-
const log1 = sl.getLogger(
|
|
431
|
-
const log2 = sl.getLogger(
|
|
441
|
+
const log1 = sl.getLogger('l1');
|
|
442
|
+
const log2 = sl.getLogger('l2');
|
|
432
443
|
suppressLogs(() => {
|
|
433
444
|
log1.alert('should not see me');
|
|
434
445
|
log2.alert('should not see me too');
|
|
@@ -437,10 +448,46 @@ describe('suppressLogs()', () => {
|
|
|
437
448
|
});
|
|
438
449
|
test('return block result', () => {
|
|
439
450
|
const sl = createStandardLogForTest();
|
|
440
|
-
const log1 = sl.getLogger(
|
|
441
|
-
const log2 = sl.getLogger(
|
|
451
|
+
const log1 = sl.getLogger('l');
|
|
452
|
+
const log2 = sl.getLogger('l2');
|
|
442
453
|
const a = suppressLogs(() => 1, log1, log2);
|
|
443
454
|
expect(a).toBe(1);
|
|
444
455
|
});
|
|
445
456
|
});
|
|
457
|
+
describe('getLogger()', () => {
|
|
458
|
+
it('gets a logger that logs to console by default', () => {
|
|
459
|
+
const log = getLogger('default');
|
|
460
|
+
log.info('from global log, expected to be printed');
|
|
461
|
+
log.debug('from global log, this should not be printed');
|
|
462
|
+
});
|
|
463
|
+
});
|
|
464
|
+
describe('configGlobal()', () => {
|
|
465
|
+
beforeEach(() => {
|
|
466
|
+
ctx.gsl = undefined;
|
|
467
|
+
ctx.configured = false;
|
|
468
|
+
});
|
|
469
|
+
it('configure the global instance', () => {
|
|
470
|
+
const mem = createMemoryLogReporter();
|
|
471
|
+
configGlobal({ logLevel: logLevels.error, reporters: [mem] });
|
|
472
|
+
const log = getLogger('default');
|
|
473
|
+
log.error('error message');
|
|
474
|
+
log.info('info message');
|
|
475
|
+
expect(mem.logs.length).toBe(1);
|
|
476
|
+
});
|
|
477
|
+
it('can call after getLogger', () => {
|
|
478
|
+
const log = getLogger('default');
|
|
479
|
+
log.info('from global log, expected to be printed');
|
|
480
|
+
const mem = createMemoryLogReporter();
|
|
481
|
+
configGlobal({ logLevel: logLevels.error, reporters: [mem] });
|
|
482
|
+
log.error('from global log, expected to be printed');
|
|
483
|
+
log.info('from global log, this should not be printed');
|
|
484
|
+
expect(mem.logs.length).toBe(1);
|
|
485
|
+
});
|
|
486
|
+
it('will emit a warning when called twice', () => {
|
|
487
|
+
const mem = createMemoryLogReporter();
|
|
488
|
+
configGlobal({ reporters: [mem] });
|
|
489
|
+
configGlobal({ reporters: [] });
|
|
490
|
+
a.satisfies(mem.logs, [{ id: 'standard-log', level: logLevels.warn, args: [/being called more than once/] }]);
|
|
491
|
+
});
|
|
492
|
+
});
|
|
446
493
|
//# sourceMappingURL=standardLog.spec.js.map
|