-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathterrier.ts
53 lines (49 loc) · 1.58 KB
/
terrier.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import type {
Logger,
LoggerConfiguration,
LoggerMethod,
} from './lib/types.js';
import {
defaultLevel,
defaultPrefix,
defaultSeparator,
defaultStream,
defaultStringify,
defaultTimestamp,
} from './lib/defaults.js';
import { LoggerLevel } from './lib/types.js';
export * from './lib/colors.js';
export * from './lib/defaults.js';
export * from './lib/types.js';
/** Creates a new logger with the given configuration. */
export function createLogger({
level = defaultLevel,
timestamp = defaultTimestamp,
stringify = defaultStringify,
prefix = defaultPrefix,
stream = defaultStream,
separator = defaultSeparator,
}: LoggerConfiguration = { }): Logger {
return (function createChild(...context: string[]) {
const createMethodForLevel = (
targetLevel: LoggerLevel,
): LoggerMethod => function log(...args) {
if (targetLevel < level) return; // ignored
const target = stream(targetLevel);
const string = args.map(stringify).join(' ');
const pre = prefix(targetLevel);
const ctx = context.join(separator) + (context.length ? separator : '');
target.write(`${timestamp( )}${pre}${ctx}${string}\n`);
};
const child = createMethodForLevel(LoggerLevel.Info);
return Object.assign(child, {
fatal: createMethodForLevel(LoggerLevel.Fatal),
error: createMethodForLevel(LoggerLevel.Error),
warn: createMethodForLevel(LoggerLevel.Warn),
info: createMethodForLevel(LoggerLevel.Info),
debug: createMethodForLevel(LoggerLevel.Debug),
trace: createMethodForLevel(LoggerLevel.Trace),
child: (...ctx: string[]) => createChild(...context, ...ctx),
});
})( );
}