]>
git.r.bdr.sh - rbdr/cologne/blob - lib/cologne.js
392b452210ccd3fc07fdb732e9eac21e9d9591ba
3 const Utilities
= require('./utilities');
7 // Maps log levels to their syslog labels
20 // Version of the Cologne Log Format used
25 /** TYPE DEFINITIONS **/
28 * Main interface for Cologne Loggers
34 * Receives any number of cologne log objects and logs them.
42 * Main interface for Cologne Formatters
44 * @interface IFormatter
48 * Receives a cologne log object and returns a formatted string.
50 * @memberof IFormatter
53 * @param {tCologneLog} logObject the log to be formatted
54 * @returns {string} the formatted log
58 * The main cologne log format.
60 * @typedef {object} tCologneLog
61 * @property {Bigint} _timestamp the timestamp in nanoseconds
62 * @property {String} _cologneLog main identifier, encodes the version of the
63 * cologne log format being used.
64 * @property {String} _from the origin of the log message.
65 * @property {String} _level the severity level of the log, uses syslog
67 * @property {String} _levelString the severity level keyword of the log,
68 * uses syslog priority keywords.
72 * The main logger class. It can be instantiated with loggers in order to
73 * send messages to different destinations.
77 const Cologne
= class Cologne
{
82 * The name of this logger, useful to distinguish between different
89 * @default 'Generic Cologne Logger
91 this.from = 'Generic Cologne Logger';
94 * The array containing all the loggers it will call to.
104 Object
.assign(this, config
);
108 * Adds a logger to the current instance.
110 * @function addLogger
113 * @param {ILogger} logger the logger to add
117 this.loggers
.push(logger
);
121 * Removes a logger from the current instance.
123 * @function removeLogger
126 * @param {ILogger} logger the logger to remove
127 * @return {ILogger[]} the removed log, inside an array.
129 removeLogger(logger
) {
131 const index
= this.loggers
.indexOf(logger
);
133 this.loggers
.splice(index
, 1);
138 * Given a message, it builds a cologne log object without logging it.
139 * If you send a cologne log object, it will only update the level.
141 * If the message is an object, the log object will be extended with
147 * @param {*} message The message to log
148 * @param {number} [level=6] The level of the message to log
149 * @return {tCologneLog} a cologne log object
151 buildLog(rawMessage
, level
) {
153 if (typeof rawMessage
=== 'undefined' || rawMessage
=== null || !rawMessage
._cologneLog
) {
155 const message
= typeof rawMessage
=== 'object' ? Utilities
.stringify(rawMessage
) : rawMessage
;
158 message: String(message
),
159 _cologneLog: internals
.kLogVersion
,
162 _timestamp: Utilities
.now()
165 logObject
._levelString
= internals
.kLevelStrings
[logObject
._level
];
167 if (typeof rawMessage
=== 'object') {
168 Object
.assign(logObject
, rawMessage
);
174 rawMessage
._level
= level
|| rawMessage
._level
;
175 rawMessage
._levelString
= internals
.kLevelStrings
[rawMessage
._level
];
181 * Default log function. Sends arguments to loggers. If not specified in log
182 * object, it will set the severity to 6 - INFO.
190 this._log(null, ...logs
);
194 * Logs with debug level
202 this._log(7, ...logs
);
206 * Logs with info level
214 this._log(6, ...logs
);
218 * Logs with notice level
226 this._log(5, ...logs
);
230 * Logs with warn level
238 this._log(4, ...logs
);
242 * Logs with error level
250 this._log(3, ...logs
);
253 // Private method that builds all the logs and sends them to the loggers.
255 _log(level
, ...logs
) {
257 const structuredLogs
= logs
.map((log
) => this.buildLog(log
, level
));
259 for (const logger
of this.loggers
) {
260 logger
.log(...structuredLogs
);
266 * Namespace that includes the built-in formatters.
268 * @namespace Formatters
270 const Formatters
= {};
271 Formatters
.Simple
= require('./formatters/simple');
272 Formatters
.Token
= require('./formatters/token');
275 * Namespace that includes the built-in loggers.
280 Loggers
.Console
= require('./loggers/console');
281 Loggers
.File
= require('./loggers/file');