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