-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlogger.js
111 lines (96 loc) · 2.62 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
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
const fs = require('fs');
const settings = require('./settings.json');
const chalk = require('chalk');
const mbotLogStream = fs.createWriteStream('logs/mbot.log');
const isFileLogging = settings.fileLogging;
/**
* Used to have more detailed logging.
* @example
* const Logger = require('./logger');
* Logger.info('Connected to logger!');
*/
class Logger {
/**
* Use debug tag when logging to console.
*
* @param {string} text The text you want to send to console.
*/
static debug(text) {
if (settings.debug) {
return rawLog(text, 'DEBUG');
}
}
/**
* Use file tag when logging to console.
*
* @param {string} text The text you want to send to console.
*/
static file(text) {
return rawLog(text, 'FILE');
}
/**
* Use info tag when logging to console.
*
* @param {string} text The text you want to send to console.
*/
static info(text) {
return rawLog(text, 'INFO');
}
/**
* Use error tag when logging to console.
*
* @param {string} text The text you want to send to console.
*/
static error(text) {
return rawLog(text, 'ERROR');
}
/**
* Use warn tag when logging to console.
*
* @param {string} text The text you want to send to console.
*/
static warn(text) {
return rawLog(text, 'WARN');
}
/**
* Use no tag when logging to console.
*
* @param {string} text The text you want to send to console.
*/
static log(text) {
return new Promise(resolve => {
process.nextTick(() => {
console.log(`[${time()}]: ${text}`);
resolve();
});
});
}
}
function rawLog(message, type) {
return new Promise(resolve => {
process.nextTick(() => {
const open = chalk.yellow('[');
const close = chalk.yellow(']');
type = chalk.blue('LEVEL-' + type);
console.log(`${open}${chalk.green(time())} ${type}${close}: ${message}`);
if (isFileLogging) {
mbotLogStream.write(writeLog(message, type));
}
resolve();
});
});
}
function writeLog(message, type) {
return `[${time()} LEVEL-${type}]: ${message}\n`;
}
function time() {
const date = new Date();
let hour = date.getHours();
hour = (hour < 10 ? "0" : "") + hour;
let min = date.getMinutes();
min = (min < 10 ? "0" : "") + min;
let sec = date.getSeconds();
sec = (sec < 10 ? "0" : "") + sec;
return `${hour}:${min}:${sec}`;
}
module.exports = Logger;