]> git.r.bdr.sh - rbdr/cologne/blob - lib/loggers/console.js
Update docs and tooling
[rbdr/cologne] / lib / loggers / console.js
1 'use strict';
2
3 const Utilities = require('../utilities');
4
5 /**
6 * Logger for the javascript console.
7 *
8 * @implements ILogger
9 * @memberof Loggers
10 * @class Console
11 */
12 module.exports = class ConsoleLogger {
13 constructor(config) {
14
15 /**
16 * The console it will write to, can be any object that looks
17 * and acts like a console, including other cologne objects.
18 *
19 * @name console
20 * @instance
21 * @memberof Loggers.Console
22 * @type Object
23 * @default global.console
24 */
25 this.console = console;
26
27 /**
28 * The formatter it will use to output the log. If not present it
29 * will output raw JSON
30 *
31 * @name formatter
32 * @instance
33 * @memberof Loggers.Console
34 * @type IFormatter
35 * @default null
36 */
37 this.formatter = null;
38
39 Object.assign(this, config);
40 }
41
42
43 /**
44 * Main entry point, for each incoming argument it will attempt to
45 * format and send to the console.
46 *
47 * @function log
48 * @instance
49 * @memberof Loggers.Console
50 * @return {undefined}
51 */
52 log(...logs) {
53
54 const formattedLogs = logs.map((log) => ({ log: this._format(log), level: log._level }));
55
56 for (const { log, level } of formattedLogs) {
57 this._log(log, level );
58 }
59
60 }
61
62 // Routes an individual log to the appropriatet console
63
64 _log(log, level) {
65
66 switch (level) {
67 case 0:
68 case 1:
69 case 2:
70 case 3:
71 this.console.error(log);
72 break;
73 case 4:
74 this.console.warn(log);
75 break;
76 case 5:
77 case 6:
78 this.console.info(log);
79 break;
80 case 7:
81 default:
82 this.console.log(log);
83 break;
84 }
85 }
86
87 _format(logObject) {
88
89 if (this.formatter) {
90 return this.formatter.format(logObject);
91 }
92
93 return Utilities.stringify(logObject);
94 }
95 };