-
Notifications
You must be signed in to change notification settings - Fork 1
/
logger-service.ts
57 lines (47 loc) · 1.72 KB
/
logger-service.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
import { inject, injectable } from 'inversify';
import type { LoggerInterface } from './types.js';
import { LoggerLevels } from './types.js';
import StorageService from '../storage/storage-service.js';
import type { DeployerBehavior } from '../server/types.js';
import { COLORS } from './consts.js';
const LoggerLevelsColorMap: Record<LoggerLevels, string> = {
[LoggerLevels.INFO]: 'white',
[LoggerLevels.ERROR]: 'red',
[LoggerLevels.WARNING]: 'yellow',
[LoggerLevels.COMMAND]: 'gray',
[LoggerLevels.SUCCESS]: 'green',
[LoggerLevels.VERBOSE]: 'cyan',
};
@injectable()
export default class LoggerService implements LoggerInterface {
constructor(@inject(StorageService) protected readonly storage: StorageService) {}
public info(...messages: any[]) {
this.stdout(LoggerLevels.INFO, ...messages);
}
public error(...messages: any[]) {
this.stdout(LoggerLevels.ERROR, ...messages);
}
public warn(...messages: any[]) {
this.stdout(LoggerLevels.WARNING, ...messages);
}
public command(...messages: any[]) {
this.stdout(LoggerLevels.COMMAND, ...messages);
}
public success(...messages: any[]) {
this.stdout(LoggerLevels.SUCCESS, ...messages);
}
public verbose(...messages: any[]) {
let deployerSettings: DeployerBehavior | null = null;
try {
deployerSettings = this.storage.getCurrentConfig().deployer;
} catch (e) {}
if (deployerSettings?.showCommandLogs) {
this.stdout(LoggerLevels.VERBOSE, ...messages);
}
}
protected stdout(level: LoggerLevels, ...messages: any[]): void {
const colorKey = LoggerLevelsColorMap[level];
const header = `${COLORS[colorKey]}[${new Date().toUTCString()}] [${level}]`;
console.log(header, ...messages, COLORS.reset);
}
}