-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.ts
138 lines (128 loc) · 4.1 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
const terminalColors = {
reset: "\x1b[0m",
bright: "\x1b[1m",
dim: "\x1b[2m",
underscore: "\x1b[4m",
blink: "\x1b[5m",
reverse: "\x1b[7m",
hidden: "\x1b[8m",
textColors: {
black: "\x1b[30m",
brightBlue: "\x1b[94m",
brightCyan: "\x1b[96m",
brightGreen: "\x1b[92m",
brightRed: "\x1b[91m",
blue: "\x1b[34m",
cyan: "\x1b[36m",
gray: "\x1b[90m",
green: "\x1b[32m",
grey: "\x1b[90m",
magenta: "\x1b[35m",
pink: "\x1b[95m",
red: "\x1b[31m",
white: "\x1b[97m",
yellow: "\x1b[33m",
}
};
/**
* Logger Class
* @class Logena
* @description A simple logger class that can be used to log messages to the console.
* @example const logger = new Logger();
* @example logger.set({ debug: true, appName: 'MyApp', useTimestamps: true });
* @example logger.info('This is a log message');
*/
class Logena {
private static appName: string = "";
private static colors: {
timestamp?: keyof typeof terminalColors.textColors,
appName?: keyof typeof terminalColors.textColors,
message?: keyof typeof terminalColors.textColors,
levels: {
info: keyof typeof terminalColors.textColors,
warn: keyof typeof terminalColors.textColors,
error: keyof typeof terminalColors.textColors,
debug: keyof typeof terminalColors.textColors
}
} = {
levels: {
info: "blue",
warn: "yellow",
error: "red",
debug: "cyan"
}
};
private static debugMode: boolean = false;
private static useTimestamps: boolean = false;
static set(config: {
debug?: boolean,
appName?: string,
useTimestamps?: boolean,
colors?: {
timestamp?: keyof typeof terminalColors.textColors,
appName?: keyof typeof terminalColors.textColors,
message?: keyof typeof terminalColors.textColors,
levels?: {
info?: keyof typeof terminalColors.textColors,
warn?: keyof typeof terminalColors.textColors,
error?: keyof typeof terminalColors.textColors,
debug?: keyof typeof terminalColors.textColors
}
}
}): void {
if (config.appName !== undefined) this.appName = config.appName;
if (config.debug !== undefined) this.debugMode = config.debug;
if (config.useTimestamps !== undefined) this.useTimestamps = config.useTimestamps;
if (config.colors !== undefined) {
this.colors = {
...this.colors,
...config.colors,
levels: {
...this.colors.levels,
...config.colors.levels
}
};
}
}
private static formatMessage(level: string, message: string | object): string {
const formatTimestamp = (date: Date): string => date.toISOString().replace("T", " ").replace(/\..+/, "Z");
const timestamp = this.useTimestamps ? `${terminalColors.textColors[this.colors.timestamp || "white"]}${formatTimestamp(new Date())}${terminalColors.reset} ` : "";
const app = this.appName ? `${terminalColors.textColors[this.colors.appName || "white"]}[ ${this.appName} ]${terminalColors.reset} ` : "";
const msg = typeof message === "string" ? message : JSON.stringify(message, null, 2);
const levelColor = terminalColors.textColors[this.colors.levels[level.toLowerCase() as keyof typeof this.colors.levels]];
return `${timestamp}${app}${levelColor}${level.toUpperCase()}${terminalColors.reset}: ${terminalColors.textColors[this.colors.message || "white"]}${msg}${terminalColors.reset}`;
}
/**
* Log an info message to the console
* @param (string | object) message - The message to log
* @returns void
*/
static info(message: string | object): void {
console.log(this.formatMessage("INFO", message));
}
/**
* Log a warning to the console
* @param (string | object) message - The message to log
*/
static warn(message: string | object): void {
console.warn(this.formatMessage("WARN", message));
}
/**
* Log an error to the console
* @param (string | object) message - The message to log
*/
static error(message: string | object): void {
console.error(this.formatMessage("ERROR", message));
}
/**
* Log a debug message to the console
* @param (string | object) message - The message to log
*/
static debug(message: string | object): void {
if (this.debugMode) {
console.debug(this.formatMessage("DEBUG", message));
}
}
}
export {Logena};
export default Logena;