forked from mill6-plat6aux/pathfinder-testbed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.js
127 lines (114 loc) · 3.85 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*!
* Copyright 2017 Takuro Okada.
* Released under the MIT License.
*/
// @ts-check
import { readFileSync, appendFile } from "fs";
import YAML from "js-yaml";
export const LogLevel = {
debug: 1,
info: 2,
warning: 3,
error: 4,
critical: 5
};
/**
* @typedef {object} LoggerSetting
* @property {"debug"|"info"|"warning"|"error"|"critical"} [threshold]
* @property {string} [output]
* @property {string} [errorOutput]
*/
export class Logger {
#threshold = 2;
#output;
#errorOutput;
/**
* @param {string} [caller]
* @param {string|LoggerSetting} [setting]
*/
constructor(caller, setting) {
/** @type {object} */
let _setting;
if(setting != null) {
if(typeof setting == "string") {
if(setting.endsWith(".json")) {
_setting = JSON.parse(readFileSync(setting, "utf8"));
}else if(setting.endsWith(".yaml") || setting.endsWith(".yml")) {
_setting = /** @type {object} */(YAML.load(readFileSync(setting, "utf8")));
}
}else {
_setting = setting;
}
}
if(_setting == null) {
_setting = {
threshold: "debug"
};
}
if(caller != null && _setting[caller] != null) {
let __settings = _setting[caller];
Object.keys(__settings).forEach(key => {
_setting[key] = __settings[key];
});
}
if(_setting.threshold != null && typeof _setting.threshold == "string") {
if(_setting.threshold == "debug") {
this.#threshold = LogLevel.debug;
}else if(_setting.threshold == "info") {
this.#threshold = LogLevel.info;
}else if(_setting.threshold == "warning") {
this.#threshold = LogLevel.warning;
}else if(_setting.threshold == "error") {
this.#threshold = LogLevel.error;
}else if(_setting.threshold == "critical") {
this.#threshold = LogLevel.critical;
}
}
if(_setting.output != null && typeof _setting.output == "string") {
this.#output = _setting.output;
}
if(_setting.errorOutput != null && typeof _setting.errorOutput == "string") {
this.#errorOutput = _setting.errorOutput;
}
}
writeLog(message, logLevel, force) {
if(logLevel == undefined) {
logLevel = 2;
}
if((force == undefined || !force) && logLevel < this.#threshold) {
return;
}
message = this.dateString() + " " + message + "\n";
if(this.#output !== undefined) {
message = message.replaceAll(/\u001b\[[0-9]{1,2}m/g, "");
appendFile(this.#output, message, function(){});
}else {
process.stdout.write(message);
}
}
writeError(message, logLevel, force) {
if(logLevel == undefined) {
logLevel = 4;
}
if((force == undefined || !force) && logLevel < this.#threshold) {
return;
}
message = this.dateString() + " " + message + "\n";
if(this.#errorOutput !== undefined) {
message = message.replaceAll(/\u001b\[[0-9]{1,2}m/g, "");
appendFile(this.#errorOutput, message, function(){});
}else {
process.stderr.write(message);
}
}
dateString() {
let date = new Date();
return date.getFullYear() + "/" +
("00"+(date.getMonth()+1)).slice(-2) + "/" +
("00"+date.getDate()).slice(-2) + " " +
("00"+date.getHours()).slice(-2) + ":" +
("00"+date.getMinutes()).slice(-2) + ":" +
("00"+date.getSeconds()).slice(-2) + ":" +
("000"+date.getMilliseconds()).slice(-3)
}
}