]>
git.r.bdr.sh - rbdr/cologne/blob - lib/cologne.js
3 let LogUtilities
= require('./cologne/log_utilities');
5 /** TYPE DEFINITIONS **/
8 * Main interface for Cologne Loggers
15 * Receives any number of cologne log objects and logs them.
17 * @memberof Cologne.ILogger
20 * @returns {undefined}
24 * Main interface for Cologne Formatters
27 * @interface IFormatter
31 * Receives a cologne log object and returns a formatted string.
33 * @memberof Cologne.IFormatter
36 * @param {Cologne.tCologneLog} logObject the log to be formatted
37 * @returns {string} the formatted log
41 * The main cologne log format.
44 * @typedef {object} tCologneLog
45 * @property {Number} _timestamp the timestamp in miliseconds with decimal
46 * numbers representing fractions of miliseconds
47 * @property {String} _cologneLog main identifier, encodes the version of the
48 * cologne log format being used.
49 * @property {String} _from the origin of the log message.
50 * @property {String} _level the severity level of the log, uses syslog
52 * @property {String} _levelString the severity level keyword of the log,
53 * uses syslog priority keywords.
57 * Cologne is a logger multiplexer that works mainly with a JSON format. It
58 * can be instantiated with several loggers, or they can be changed after
66 * let co = new Cologne({
67 * from: "Special Worker Logger",
69 * new Cologne.Logger.Console({
70 * formatter: new Cologne.Formatter.Token({
71 * formatString: '[{{_timestamp}}]{{_from}}: {{message}}'
80 let Cologne
= class Cologne
{
82 constructor (config
) {
85 * The name of this logger, useful to distinguish between different
92 * @default 'Generic Cologne Logger
94 this.from = 'Generic Cologne Logger';
97 * The array containing all the loggers it will call to.
102 * @type Cologne.ILogger[]
107 Object
.assign(this, config
|| {});
111 * Adds a logger to the current instance.
113 * @function addLogger
116 * @param {Cologne.ILogger} logger the logger to add
117 * @return {undefined}
120 this.loggers
.push(logger
);
124 * Removes a logger from the current instance.
126 * @function removeLogger
129 * @param {Cologne.ILogger} logger the logger to remove
130 * @return {Cologne.ILogger[]} the removed log, inside an array.
132 removeLogger (logger
) {
135 index
= this.loggers
.indexOf(logger
);
137 this.loggers
.splice(index
, 1);
142 * Given an item, it builds a cologne log object. this is done
143 * automatically by the logger, though this is useful if you need
144 * to attach metadata before logging.
149 * @param {*} item The item to log
150 * @return {Cologne.tCologneLog} a cologne log object
152 buildLog (item
, level
, meta
) {
157 if (typeof item
=== 'undefined' || item
=== null || !item
._cologneLog
) {
158 logObject
.message
= item
;
159 logObject
._cologneLog
= this.constructor._version
;
160 logObject
._from
= this.from;
161 logObject
._level
= level
|| 6;
162 logObject
._levelString
= this._levelString(logObject
._level
);
163 logObject
._timestamp
= LogUtilities
.now();
165 if (meta
&& typeof meta
=== 'object') {
166 Object
.assign(logObject
, meta
);
172 if (item
._cologneLog
) {
173 item
._level
= level
|| item
._level
|| 6;
174 item
._levelString
= this._levelString(item
._level
);
181 * Default log function. Sends arguments to loggers. If not specified in log
182 * object, it will set the severity to 6 - INFO.
187 * @return {undefined}
190 this._log
.apply(this, [null].concat(Array
.prototype.slice
.call(arguments
)));
194 * Logs with debug level
199 * @return {undefined}
202 this._log
.apply(this, [7].concat(Array
.prototype.slice
.call(arguments
)));
206 * Logs with info level
211 * @return {undefined}
214 this._log
.apply(this, [6].concat(Array
.prototype.slice
.call(arguments
)));
218 * Logs with notice level
223 * @return {undefined}
226 this._log
.apply(this, [5].concat(Array
.prototype.slice
.call(arguments
)));
230 * Logs with warn level
235 * @return {undefined}
238 this._log
.apply(this, [4].concat(Array
.prototype.slice
.call(arguments
)));
242 * Logs with error level
247 * @return {undefined}
250 this._log
.apply(this, [3].concat(Array
.prototype.slice
.call(arguments
)));
253 // Private method that builds all the logs and sends them to the loggers.
255 let remainingArguments
, logObjectArray
, log
, logger
;
257 remainingArguments
= Array
.prototype.slice
.call(arguments
, 1);
260 for (log
of remainingArguments
) {
261 if (typeof log
=== 'undefined') {
262 logObjectArray
.push(this.buildLog('undefined', severity
));
267 logObjectArray
.push(this.buildLog('null', severity
));
271 logObjectArray
.push(this.buildLog(log
, severity
));
274 for (logger
of this.loggers
) {
275 logger
.log
.apply(logger
, logObjectArray
);
279 // Private utility method that will return the string for any given
280 // numerical severity level
281 _levelString (level
) {
303 // Version of the Cologne Log Format used.
304 Cologne
._version
= '1.0.0';
307 * Namespace that includes the built-in formatters.
309 * @namespace Formatter
312 Cologne
.Formatter
= {};
313 Cologne
.Formatter
.Simple
= require('./cologne/formatter/simple');
314 Cologne
.Formatter
.Token
= require('./cologne/formatter/token');
317 * Namespace that includes the built-in loggers.
323 Cologne
.Logger
.Console
= require('./cologne/logger/console');
324 Cologne
.Logger
.File
= require('./cologne/logger/file');
326 Cologne
.LogUtilities
= require('./cologne/log_utilities');
328 module
.exports
= Cologne
;