-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLogger.js
More file actions
278 lines (234 loc) · 6.14 KB
/
Logger.js
File metadata and controls
278 lines (234 loc) · 6.14 KB
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: deep-purple; icon-glyph: file-alt;
const { Files } = importModule("Files");
/**
* Log level enum.
*
* @class LogLevel
*/
class LogLevel {
/**
* States that service logging is turned off.
*
* @static
* @memberof LogLevel
*/
static OFF = "OFF";
/**
* For information messages.
*
* @static
* @memberof LogLevel
*/
static INFO = "INFO";
/**
* For non-critical warnings.
*
* @static
* @memberof LogLevel
*/
static WARN = "WARN";
/**
* For errors.
*
* @static
* @memberof LogLevel
*/
static ERROR = "ERROR";
/**
* For debugging messages.
*
* @static
* @memberof LogLevel
*/
static DEBUG = "DEBUG";
}
/**
* Logger class.
*
* @class Logger
*/
class Logger {
static #PLAIN_LOG_FILE_NAME = "scriptable.user.log";
static #JSON_LOG_FILE_NAME = "scriptable.user.log.json";
static #LEVELS_FILE_NAME = "levels.json";
static #logLevels;
/**
* Log levels in their priority order.
*
* @memberof Logger
*/
static #logLevelList = [
LogLevel.OFF,
LogLevel.INFO,
LogLevel.WARN,
LogLevel.ERROR,
LogLevel.DEBUG
];
#service;
#serviceLogginPriority;
/**
* Creates an instance of Logger.
*
* @param {String} service name of service that is being logged.
* @memberof Logger
*/
constructor(service) {
this.#service = service;
this.#serviceLogginPriority = this.#getServiceLoggingPriority(service);
}
/**
* Used to log informational event.
*
* @param {String} message message explaining event
* @param {Object} data additional data related to event
* @memberof Logger
*/
info(message, data) {
this.#log(LogLevel.INFO, message, data);
}
/**
* Used to log warning.
*
* @param {String} message message explaining event
* @param {Object} data additional data related to event
* @memberof Logger
*/
warn(message, data) {
this.#log(LogLevel.WARN, message, data);
}
/**
* Used to log error.
*
* @param {String} message message explaining event
* @param {Object} data additional data related to event
* @memberof Logger
*/
error(message, data) {
this.#log(LogLevel.ERROR, message, data);
}
/**
* Used to log debugging information.
*
* @param {String} message message explaining event
* @param {Object} data additional data related to event
* @memberof Logger
*/
debug(message, data) {
this.#log(LogLevel.DEBUG, message, data);
}
/**
* Used to log event.
*
* @param {LogLevel} level event log level
* @param {String} message event message
* @param {Object} [data={}] additional data related to event
* @memberof Logger
*/
#log(level, message, data = {}) {
if (!this.#shouldBeLogged(level)) {
return;
}
const log = {
timestamp: new Date().toISOString(),
level,
service: this.#service,
message,
...data
};
this.#saveToJSONFile(log);
this.#saveToPlainFile(log);
}
/**
* Used to save message to JSON log file.
*
* @param {Object} log event log object
*/
async #saveToJSONFile(log) {
const jsonLogFile = Files.readJson(Logger.name, Logger.#JSON_LOG_FILE_NAME, {logs: []});
jsonLogFile.logs.push(log);
await Files.updateJson(Logger.name, Logger.#JSON_LOG_FILE_NAME, jsonLogFile);
}
/**
* Used to save message to plain log file.
*
* @param {Object} log event log object
*/
async #saveToPlainFile(log) {
let {
timestamp,
level,
service,
message
} = log;
delete log.timestamp;
delete log.level;
delete log.service;
delete log.message;
const dataArray = [];
for (const key of Object.keys(log)) {
dataArray.push(`${key}=${log[key]}`);
}
if (!message.endsWith(".")) {
message += '.';
}
let logFile = Files.readFile(Logger.name, Logger.#PLAIN_LOG_FILE_NAME, "");
const logMessage = `${timestamp} [${level}] [${service}] ${message} ${dataArray.join(", ")}\n`;
logFile += logMessage;
await Files.updateFile(Logger.name, Logger.#PLAIN_LOG_FILE_NAME, logFile);
}
/**
* Used to check whether provided log level
* should be logged for current logger.
*
* @param {LogLevel} logLevel log level to check
* @returns
*/
#shouldBeLogged(logLevel) {
const logLevelPriority = Logger.#logLevelList.indexOf(logLevel);
return logLevelPriority <= this.#serviceLogginPriority;
}
/**
* Used to get logging priority of
* current service.
*
* @param {String} service service to get logging priority for
* @returns
*/
#getServiceLoggingPriority(service) {
const logLevels = this.#getLogLevels();
let loggingLevel = logLevels.find(record => record.service === service)?.level;
// Don't log if logging level is not set.
if (!loggingLevel) {
loggingLevel = LogLevel.OFF;
}
return Logger.#logLevelList.indexOf(loggingLevel);
}
/**
* Used to get log levels configuration file.
*
* @returns {Object} log levels JSON object
*/
#getLogLevels() {
if (!Logger.#logLevels) {
Logger.#logLevels = Files.readJson(Logger.name, Logger.#LEVELS_FILE_NAME, []);
}
return Logger.#logLevels;
}
}
/**
* Used to get logger for end-scripts (not modules).
* This shouldn't be used in modules since modules do not
* contain their script name when imported.
*
* @returns {Logger} instance of logger
*/
function getLogger() {
return new Logger(Script.name());
}
module.exports = {
getLogger,
Logger,
LogLevel
};