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
279
280
281
282
283
284
285
286
287
288
|
'use strict';
const Utilities = require('./utilities');
const internals = {
// Maps log levels to their syslog labels
kLevelStrings: [
'emerg',
'alert',
'crit',
'error',
'warning',
'notice',
'info',
'debug'
],
// Version of the Cologne Log Format used
kLogVersion: '2.0.0'
};
/** TYPE DEFINITIONS **/
/**
* Main interface for Cologne Loggers
*
* @interface ILogger
*/
/**
* Receives any number of cologne log objects and logs them.
*
* @memberof ILogger
* @function
* @name log
*/
/**
* Main interface for Cologne Formatters
*
* @interface IFormatter
*/
/**
* Receives a cologne log object and returns a formatted string.
*
* @memberof IFormatter
* @function
* @name format
* @param {tCologneLog} logObject the log to be formatted
* @returns {string} the formatted log
*/
/**
* The main cologne log format.
*
* @typedef {object} tCologneLog
* @property {Bigint} _timestamp the timestamp in nanoseconds
* @property {String} _cologneLog main identifier, encodes the version of the
* cologne log format being used.
* @property {String} _from the origin of the log message.
* @property {String} _level the severity level of the log, uses syslog
* priorities.
* @property {String} _levelString the severity level keyword of the log,
* uses syslog priority keywords.
*/
/**
* The main logger class. It can be instantiated with loggers in order to
* send messages to different destinations.
*
* @class Cologne
*/
const Cologne = class Cologne {
constructor(config) {
/**
* The name of this logger, useful to distinguish between different
* loggers.
*
* @name from
* @instance
* @memberof Cologne
* @type String
* @default 'Generic Cologne Logger
*/
this.from = 'Generic Cologne Logger';
/**
* The array containing all the loggers it will call to.
*
* @name loggers
* @instance
* @memberof Cologne
* @type ILogger[]
* @default []
*/
this.loggers = [];
Object.assign(this, config);
}
/**
* Adds a logger to the current instance.
*
* @function addLogger
* @instance
* @memberof Cologne
* @param {ILogger} logger the logger to add
*/
addLogger(logger) {
this.loggers.push(logger);
}
/**
* Removes a logger from the current instance.
*
* @function removeLogger
* @instance
* @memberof Cologne
* @param {ILogger} logger the logger to remove
* @return {ILogger[]} the removed log, inside an array.
*/
removeLogger(logger) {
const index = this.loggers.indexOf(logger);
if (index >= 0) {
this.loggers.splice(index, 1);
}
}
/**
* Given a message, it builds a cologne log object without logging it.
* If you send a cologne log object, it will only update the level.
*
* If the message is an object, the log object will be extended with
* its properties.
*
* @function buildLog
* @instance
* @memberof Cologne
* @param {*} message The message to log
* @param {number} [level=6] The level of the message to log
* @return {tCologneLog} a cologne log object
*/
buildLog(rawMessage, level) {
if (typeof rawMessage === 'undefined' || rawMessage === null || !rawMessage._cologneLog) {
const message = typeof rawMessage === 'object' ? Utilities.stringify(rawMessage) : rawMessage;
const logObject = {
message: String(message),
_cologneLog: internals.kLogVersion,
_from: this.from,
_level: level || 6,
_timestamp: Utilities.now()
};
logObject._levelString = internals.kLevelStrings[logObject._level];
if (typeof rawMessage === 'object') {
Object.assign(logObject, rawMessage);
}
return logObject;
}
rawMessage._level = level || rawMessage._level;
rawMessage._levelString = internals.kLevelStrings[rawMessage._level];
return rawMessage;
}
/**
* Default log function. Sends arguments to loggers. If not specified in log
* object, it will set the severity to 6 - INFO.
*
* @function log
* @instance
* @memberof Cologne
*/
log(...logs) {
this._log(null, ...logs);
}
/**
* Logs with debug level
*
* @function debug
* @instance
* @memberof Cologne
*/
debug(...logs) {
this._log(7, ...logs);
}
/**
* Logs with info level
*
* @function info
* @instance
* @memberof Cologne
*/
info(...logs) {
this._log(6, ...logs);
}
/**
* Logs with notice level
*
* @function notice
* @instance
* @memberof Cologne
*/
notice(...logs) {
this._log(5, ...logs);
}
/**
* Logs with warn level
*
* @function warn
* @instance
* @memberof Cologne
*/
warn(...logs) {
this._log(4, ...logs);
}
/**
* Logs with error level
*
* @function error
* @instance
* @memberof Cologne
*/
error(...logs) {
this._log(3, ...logs);
}
// Private method that builds all the logs and sends them to the loggers.
_log(level, ...logs) {
const structuredLogs = logs.map((log) => this.buildLog(log, level));
for (const logger of this.loggers) {
logger.log(...structuredLogs);
}
}
};
/**
* Namespace that includes the built-in formatters.
*
* @namespace Formatters
*/
const Formatters = {};
Formatters.Simple = require('./formatters/simple');
Formatters.Token = require('./formatters/token');
/**
* Namespace that includes the built-in loggers.
*
* @namespace Loggers
*/
const Loggers = {};
Loggers.Console = require('./loggers/console');
Loggers.File = require('./loggers/file');
module.exports = {
Cologne,
Formatters,
Loggers,
Utilities
};
|