diff options
| author | Ruben Beltran del Rio <ruben@unlimited.pizza> | 2020-09-21 01:00:19 +0200 |
|---|---|---|
| committer | Ruben Beltran del Rio <ruben@unlimited.pizza> | 2020-09-21 01:00:19 +0200 |
| commit | ae85c067634676251e812765c81adfdef8f85f9d (patch) | |
| tree | ea1557df2172aeb30a363afec6a2183f05469d22 /lib/loggers/console.js | |
| parent | 58906d77975b35fe93569f8083b90140124f9c41 (diff) | |
Update the code.
Diffstat (limited to 'lib/loggers/console.js')
| -rw-r--r-- | lib/loggers/console.js | 94 |
1 files changed, 94 insertions, 0 deletions
diff --git a/lib/loggers/console.js b/lib/loggers/console.js new file mode 100644 index 0000000..116b04b --- /dev/null +++ b/lib/loggers/console.js @@ -0,0 +1,94 @@ +'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); + } + + + /** + * 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 ); + } + + } + + // 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); + } +}; |