X-Git-Url: https://git.r.bdr.sh/rbdr/cologne/blobdiff_plain/c35e454310676fa3fca931bdb5c4e56a01775ef4..ae85c067634676251e812765c81adfdef8f85f9d:/lib/loggers/console.js diff --git a/lib/loggers/console.js b/lib/loggers/console.js index 5002502..116b04b 100644 --- a/lib/loggers/console.js +++ b/lib/loggers/console.js @@ -1,87 +1,94 @@ -(function (global) { - var Module, Class; - - // Load up dependencies - if (typeof require === 'function') { - var Ne = require('neon'); - Module = Ne.Module; - Class = Ne.Class; - } else { - Module = global.Module; - Class = global.Class; +'use strict'; + +const Utilities = require('../utilities'); + +/** + * Logger for the javascript console. + * + * @implements ILogger + * @class Console + */ +module.exports = class ConsoleLogger { + constructor(config) { + + /** + * The console it will write to, can be any object that looks + * and acts like a console, including other cologne objects. + * + * @name console + * @instance + * @memberof Loggers.Console + * @type Object + * @default global.console + */ + this.console = console; + + /** + * The formatter it will use to output the log. If not present it + * will output raw JSON + * + * @name formatter + * @instance + * @memberof Loggers.Console + * @type IFormatter + * @default null + */ + this.formatter = null; + + Object.assign(this, config); } - var Cobalt = {}; - Module(Cobalt, 'Logger')({}); - - Cobalt.Logger.JsConsole = Class(Cobalt.Logger, 'JsConsole')({ - prototype : { - console : null, - formatterOpts : {}, - - init : function (config) { - var logger = this, - property; - - if (config) { - for (property in config) { - logger[property] = config[property]; - } - } - }, - - log : function () { - var i, messageArray = [], message, severity; - - for (i = 0; i < arguments.length; i++) { - messageArray.push(this.format(arguments[i])); - if (!severity) { - severity = arguments[i]._level - } - } - - message = messageArray.join(' '); - - switch (severity){ - case 0: - case 1: - case 2: - case 3: - this.console.error(message); - break; - case 4: - this.console.warn(message); - break; - case 5: - case 6: - this.console.info(message); - break; - case 7: - default: - this.console.log(message); - break; - } - }, - - format : function (logObject) { - // Usually what you want to do here is format. Preferably using - // someone inside Cobalt.Formatter - if (this.formatter) { - return this.formatter.format(logObject, this.formatterOpts); - } - - return logObject; - } + + /** + * Main entry point, for each incoming argument it will attempt to + * format and send to the console. + * + * @function log + * @instance + * @memberof Loggers.Console + * @return {undefined} + */ + log(...logs) { + + const formattedLogs = logs.map((log) => ({ log: this._format(log), level: log._level })); + + for (const { log, level } of formattedLogs) { + this._log(log, level ); } - }); - if (Cobalt.Logger.JsConsole.__objectSpy) { - Cobalt.Logger.JsConsole.__objectSpy.destroy(); } - if (typeof require === 'function') { - global.JsConsole = Cobalt.Logger.JsConsole; - } else { - global.Cobalt.Logger.JsConsole = Cobalt.Logger.JsConsole; + // Routes an individual log to the appropriatet console + + _log(log, level) { + + switch (level) { + case 0: + case 1: + case 2: + case 3: + this.console.error(log); + break; + case 4: + this.console.warn(log); + break; + case 5: + case 6: + this.console.info(log); + break; + case 7: + default: + this.console.log(log); + break; + } + } + + _format(logObject) { + + if (this.formatter) { + return this.formatter.format(logObject); + } + + return Utilities.stringify(logObject); } -}(typeof window !== 'undefined' ? window : (typeof exports !== 'undefined' ? exports : self))); +};