]> git.r.bdr.sh - rbdr/cologne/blame - lib/loggers/console.js
Cache the nyc_output dir
[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
8b25c581 9 * @memberof Loggers
58906d77
RBR
10 * @class Console
11 */
ae85c067
RBR
12module.exports = class ConsoleLogger {
13 constructor(config) {
58906d77
RBR
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
ae85c067 21 * @memberof Loggers.Console
58906d77
RBR
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
ae85c067
RBR
33 * @memberof Loggers.Console
34 * @type IFormatter
58906d77
RBR
35 * @default null
36 */
37 this.formatter = null;
38
ae85c067 39 Object.assign(this, config);
58906d77
RBR
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
ae85c067 49 * @memberof Loggers.Console
58906d77
RBR
50 * @return {undefined}
51 */
ae85c067 52 log(...logs) {
58906d77 53
ae85c067 54 const formattedLogs = logs.map((log) => ({ log: this._format(log), level: log._level }));
58906d77 55
ae85c067
RBR
56 for (const { log, level } of formattedLogs) {
57 this._log(log, level );
58906d77
RBR
58 }
59
ae85c067
RBR
60 }
61
62 // Routes an individual log to the appropriatet console
63
64 _log(log, level) {
65
66 switch (level) {
58906d77
RBR
67 case 0:
68 case 1:
69 case 2:
70 case 3:
ae85c067 71 this.console.error(log);
58906d77
RBR
72 break;
73 case 4:
ae85c067 74 this.console.warn(log);
58906d77
RBR
75 break;
76 case 5:
77 case 6:
ae85c067 78 this.console.info(log);
58906d77
RBR
79 break;
80 case 7:
81 default:
ae85c067 82 this.console.log(log);
58906d77
RBR
83 break;
84 }
85 }
86
ae85c067
RBR
87 _format(logObject) {
88
58906d77
RBR
89 if (this.formatter) {
90 return this.formatter.format(logObject);
91 }
92
ae85c067 93 return Utilities.stringify(logObject);
58906d77
RBR
94 }
95};