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