-
Notifications
You must be signed in to change notification settings - Fork 34
/
logger.ts
53 lines (45 loc) · 1.48 KB
/
logger.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
// @ts-expect-error
import * as kolorist from 'kolorist';
// @ts-expect-error
import dedentString from 'dedent';
import {isInDebugMode} from './flag.js';
type LogLevel = 'log' | 'warn' | 'error';
type Color = 'red' | 'yellow' | 'green' | 'white';
interface LogOptions {
dedent?: boolean;
}
type LogString = (message: string, options?: LogOptions) => void;
type Logger = LogString & {[K in Color]: LogString};
const createLogWith = (level: LogLevel, defaultColor?: Color): Logger => {
// eslint-disable-next-line no-console
const log = console[level];
const withColor = (color = defaultColor): LogString => (message, options = {}) => {
const {dedent = true} = options;
const dedented = dedent ? dedentString(message) : message;
const colorized = color ? kolorist[color](dedented) : dedented;
log(colorized);
};
const logBase = withColor();
return Object.assign(
logBase,
{
red: withColor('red'),
yellow: withColor('yellow'),
green: withColor('green'),
white: withColor('white'),
}
);
};
export default {
log: createLogWith('log'),
warn: createLogWith('warn', 'yellow'),
error: createLogWith('error', 'red'),
debug: (message: any) => {
if (isInDebugMode()) {
// eslint-disable-next-line no-console
console.log(message);
}
},
// eslint-disable-next-line no-console
lineBreak: () => console.log(''),
};