This repository was archived by the owner on Oct 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdebug.ts
58 lines (45 loc) · 1.42 KB
/
debug.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
54
55
56
57
58
import chalk from 'chalk';
import { Logger } from './src/compiler/interfaces';
const appName = `driza`;
const levels = [`info`, `notice`, `warn`, `error`, `fatal`];
const timestamp = Date.now();
let currentLogger = console.log;
let currentLevel = 0;
export const setLogger = (customLogger: (...messages: any[]) => void) => {
currentLogger = customLogger;
};
export const setLevel = (customLevel: string) => {
currentLevel = levels.findIndex(l => l === customLevel);
};
export default (job: string): Logger => {
const log = (messageLevel: string, ...messages: string[]) => {
const messageLevelIndex = levels.findIndex(l => l === messageLevel);
if (messageLevelIndex >= currentLevel) {
currentLogger(chalk.blue.bold(`${appName}:${job}`), ...messages, chalk.blue.bold(`+${Date.now() - timestamp}ms`));
}
};
function fatal(...message: any[]) {
log(`fatal`, chalk.black.bgRed(' FATAL ERROR '), ...message);
notice('Fatal error. Exiting with code 1.');
process.exit(1);
}
function error(...message: any[]) {
log(`error`, chalk.black.bgRed(' ERROR '), ...message);
}
function warn(...message: any[]) {
log(`warn`, chalk.black.bgYellow(' WARN '), ...message);
}
function notice(...message: any[]) {
log(`notice`, chalk.black.bgCyan(' NOTICE '), ...message);
}
function info(...message: any[]) {
log(`info`, chalk.black.bgGreen(' INFO '), ...message);
}
return {
fatal,
error,
warn,
notice,
info,
};
};