standard-log 9.1.2 → 9.1.5
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 -1
- package/cjs/index.d.ts.map +1 -1
- 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 +6 -9
- package/cjs/standardLog.d.ts.map +1 -1
- package/cjs/standardLog.js +5 -6
- package/cjs/standardLog.js.map +1 -1
- package/cjs/standardLog.spec.js +54 -48
- 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 +10141 -1665
- 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 +22 -12
- 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 -1
- package/esm/index.d.ts.map +1 -1
- 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 +6 -9
- package/esm/standardLog.d.ts.map +1 -1
- package/esm/standardLog.js +7 -6
- package/esm/standardLog.js.map +1 -1
- package/esm/standardLog.spec.js +56 -49
- 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 +6 -6
- package/ts/index.ts +2 -2
- package/ts/logStore.ts +2 -2
- package/ts/logger.ts +2 -3
- package/ts/standardLog.ts +13 -11
- package/ts/types.ts +4 -2
package/esm/logStore.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"logStore.js","sourceRoot":"","sources":["../ts/logStore.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM,WAAW,CAAA;AAC1D,OAAO,EAAE,wBAAwB,EAAE,MAAM,cAAc,CAAA;AACvD,OAAO,EAAE,SAAS,EAAE,UAAU,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAA;AAGtE,MAAM,UAAU,cAAc,
|
|
1
|
+
{"version":3,"file":"logStore.js","sourceRoot":"","sources":["../ts/logStore.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM,WAAW,CAAA;AAC1D,OAAO,EAAE,wBAAwB,EAAE,MAAM,cAAc,CAAA;AACvD,OAAO,EAAE,SAAS,EAAE,UAAU,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAA;AAGtE,MAAM,UAAU,cAAc,CAAoC,OAAwC;IACxG,OAAO;QACL,OAAO,EAAE,EAAE;QACX,SAAS,EAAE,OAAO,CAAC,SAAS,IAAI,CAAC,wBAAwB,EAAE,CAAC;QAC5D,QAAQ,EAAE,OAAO,CAAC,QAAQ;QAC1B,aAAa,EAAE,aAAa,CAAC,OAAO,CAAC;KACtC,CAAA;AACH,CAAC;AAaD,MAAM,UAAU,aAAa,CAAC,OAA6B;IACzD,MAAM,YAAY,GAAG,OAAO,CAAC,YAAY,IAAI,EAAE,CAAA;IAE/C,MAAM,KAAK,GAAG;QACZ,YAAY;QACZ,mBAAmB,EAAE,WAAW,CAAC,YAAY,EAAE,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE;YACtD,CAAC,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAA;YACtB,OAAO,CAAC,CAAA;QACV,CAAC,EAAE,EAA4B,CAAC;KACjC,CAAA;IACD,OAAO;QACL,OAAO,CAAC,KAAa;;YACnB,OAAO,MAAA,KAAK,CAAC,mBAAmB,CAAC,KAAK,CAAC,mCAAI,cAAc,CAAC,KAAK,CAAC,CAAA;QAClE,CAAC;QACD,QAAQ,CAAC,IAAY;;YACnB,OAAO,MAAA,KAAK,CAAC,YAAY,CAAC,IAAI,CAAC,mCAAI,UAAU,CAAC,IAAI,CAAC,CAAA;QACrD,CAAC;QACD,YAAY;YACV,OAAO,SAAS,CAAC,SAAS,EAAE,CAAC,MAAM,EAAE,IAAI,EAAE,EAAE;gBAC3C,IAAI,IAAI,KAAK,MAAM,IAAI,IAAI,KAAK,KAAK;oBAAE,MAAM,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,KAAK,EAAE,SAAS,CAAC,IAAI,CAAC,EAAE,CAAC,CAAA;gBAEpF,OAAO,MAAM,CAAA;YACf,CAAC,EAAE,MAAM,CAAC,KAAK,CAAC,YAAY,EAAE,IAAI,CAAC,EAAE,CAAC,CAAC,EAAE,IAAI,EAAE,KAAK,EAAE,KAAK,CAAC,YAAY,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC,CAAC,CAAA;QACrF,CAAC;KACF,CAAA;AACH,CAAC","sourcesContent":["\nimport { mapKey, reduceByKey, reduceKey } from 'type-plus'\nimport { createConsoleLogReporter } from './console.js'\nimport { logLevels, toLogLevel, toLogLevelName } from './logLevels.js'\nimport type { Logger, LogLevel, LogMethodNames, LogReporter, StandardLogOptions } from './types.js'\n\nexport function createLogStore<N extends string = LogMethodNames>(options: Required<StandardLogOptions<N>>): LogStore {\n return {\n loggers: {},\n reporters: options.reporters || [createConsoleLogReporter()],\n logLevel: options.logLevel,\n logLevelStore: logLevelStore(options)\n }\n}\n\nexport interface LogStore {\n loggers: Record<string, Logger>,\n reporters: LogReporter[],\n logLevel: LogLevel,\n logLevelStore: ReturnType<typeof logLevelStore>\n}\n\nexport interface LogLevelStoreOptions {\n customLevels?: Record<string, number>\n}\n\nexport function logLevelStore(options: LogLevelStoreOptions) {\n const customLevels = options.customLevels || {}\n\n const store = {\n customLevels,\n customLevelsReverse: reduceByKey(customLevels, (p, k) => {\n p[customLevels[k]] = k\n return p\n }, {} as Record<number, string>)\n }\n return {\n getName(level: number) {\n return store.customLevelsReverse[level] ?? toLogLevelName(level)\n },\n getLevel(name: string) {\n return store.customLevels[name] ?? toLogLevel(name)\n },\n getAllLevels() {\n return reduceKey(logLevels, (result, name) => {\n if (name !== 'none' && name !== 'all') result.push({ name, level: logLevels[name] })\n\n return result\n }, mapKey(store.customLevels, name => ({ name, level: store.customLevels[name] })))\n }\n }\n}\n"]}
|
package/esm/logger.d.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import type { StackTraceMeta } from '@just-func/types';
|
|
2
1
|
import { LogStore } from './logStore.js';
|
|
3
2
|
import type { Logger, LoggerOptions, LogMethodNames } from './types.js';
|
|
4
|
-
export declare function createLogger<T extends string = LogMethodNames>(
|
|
3
|
+
export declare function createLogger<T extends string = LogMethodNames>(store: LogStore, id: string, options?: LoggerOptions): Logger;
|
|
5
4
|
//# sourceMappingURL=logger.d.ts.map
|
package/esm/logger.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"logger.d.ts","sourceRoot":"","sources":["../ts/logger.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"logger.d.ts","sourceRoot":"","sources":["../ts/logger.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAA;AACxC,OAAO,KAAK,EAAyB,MAAM,EAAE,aAAa,EAAE,cAAc,EAAe,MAAM,YAAY,CAAA;AAG3G,wBAAgB,YAAY,CAAC,CAAC,SAAS,MAAM,GAAG,cAAc,EAC5D,KAAK,EAAE,QAAQ,EAAE,EAAE,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,aAAa,GACnD,MAAM,CAyDR"}
|
package/esm/logger.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { InvalidId } from './errors.js';
|
|
2
2
|
import { logLevels } from './logLevels.js';
|
|
3
|
-
export function createLogger(
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
export function createLogger(store, id, options) {
|
|
4
|
+
var _a;
|
|
5
|
+
validateId(id, options);
|
|
6
|
+
const writeTo = (_a = options === null || options === void 0 ? void 0 : options.writeTo) !== null && _a !== void 0 ? _a : (() => true);
|
|
6
7
|
const [filter, reporters] = typeof writeTo === 'string'
|
|
7
8
|
? [id => id === writeTo, store.reporters]
|
|
8
9
|
: writeTo instanceof RegExp
|
|
@@ -14,7 +15,7 @@ export function createLogger([store, id, options], meta) {
|
|
|
14
15
|
writeToReporters(reporters, entry, filter);
|
|
15
16
|
}
|
|
16
17
|
const logger = {
|
|
17
|
-
level: options
|
|
18
|
+
level: options === null || options === void 0 ? void 0 : options.level,
|
|
18
19
|
write
|
|
19
20
|
};
|
|
20
21
|
Object.defineProperties(logger, {
|
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,EA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AAG,IAAW,EAAE,EAAE;gBACxB,IAAI,SAAS,CAAC,KAAK,EAAE,KAAK,EAAE,MAAM,CAAC,KAAK,CAAC;oBAAE,KAAK,CAAC,EAAE,EAAE,EAAE,KAAK,EAAE,IAAI,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,11 @@
|
|
|
1
|
-
import { StackTraceMeta } from '@just-func/types';
|
|
2
1
|
import { LogStore } from './logStore.js';
|
|
2
|
+
import { MemoryLogReporter } from './memory.js';
|
|
3
3
|
import type { Logger, LoggerOptions, LogLevel, LogMethodNames, StandardLogOptions } from './types.js';
|
|
4
4
|
export interface StandardLogInstance<N extends string = LogMethodNames> {
|
|
5
5
|
logLevel: number;
|
|
6
6
|
toLogLevelName(level: number): string;
|
|
7
7
|
toLogLevel(name: N): number;
|
|
8
|
-
getLogger(
|
|
8
|
+
getLogger(id: string, options?: LoggerOptions): Logger<N>;
|
|
9
9
|
}
|
|
10
10
|
export interface StandardLog<N extends string = LogMethodNames> extends Readonly<StandardLogInstance<N>> {
|
|
11
11
|
}
|
|
@@ -14,18 +14,15 @@ export declare function createStandardLogClosure<N extends string = LogMethodNam
|
|
|
14
14
|
store: LogStore;
|
|
15
15
|
standardLog: StandardLog<N>;
|
|
16
16
|
};
|
|
17
|
-
export declare
|
|
18
|
-
reporter:
|
|
19
|
-
logLevel: number;
|
|
20
|
-
toLogLevelName: (level: number) => string;
|
|
21
|
-
toLogLevel: (name: LogMethodNames) => number;
|
|
22
|
-
getLogger: (params: [id: string, options?: LoggerOptions | undefined], meta?: StackTraceMeta | undefined) => Logger<LogMethodNames>;
|
|
17
|
+
export declare type StandardLogForTest<N extends string = LogMethodNames> = StandardLog<N> & {
|
|
18
|
+
reporter: MemoryLogReporter;
|
|
23
19
|
};
|
|
20
|
+
export declare function createStandardLogForTest<N extends string = LogMethodNames>(logLevel?: LogLevel): StandardLogForTest<N>;
|
|
24
21
|
/**
|
|
25
22
|
* Suppress logging within the code block.
|
|
26
23
|
*/
|
|
27
24
|
export declare function suppressLogs<R>(block: () => R, log: Logger, ...logs: Logger[]): R;
|
|
28
|
-
export declare function getLogger<N extends string = LogMethodNames>(
|
|
25
|
+
export declare function getLogger<N extends string = LogMethodNames>(id: string, options?: LoggerOptions): Logger<N>;
|
|
29
26
|
export declare const ctx: {
|
|
30
27
|
configured?: boolean;
|
|
31
28
|
gsl?: {
|
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,CAAA;AACxD,OAAO,EAA2B,iBAAiB,EAAE,MAAM,aAAa,CAAA;AACxE,OAAO,KAAK,EAAE,MAAM,EAAE,aAAa,EAAE,QAAQ,EAAE,cAAc,EAAE,kBAAkB,EAAE,MAAM,YAAY,CAAA;AAErG,MAAM,WAAW,mBAAmB,CAAC,CAAC,SAAS,MAAM,GAAG,cAAc;IACpE,QAAQ,EAAE,MAAM,CAAC;IACjB,cAAc,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAAC;IACtC,UAAU,CAAC,IAAI,EAAE,CAAC,GAAG,MAAM,CAAC;IAC5B,SAAS,CAAC,EAAE,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,aAAa,GAAG,MAAM,CAAC,CAAC,CAAC,CAAA;CAC1D;AAED,MAAM,WAAW,WAAW,CAAC,CAAC,SAAS,MAAM,GAAG,cAAc,CAAE,SAAQ,QAAQ,CAAC,mBAAmB,CAAC,CAAC,CAAC,CAAC;CACvG;AAED,wBAAgB,iBAAiB,CAAC,CAAC,SAAS,MAAM,GAAG,cAAc,EACjE,OAAO,CAAC,EAAE,kBAAkB,CAAC,CAAC,CAAC,GAC9B,WAAW,CAAC,CAAC,CAAC,CAIhB;AAED,wBAAgB,wBAAwB,CAAC,CAAC,SAAS,MAAM,GAAG,cAAc,EACxE,OAAO,CAAC,EAAE,kBAAkB,CAAC,CAAC,CAAC,GAC9B;IAAE,KAAK,EAAE,QAAQ,CAAC;IAAC,WAAW,EAAE,WAAW,CAAC,CAAC,CAAC,CAAA;CAAE,CAoBlD;AAED,oBAAY,kBAAkB,CAAC,CAAC,SAAS,MAAM,GAAG,cAAc,IAAI,WAAW,CAAC,CAAC,CAAC,GAAG;IAAE,QAAQ,EAAE,iBAAiB,CAAA;CAAE,CAAA;AAEpH,wBAAgB,wBAAwB,CAAC,CAAC,SAAS,MAAM,GAAG,cAAc,EACxE,QAAQ,GAAE,QAA0B,GACnC,kBAAkB,CAAC,CAAC,CAAC,CAIvB;AAED;;GAEG;AACH,wBAAgB,YAAY,CAAC,CAAC,EAAE,KAAK,EAAE,MAAM,CAAC,EAAE,GAAG,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,MAAM,EAAE,GAAG,CAAC,CAAA;AASlF,wBAAgB,SAAS,CAAC,CAAC,SAAS,MAAM,GAAG,cAAc,EAAE,EAAE,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,aAAa,aAE/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
|
@@ -21,10 +21,10 @@ export function createStandardLogClosure(options) {
|
|
|
21
21
|
toLogLevel(name) {
|
|
22
22
|
return store.logLevelStore.getLevel(name);
|
|
23
23
|
},
|
|
24
|
-
getLogger(
|
|
24
|
+
getLogger(id, options) {
|
|
25
25
|
if (store.loggers[id])
|
|
26
26
|
return store.loggers[id];
|
|
27
|
-
return store.loggers[id] = createLogger(
|
|
27
|
+
return store.loggers[id] = createLogger(store, id, { ssf: this.getLogger, ...options });
|
|
28
28
|
}
|
|
29
29
|
}
|
|
30
30
|
};
|
|
@@ -41,8 +41,8 @@ export function suppressLogs(block, ...logs) {
|
|
|
41
41
|
logs.forEach((l, i) => l.level = origLevels[i]);
|
|
42
42
|
return result;
|
|
43
43
|
}
|
|
44
|
-
export function getLogger(
|
|
45
|
-
return getGlobalSL().getLogger(
|
|
44
|
+
export function getLogger(id, options) {
|
|
45
|
+
return getGlobalSL().getLogger(id, options);
|
|
46
46
|
}
|
|
47
47
|
export const ctx = record();
|
|
48
48
|
function getGlobalSL() {
|
|
@@ -51,9 +51,10 @@ function getGlobalSL() {
|
|
|
51
51
|
return ctx.gsl.standardLog;
|
|
52
52
|
}
|
|
53
53
|
export function configGlobal(options) {
|
|
54
|
+
var _a;
|
|
54
55
|
if (ctx.configured) {
|
|
55
|
-
const log = ctx.gsl
|
|
56
|
-
log
|
|
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.');
|
|
57
58
|
}
|
|
58
59
|
if (ctx.gsl) {
|
|
59
60
|
if (options.logLevel) {
|
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,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAA;AAC1C,OAAO,EAAE,cAAc,EAAY,MAAM,eAAe,CAAA;AACxD,OAAO,EAAE,uBAAuB,EAAqB,MAAM,aAAa,CAAA;AAaxE,MAAM,UAAU,iBAAiB,CAC/B,OAA+B;IAE/B,MAAM,OAAO,GAAG,wBAAwB,CAAC,OAA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AG,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, MemoryLogReporter } 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>\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 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 standardLog = createStandardLog<N>({ 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
|
@@ -30,7 +30,7 @@ describe('createStandardLog()', () => {
|
|
|
30
30
|
it('can specify reporter', () => {
|
|
31
31
|
const mem = createMemoryLogReporter();
|
|
32
32
|
const sl = createStandardLog({ reporters: [mem] });
|
|
33
|
-
const log = sl.getLogger(
|
|
33
|
+
const log = sl.getLogger('x');
|
|
34
34
|
log.info('abc');
|
|
35
35
|
expect(mem.logs.length).toBe(1);
|
|
36
36
|
});
|
|
@@ -41,8 +41,9 @@ describe('createStandardLog()', () => {
|
|
|
41
41
|
ctx.configured = false;
|
|
42
42
|
});
|
|
43
43
|
it('will affect getLogger() if configGlobal() is not called', () => {
|
|
44
|
+
var _a;
|
|
44
45
|
createStandardLog({ logLevel: logLevels.planck });
|
|
45
|
-
expect(ctx.gsl
|
|
46
|
+
expect((_a = ctx.gsl) === null || _a === void 0 ? void 0 : _a.standardLog.logLevel).toBe(logLevels.planck);
|
|
46
47
|
});
|
|
47
48
|
});
|
|
48
49
|
});
|
|
@@ -59,100 +60,100 @@ describe('createStandardLogForTest()', () => {
|
|
|
59
60
|
describe('standardLog.getLogger()', () => {
|
|
60
61
|
it('supports id with alphanumeric and :_-.', () => {
|
|
61
62
|
const sl = createStandardLog();
|
|
62
|
-
sl.getLogger(
|
|
63
|
+
sl.getLogger('abcdefghijklmnopqrstuvwxyz:-_.1234567890');
|
|
63
64
|
});
|
|
64
65
|
it('supports id with unicode', () => {
|
|
65
66
|
const sl = createStandardLog();
|
|
66
|
-
sl.getLogger(
|
|
67
|
+
sl.getLogger('ミク香港');
|
|
67
68
|
});
|
|
68
69
|
it('supports id with @, \\ and /', () => {
|
|
69
70
|
const sl = createStandardLog();
|
|
70
|
-
sl.getLogger(
|
|
71
|
-
sl.getLogger(
|
|
71
|
+
sl.getLogger('@unional/fixture');
|
|
72
|
+
sl.getLogger(`a\\b\\c`);
|
|
72
73
|
});
|
|
73
74
|
it.each('`~!#$%^&*()=+|[]{}<>,?'.split(''))('throws if id has unsupported character %s', (char) => {
|
|
74
75
|
const sl = createStandardLog();
|
|
75
|
-
a.throws(() => sl.getLogger(
|
|
76
|
+
a.throws(() => sl.getLogger(char), InvalidId);
|
|
76
77
|
});
|
|
77
78
|
it('throw InvalidId with ssf to the call site', () => {
|
|
78
79
|
const sl = createStandardLog();
|
|
79
|
-
const err = a.throws(() => sl.getLogger(
|
|
80
|
+
const err = a.throws(() => sl.getLogger('!'), InvalidId);
|
|
80
81
|
assertSSF(err, __filename);
|
|
81
82
|
});
|
|
82
83
|
test('id is readonly', () => {
|
|
83
84
|
const sl = createStandardLog();
|
|
84
|
-
const log = sl.getLogger(
|
|
85
|
+
const log = sl.getLogger('id is readonly');
|
|
85
86
|
a.throws(() => log.id = 'b');
|
|
86
87
|
});
|
|
87
88
|
test('get logger with same name gets the same instance', () => {
|
|
88
89
|
const sl = createStandardLog();
|
|
89
|
-
const expected = sl.getLogger(
|
|
90
|
-
const actual = sl.getLogger(
|
|
90
|
+
const expected = sl.getLogger('same-inst');
|
|
91
|
+
const actual = sl.getLogger('same-inst');
|
|
91
92
|
expect(actual).toBe(expected);
|
|
92
93
|
});
|
|
93
94
|
it('can specify local log level', () => {
|
|
94
95
|
const sl = createStandardLog();
|
|
95
|
-
const log = sl.getLogger(
|
|
96
|
+
const log = sl.getLogger('local', { level: logLevels.trace });
|
|
96
97
|
expect(log.level).toBe(logLevels.trace);
|
|
97
98
|
});
|
|
98
99
|
test('new logger will get custom level', () => {
|
|
99
100
|
const sl = createStandardLog({ customLevels: { 'to_logger': 1234 } });
|
|
100
|
-
const actual = sl.getLogger(
|
|
101
|
+
const actual = sl.getLogger('to_logger_logger');
|
|
101
102
|
expect(typeof actual.to_logger).toBe('function');
|
|
102
103
|
});
|
|
103
|
-
it('
|
|
104
|
+
it('needs to cast Logger when wrapped', () => {
|
|
104
105
|
function wrapper(options) {
|
|
105
106
|
const sl = createStandardLog(options);
|
|
106
107
|
// since `N` is generic, just `Logger<N>` will not have the default log method names.
|
|
107
|
-
const log = sl.getLogger(
|
|
108
|
+
const log = sl.getLogger('local');
|
|
108
109
|
expect(log.info).toBeDefined();
|
|
109
110
|
return sl;
|
|
110
111
|
}
|
|
111
112
|
const sl = wrapper({ customLevels: { 'cust': 123 } });
|
|
112
|
-
const actual = sl.getLogger(
|
|
113
|
+
const actual = sl.getLogger('c');
|
|
113
114
|
expect(actual.cust).toBeDefined();
|
|
114
115
|
});
|
|
115
116
|
describe('log levels', () => {
|
|
116
117
|
async function assertLoggedAtLevel(sl, method, level) {
|
|
117
|
-
const log = sl.getLogger(
|
|
118
|
+
const log = sl.getLogger(method);
|
|
118
119
|
const msg = `should log on level: ${level}`;
|
|
119
120
|
log[method](msg);
|
|
120
121
|
a.satisfies(sl.reporter.logs, [{ id: method, level: sl.toLogLevel(method), args: [msg] }]);
|
|
121
122
|
}
|
|
122
123
|
async function assertNotLoggedAtLevel(sl, method, level) {
|
|
123
|
-
const log = sl.getLogger(
|
|
124
|
+
const log = sl.getLogger(method);
|
|
124
125
|
log[method](`should not log on level: ${level}`);
|
|
125
126
|
expect(sl.reporter.logs.length).toBe(0);
|
|
126
127
|
}
|
|
127
128
|
async function assertLoggedAtLocalLevel(sl, method, localLevel) {
|
|
128
|
-
const log = sl.getLogger(
|
|
129
|
+
const log = sl.getLogger(method, { level: localLevel });
|
|
129
130
|
const msg = `should log on level: ${localLevel}`;
|
|
130
131
|
log[method](msg);
|
|
131
132
|
a.satisfies(sl.reporter.logs, [{ id: log.id, level: sl.toLogLevel(method), args: [msg] }]);
|
|
132
133
|
}
|
|
133
134
|
async function assertNotLoggedAtLocalLevel(sl, method, localLevel) {
|
|
134
|
-
const log = sl.getLogger(
|
|
135
|
+
const log = sl.getLogger(method, { level: localLevel });
|
|
135
136
|
log[method](`should not log on level: ${localLevel}`);
|
|
136
137
|
expect(sl.reporter.logs.length).toBe(0);
|
|
137
138
|
}
|
|
138
139
|
async function assertLoggedAtCallLevel(sl, method, callLevel) {
|
|
139
|
-
const log = sl.getLogger(
|
|
140
|
+
const log = sl.getLogger(method);
|
|
140
141
|
let actual = false;
|
|
141
142
|
log.on(callLevel, () => actual = true);
|
|
142
143
|
expect(actual).toBe(true);
|
|
143
144
|
}
|
|
144
145
|
async function assertNotLoggedAtCallLevel(sl, method, callLevel, actualLevel) {
|
|
145
|
-
const log = sl.getLogger(
|
|
146
|
+
const log = sl.getLogger(method);
|
|
146
147
|
log.on(callLevel, () => { throw new Error(`should not log at level ${actualLevel}`); });
|
|
147
148
|
}
|
|
148
149
|
async function assertLoggedAtCallLevelOverrideLocalLevel(sl, method, localLevel, callLevel) {
|
|
149
|
-
const log = sl.getLogger(
|
|
150
|
+
const log = sl.getLogger(method, { level: localLevel });
|
|
150
151
|
let actual = false;
|
|
151
152
|
log.on(callLevel, () => actual = true);
|
|
152
153
|
expect(actual).toBe(true);
|
|
153
154
|
}
|
|
154
155
|
async function assertNotLoggedAtCallLevelOverrideLocalLevel(sl, method, localLevel, callLevel) {
|
|
155
|
-
const log = sl.getLogger(
|
|
156
|
+
const log = sl.getLogger(method, { level: localLevel });
|
|
156
157
|
log.on(callLevel, () => { throw new Error(`should not log at level ${localLevel}`); });
|
|
157
158
|
}
|
|
158
159
|
const shouldLog = wrapTest((test) => (method, level) => {
|
|
@@ -310,20 +311,20 @@ describe('standardLog.getLogger()', () => {
|
|
|
310
311
|
});
|
|
311
312
|
it('writes entry directly', () => {
|
|
312
313
|
const sl = createStandardLogForTest();
|
|
313
|
-
const log = sl.getLogger(
|
|
314
|
+
const log = sl.getLogger('write-entry');
|
|
314
315
|
const entry = { id: 'write-entry', level: logLevels.info, args: ['abc'], timestamp: new Date() };
|
|
315
316
|
log.write(entry);
|
|
316
317
|
a.satisfies(sl.reporter.logs, [entry]);
|
|
317
318
|
});
|
|
318
319
|
test.each(['on', 'count', 'error', 'warn', 'info', 'debug'])('method %s is readonly', name => {
|
|
319
320
|
const sl = createStandardLog();
|
|
320
|
-
const log = sl.getLogger(
|
|
321
|
+
const log = sl.getLogger(`${name} is readonly`);
|
|
321
322
|
a.throws(() => log[name] = true);
|
|
322
323
|
});
|
|
323
324
|
describe('on()', () => {
|
|
324
325
|
it('log using log argument', () => {
|
|
325
326
|
const sl = createStandardLogForTest();
|
|
326
|
-
const logger = sl.getLogger(
|
|
327
|
+
const logger = sl.getLogger('log on fn');
|
|
327
328
|
logger.on('error', log => { log('value'); });
|
|
328
329
|
a.satisfies(sl.reporter.logs, [
|
|
329
330
|
{ id: 'log on fn', level: logLevels.error, args: ['value'] }
|
|
@@ -331,14 +332,14 @@ describe('standardLog.getLogger()', () => {
|
|
|
331
332
|
});
|
|
332
333
|
it('can take log level name in first argument', () => {
|
|
333
334
|
const sl = createStandardLogForTest();
|
|
334
|
-
const log = sl.getLogger(
|
|
335
|
+
const log = sl.getLogger('string-on');
|
|
335
336
|
log.on('debug', () => { return; });
|
|
336
337
|
});
|
|
337
338
|
});
|
|
338
339
|
describe('count()', () => {
|
|
339
340
|
test('will increment the counter', async () => {
|
|
340
341
|
const sl = createStandardLogForTest();
|
|
341
|
-
const logger = sl.getLogger(
|
|
342
|
+
const logger = sl.getLogger('inc counter');
|
|
342
343
|
logger.count();
|
|
343
344
|
logger.count();
|
|
344
345
|
a.satisfies(sl.reporter.logs, [
|
|
@@ -349,7 +350,7 @@ describe('standardLog.getLogger()', () => {
|
|
|
349
350
|
test('append args after the counter', async () => {
|
|
350
351
|
const sl = createStandardLogForTest(logLevels.debug);
|
|
351
352
|
const id = 'inc with args';
|
|
352
|
-
const logger = sl.getLogger(
|
|
353
|
+
const logger = sl.getLogger(id);
|
|
353
354
|
logger.count('msg1', 'msg2');
|
|
354
355
|
a.satisfies(sl.reporter.logs, [
|
|
355
356
|
{ id, level: logLevels.debug, args: [1, 'msg1', 'msg2'] }
|
|
@@ -358,7 +359,7 @@ describe('standardLog.getLogger()', () => {
|
|
|
358
359
|
test('not log if log level is less than debug', async () => {
|
|
359
360
|
const sl = createStandardLogForTest(logLevels.debug - 1);
|
|
360
361
|
const id = 'inc with args';
|
|
361
|
-
const logger = sl.getLogger(
|
|
362
|
+
const logger = sl.getLogger(id);
|
|
362
363
|
logger.count('msg1', 'msg2');
|
|
363
364
|
expect(sl.reporter.logs.length).toBe(0);
|
|
364
365
|
});
|
|
@@ -366,41 +367,41 @@ describe('standardLog.getLogger()', () => {
|
|
|
366
367
|
test('logger with custom level', async () => {
|
|
367
368
|
const mem = createMemoryLogReporter();
|
|
368
369
|
const sl = createStandardLog({ customLevels: { 'cust_lvl': 100 }, reporters: [mem] });
|
|
369
|
-
const logger = sl.getLogger(
|
|
370
|
+
const logger = sl.getLogger('cust');
|
|
370
371
|
logger.cust_lvl('a', 'b');
|
|
371
372
|
a.satisfies(mem.logs, [{ id: 'cust', level: 100, args: ['a', 'b'] }]);
|
|
372
373
|
});
|
|
373
374
|
describe('getLogger([,{ writeTo }])', () => {
|
|
374
|
-
function testWriteTo(
|
|
375
|
+
function testWriteTo(id, options, handler) {
|
|
375
376
|
const memReporter = createMemoryLogReporter();
|
|
376
377
|
const specialReporter = createMemoryLogReporter({ id: 'special' });
|
|
377
378
|
const sl = createStandardLog({ reporters: [memReporter, specialReporter] });
|
|
378
|
-
const log = sl.getLogger(
|
|
379
|
+
const log = sl.getLogger(id, options);
|
|
379
380
|
handler(log, memReporter.logs, specialReporter.logs, sl);
|
|
380
381
|
}
|
|
381
382
|
it('can specify which reporter to use by name', () => {
|
|
382
|
-
testWriteTo(
|
|
383
|
+
testWriteTo('writeTo-string', { writeTo: 'special' }, (log, memLogs, specialLogs) => {
|
|
383
384
|
log.error('error message');
|
|
384
385
|
expect(memLogs.length).toEqual(0);
|
|
385
386
|
expect(specialLogs.length).toEqual(1);
|
|
386
387
|
});
|
|
387
388
|
});
|
|
388
389
|
it('can specify which reporter to use by regex', () => {
|
|
389
|
-
testWriteTo(
|
|
390
|
+
testWriteTo('writeTo-regex', { writeTo: /^spec/ }, (log, memLogs, specialLogs) => {
|
|
390
391
|
log.error('error message');
|
|
391
392
|
expect(memLogs.length).toEqual(0);
|
|
392
393
|
expect(specialLogs.length).toEqual(1);
|
|
393
394
|
});
|
|
394
395
|
});
|
|
395
396
|
it('can specify which reporter to use by RegExp', () => {
|
|
396
|
-
testWriteTo(
|
|
397
|
+
testWriteTo('writeTo-RegExp', { writeTo: new RegExp('^spec') }, (log, memLogs, specialLogs) => {
|
|
397
398
|
log.error('error message');
|
|
398
399
|
expect(memLogs.length).toEqual(0);
|
|
399
400
|
expect(specialLogs.length).toEqual(1);
|
|
400
401
|
});
|
|
401
402
|
});
|
|
402
403
|
it('can specify which reporter to use by function', () => {
|
|
403
|
-
testWriteTo(
|
|
404
|
+
testWriteTo('writeTo-fn', { writeTo: id => id === 'special' }, (log, memLogs, specialLogs) => {
|
|
404
405
|
log.error('error message');
|
|
405
406
|
expect(memLogs.length).toEqual(0);
|
|
406
407
|
expect(specialLogs.length).toEqual(1);
|
|
@@ -408,7 +409,7 @@ describe('standardLog.getLogger()', () => {
|
|
|
408
409
|
});
|
|
409
410
|
it('can specify custom reporter', () => {
|
|
410
411
|
const reporter = createMemoryLogReporter({ id: 'custom mem' });
|
|
411
|
-
testWriteTo(
|
|
412
|
+
testWriteTo('writeTo-reporter', { writeTo: reporter }, (log, memLogs, specialLogs) => {
|
|
412
413
|
log.error('error message');
|
|
413
414
|
expect(memLogs.length).toEqual(0);
|
|
414
415
|
expect(specialLogs.length).toEqual(0);
|
|
@@ -419,8 +420,8 @@ describe('standardLog.getLogger()', () => {
|
|
|
419
420
|
// if not, it would be a security issue as the new reporter
|
|
420
421
|
// can capture the logs from other logger and send it elsewhere
|
|
421
422
|
const reporter = createMemoryLogReporter({ id: 'custom mem' });
|
|
422
|
-
testWriteTo(
|
|
423
|
-
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');
|
|
424
425
|
log.error('error message');
|
|
425
426
|
expect(reporter.logs.length).toEqual(0);
|
|
426
427
|
});
|
|
@@ -430,7 +431,7 @@ describe('standardLog.getLogger()', () => {
|
|
|
430
431
|
describe('suppressLogs()', () => {
|
|
431
432
|
test('suppress log', () => {
|
|
432
433
|
const sl = createStandardLogForTest();
|
|
433
|
-
const log = sl.getLogger(
|
|
434
|
+
const log = sl.getLogger('l');
|
|
434
435
|
suppressLogs(() => {
|
|
435
436
|
log.alert('should not see me');
|
|
436
437
|
}, log);
|
|
@@ -438,8 +439,8 @@ describe('suppressLogs()', () => {
|
|
|
438
439
|
});
|
|
439
440
|
test('suppress multiple logs', () => {
|
|
440
441
|
const sl = createStandardLogForTest();
|
|
441
|
-
const log1 = sl.getLogger(
|
|
442
|
-
const log2 = sl.getLogger(
|
|
442
|
+
const log1 = sl.getLogger('l1');
|
|
443
|
+
const log2 = sl.getLogger('l2');
|
|
443
444
|
suppressLogs(() => {
|
|
444
445
|
log1.alert('should not see me');
|
|
445
446
|
log2.alert('should not see me too');
|
|
@@ -448,18 +449,24 @@ describe('suppressLogs()', () => {
|
|
|
448
449
|
});
|
|
449
450
|
test('return block result', () => {
|
|
450
451
|
const sl = createStandardLogForTest();
|
|
451
|
-
const log1 = sl.getLogger(
|
|
452
|
-
const log2 = sl.getLogger(
|
|
452
|
+
const log1 = sl.getLogger('l');
|
|
453
|
+
const log2 = sl.getLogger('l2');
|
|
453
454
|
const a = suppressLogs(() => 1, log1, log2);
|
|
454
455
|
expect(a).toBe(1);
|
|
455
456
|
});
|
|
456
457
|
});
|
|
457
458
|
describe('getLogger()', () => {
|
|
458
459
|
it('gets a logger that logs to console by default', () => {
|
|
459
|
-
const log = getLogger(
|
|
460
|
+
const log = getLogger('default');
|
|
460
461
|
log.info('from global log, expected to be printed');
|
|
461
462
|
log.debug('from global log, this should not be printed');
|
|
462
463
|
});
|
|
464
|
+
it('write to local reporter', () => {
|
|
465
|
+
const reporter = createMemoryLogReporter();
|
|
466
|
+
const log = getLogger('to local reporter', { writeTo: reporter });
|
|
467
|
+
log.warn('hello');
|
|
468
|
+
expect(reporter.logs.length).toBe(1);
|
|
469
|
+
});
|
|
463
470
|
});
|
|
464
471
|
describe('configGlobal()', () => {
|
|
465
472
|
beforeEach(() => {
|
|
@@ -469,13 +476,13 @@ describe('configGlobal()', () => {
|
|
|
469
476
|
it('configure the global instance', () => {
|
|
470
477
|
const mem = createMemoryLogReporter();
|
|
471
478
|
configGlobal({ logLevel: logLevels.error, reporters: [mem] });
|
|
472
|
-
const log = getLogger(
|
|
479
|
+
const log = getLogger('default');
|
|
473
480
|
log.error('error message');
|
|
474
481
|
log.info('info message');
|
|
475
482
|
expect(mem.logs.length).toBe(1);
|
|
476
483
|
});
|
|
477
484
|
it('can call after getLogger', () => {
|
|
478
|
-
const log = getLogger(
|
|
485
|
+
const log = getLogger('default');
|
|
479
486
|
log.info('from global log, expected to be printed');
|
|
480
487
|
const mem = createMemoryLogReporter();
|
|
481
488
|
configGlobal({ logLevel: logLevels.error, reporters: [mem] });
|