This repository was archived by the owner on Oct 27, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.js
71 lines (69 loc) · 2.23 KB
/
logger.js
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
const fs = require('fs')
DARKGRAY='\033[1;30m'
RED='\033[0;31m'
LIGHTRED='\033[1;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
PURPLE='\033[0;35m'
LIGHTPURPLE='\033[1;35m'
CYAN='\033[0;36m'
WHITE='\033[1;37m'
SET='\033[0m'
class Logger {
initLog() {
this.initialized = true
fs.writeFileSync('latest.log', `--- The log begin at ${new Date().toLocaleString()} ---\n`)
this.info('The log file has initialized.', true)
return this
}
getLogger(thread, color = 'yellow') {
if (!this.initialized) {
this.initLog()
}
this.info(`Added logger for: ${thread}`, true)
let newLogger = new Logger();
newLogger.thread = thread
switch (color) {
case 'yellow': newLogger.color = YELLOW; break;
case 'darkgray': newLogger.color = DARKGRAY; break;
case 'red': newLogger.color = RED; break;
case 'lightred': newLogger.color = LIGHTRED; break;
case 'green': newLogger.color = GREEN; break;
case 'lightpurple': newLogger.color = LIGHTPURPLE; break;
case 'white': newLogger.color = WHITE; break;
case 'cyan': newLogger.color = CYAN; break;
case 'purple': newLogger.color = PURPLE; break;
}
return newLogger;
}
info(message, isLogger = false) {
let thread = this.thread, color = this.color
if (isLogger) { thread = 'logger'; color = PURPLE }
fs.appendFileSync('latest.log', `${color}${thread} ${CYAN}${message}${SET}\n`)
console.info(`${color}${thread} ${CYAN}${message}${SET}`)
return this
}
warn(message, isLogger = false) {
let thread = this.thread
if (isLogger) thread = 'logger'
fs.appendFileSync('latest.log', `[${thread}/WARN] ${message}\n`)
console.warn(`[${thread}/WARN] ${message}`)
return this
}
error(message, isLogger = false) {
let thread = this.thread
if (isLogger) thread = 'logger'
fs.appendFileSync('latest.log', `[${thread}/ERROR] ${message}\n`)
console.error(`[${thread}/ERROR] ${message}`)
return this
}
fatal(message, isLogger = false) {
let thread = this.thread
if (isLogger) thread = 'logger'
fs.appendFileSync('latest.log', `[${thread}/FATAL] ${message}\n`)
console.error(`[${thread}/FATAL] ${message}`)
return this
}
}
module.exports = new Logger()