This repository has been archived by the owner on Feb 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimpleLogger.js
102 lines (93 loc) · 2.03 KB
/
simpleLogger.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
'use strict';
const fs = require('fs');
/**
* Simple Logger
* @author Sparta_EN <nya@sparta0en.org>
*/
class Logger {
/**
* Constructor
* @param {*} settings User settings
*/
constructor(settings) {
this.console = settings.CONSOLE;
this.verbose = settings.DEBUG;
this.filepath = `${settings.FILEPATH}`;
}
/**
* Output debug info
* @param {boolean} verbose
*/
set verbose(verbose) {
this._verbose = verbose;
}
/**
* Output to file
* @param {string} filepath
*/
set filepath(filepath) {
if (filepath === '') return;
try {
this._filestream = fs.createWriteStream(filepath, {
flags: 'a',
});
this._file = true;
} catch (e) {
this.error(`Failed to open log file for writing.`);
this.error(e);
process.exit(1);
}
}
/**
* Log to console
* @param {boolean} console
*/
set console(console) {
this._console = true;
}
/**
* Info logging
* @param {string} message Message
*/
info(message) {
this.log('INFO', message);
}
/**
* Debug logging
* @param {string} message Message
*/
debug(message) {
if (this._verbose) {
this.log('DEBUG', message);
}
}
/**
* Warning logging
* @param {string} message
*/
warn(message) {
this.log('WARN', message);
}
/**
* Error logging
* @param {string} message
*/
error(message) {
this.log('ERROR', message);
}
/**
* Log writer
* @param {string} level
* @param {string} message
*/
log(level, message) {
const time = new Date().toISOString();
if (this._file) {
this._filestream.write(`[${time}][${level}]: ${message}\n`);
}
if (this._console) {
console.log(`[${time}][${level}]: ${message}`);
}
}
}
module.exports = Logger;