-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
220 lines (186 loc) · 5.08 KB
/
index.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import { WriteStream } from "fs";
const chalk = require("chalk")
const moment = require("moment")
const fs = require("node:fs");
const util = require("is-what");
enum LogLevel {
DISABLED = 0,
FATAL = 1,
ERROR = 2,
WARN = 3,
INFO = 4,
DEBUG = 5
}
interface LangConfig {
debug: string;
info: string;
warn: string;
error: string;
fatal: string;
}
interface ConfigBase {
logLevel?: number;
timestamp?: boolean;
lang?: LangConfig;
}
interface ConfigWithFolder extends ConfigBase {
logFolder?: string;
logFile?: never;
}
interface ConfigWithFile extends ConfigBase {
logFolder?: never;
logFile?: string;
}
type Config = ConfigWithFolder | ConfigWithFile;
interface ConstructedConfig {
logLevel: number;
logFolder?: string;
logFile?: string;
timestamp: boolean;
lang: LangConfig;
}
class MooLogger {
config: ConstructedConfig;
logFile?: WriteStream;
/**
* Represents MooLogger, a simple and beautiful logger.
* @param {Number} config.logLevel
*/
constructor(config?: Config) {
let { logLevel, logFolder, logFile, timestamp, lang } = config || {};
if (!logLevel && logLevel !== 0)
logLevel = 4
if (!timestamp)
timestamp = true
const defaultLang = {
debug: "DEBUG",
info: "INFO",
warn: "WARN",
error: "ERROR",
fatal: "FATAL",
}
lang = { ...defaultLang, ...(lang || {}) }
if (logFolder) {
this.logFile = fs.createWriteStream(
`${logFolder}/${moment().unix()}.log`,
{
flags: "a",
}
)
if (!fs.existsSync(logFolder)) {
fs.mkdirSync(logFolder, { recursive: true })
}
} else if (logFile)
this.logFile = fs.createWriteStream(logFile, {
flags: "a"
})
this.config = { logLevel, logFolder, logFile, timestamp, lang }
}
/**
* Get colored timestamp of now time.
* @private
*/
getTimestamp(): string {
return this.config.timestamp
? chalk.cyanBright(this.getPureTimestamp())
: ""
}
/**
* Get text-only timestamp of now time.
* @private
*/
getPureTimestamp(): string {
return this.config.timestamp
? moment().format("YYYY/MM/DD HH:mm:ss ")
: ""
}
/**
* Prints a debug message.
* @param {any[]} data Data to write into logger.
*/
debug(...data: any[]) {
data.unshift(this.getTimestamp() + chalk.bgGreenBright(` ${this.config.lang.debug} `))
if (this.config.logLevel >= 5)
console.debug(...data)
data.shift()
this.logFile?.write(`[${this.getPureTimestamp()}${this.config.lang.debug}] ${String(data)}\n`)
}
/**
* Prints an info message.
* @param {any[]} data Data to write into logger.
*/
info(...data: any[]) {
data.unshift(this.getTimestamp() + chalk.bgBlueBright(` ${this.config.lang.info} `))
if (this.config.logLevel >= 4)
console.info(...data)
data.shift()
this.logFile?.write(`[${this.getPureTimestamp()}${this.config.lang.info}] ${String(data)}\n`)
}
/**
* Prints a warning message.
* @param {any[]} data Data to write into logger.
*/
warn(...data: any[]) {
data.unshift(this.getTimestamp() + chalk.bgYellowBright(` ${this.config.lang.warn} `))
if (this.config.logLevel >= 3)
console.warn(...data)
data.shift()
this.logFile?.write(`[${this.getPureTimestamp()}${this.config.lang.warn}] ${String(data)}\n`)
}
/**
* Prints an error message.
* @param {any[]} data Data to write into logger.
* If the Error object is put at first, it will be logged as a stack trace string.
*/
error(...data: any[]) {
data.unshift(this.getTimestamp() + chalk.bgRedBright(` ${this.config.lang.error} `))
let original;
if (util.isError(data[1])) {
original = data[1]
data[1] = data[1].stack
.replaceAll("at", chalk.bold.gray("at"))
.replaceAll("(", "(\x1B[33m")
.replaceAll(")", "\x1B[39m)")
}
if (this.config.logLevel >= 2)
console.error(...data)
data.shift()
if (util.isError(original)) {
data[0] = original
}
this.logFile?.write(
`[${this.getPureTimestamp()}${this.config.lang.error}] ${
util.isError(data[0]) ? data[0].stack : String(data)
}\n`
)
}
/**
* Prints an fatal error message.
* @param {any[]} data Data to write into logger.
* If the Error object is put at first, it will be logged as a stack trace string.
*/
fatal(...data: any[]) {
data.unshift(this.getTimestamp() + chalk.bgRed(` ${this.config.lang.fatal} `))
let original;
if (util.isError(data[1])) {
original = data[1]
data[1] = data[1].stack
.replaceAll("at", chalk.bold.gray("at"))
.replaceAll("(", "(\x1B[33m")
.replaceAll(")", "\x1B[39m)")
}
if (this.config.logLevel >= 2)
console.error(...data)
data.shift()
if (util.isError(original)) {
data[0] = original
}
this.logFile?.write(
`[${this.getPureTimestamp()}${this.config.lang.fatal}] ${
util.isError(data[0]) ? data[0].stack : String(data)
}\n`
)
}
}
export { MooLogger, LogLevel };
export type { Config, LangConfig };