]> git.r.bdr.sh - rbdr/cologne/blob - lib/cologne/logger/console.js
3a73e420b569690224ec75a2cdb0ea96b504ac05
[rbdr/cologne] / lib / cologne / logger / console.js
1 'use strict';
2
3 let LogUtilities = require('../log_utilities');
4
5 /**
6 * Logger for the javascript console.
7 *
8 * @memberof Cologne.Logger
9 * @implements Cologne.ILogger
10 * @class Console
11 */
12 let ConsoleLogger = 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 Cologne.Logger.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 Cologne.Logger.Console
34 * @type Cologne.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 Cologne.Logger.Console
50 * @return {undefined}
51 */
52 log () {
53 let logObject, messages, severity;
54
55 messages = [];
56
57 for (logObject of arguments) {
58 messages.push(this._format(logObject));
59
60 if (!severity) {
61 severity = logObject._level;
62 }
63 }
64
65 switch(severity) {
66 case 0:
67 case 1:
68 case 2:
69 case 3:
70 this.console.error.apply(this.console, messages);
71 break;
72 case 4:
73 this.console.warn.apply(this.console, messages);
74 break;
75 case 5:
76 case 6:
77 this.console.info.apply(this.console, messages);
78 break;
79 case 7:
80 default:
81 this.console.log.apply(this.console, messages);
82 break;
83 }
84 }
85
86 _format (logObject) {
87 if (this.formatter) {
88 return this.formatter.format(logObject);
89 }
90
91 return LogUtilities.stringify(logObject);
92 }
93 };
94
95 module.exports = ConsoleLogger;