]>
Commit | Line | Data |
---|---|---|
bfd26c00 RBR |
1 | 'use strict'; |
2 | ||
b3847a2e | 3 | const Utilities = require('../utilities'); |
bfd26c00 RBR |
4 | |
5 | /** | |
6 | * Logger for the javascript console. | |
7 | * | |
b3847a2e | 8 | * @implements ILogger |
bfd26c00 RBR |
9 | * @class Console |
10 | */ | |
b3847a2e RBR |
11 | module.exports = class ConsoleLogger { |
12 | constructor(config) { | |
bfd26c00 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 | |
b3847a2e | 20 | * @memberof Loggers.Console |
bfd26c00 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 | |
b3847a2e RBR |
32 | * @memberof Loggers.Console |
33 | * @type IFormatter | |
bfd26c00 RBR |
34 | * @default null |
35 | */ | |
36 | this.formatter = null; | |
37 | ||
b3847a2e | 38 | Object.assign(this, config); |
bfd26c00 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 | |
b3847a2e | 48 | * @memberof Loggers.Console |
bfd26c00 RBR |
49 | * @return {undefined} |
50 | */ | |
b3847a2e | 51 | log(...logs) { |
bfd26c00 | 52 | |
b3847a2e | 53 | const formattedLogs = logs.map((log) => ({ log: this._format(log), level: log._level })); |
bfd26c00 | 54 | |
b3847a2e RBR |
55 | for (const { log, level } of formattedLogs) { |
56 | this._log(log, level ); | |
bfd26c00 RBR |
57 | } |
58 | ||
b3847a2e RBR |
59 | } |
60 | ||
61 | // Routes an individual log to the appropriatet console | |
62 | ||
63 | _log(log, level) { | |
64 | ||
65 | switch (level) { | |
bfd26c00 RBR |
66 | case 0: |
67 | case 1: | |
68 | case 2: | |
69 | case 3: | |
b3847a2e | 70 | this.console.error(log); |
bfd26c00 RBR |
71 | break; |
72 | case 4: | |
b3847a2e | 73 | this.console.warn(log); |
bfd26c00 RBR |
74 | break; |
75 | case 5: | |
76 | case 6: | |
b3847a2e | 77 | this.console.info(log); |
bfd26c00 RBR |
78 | break; |
79 | case 7: | |
80 | default: | |
b3847a2e | 81 | this.console.log(log); |
bfd26c00 RBR |
82 | break; |
83 | } | |
84 | } | |
85 | ||
b3847a2e RBR |
86 | _format(logObject) { |
87 | ||
bfd26c00 RBR |
88 | if (this.formatter) { |
89 | return this.formatter.format(logObject); | |
90 | } | |
91 | ||
b3847a2e | 92 | return Utilities.stringify(logObject); |
bfd26c00 RBR |
93 | } |
94 | }; |