]> git.r.bdr.sh - rbdr/cologne/blob - test/cologne/logger/console.js
ffc1876249923d53c63a3ca520d11ecb5f698c1b
[rbdr/cologne] / test / cologne / logger / console.js
1 'use strict';
2
3 let tap = require('tap');
4
5 let ConsoleLogger = require('../../../lib/cologne/logger/console');
6
7 // Prepare the test
8 let dummyFormatter, dummyConsole, logObjectA, logObjectB, regularLogger,
9 overrideLogger, formattedLogger, params;
10
11 dummyFormatter = {
12 values: [],
13 format: function (logObject) {
14 this.values.push(logObject);
15 return 'lol';
16 }
17 };
18
19 dummyConsole = {
20 values: {},
21 _log: function (type, args) {
22 this.values[type] = args;
23 },
24 log: function() {
25 this._log('log', Array.prototype.splice.call(arguments, [0]));
26 },
27 warn: function() {
28 this._log('warn', Array.prototype.splice.call(arguments, [0]));
29 },
30 error: function() {
31 this._log('error', Array.prototype.splice.call(arguments, [0]));
32 },
33 info: function() {
34 this._log('info', Array.prototype.splice.call(arguments, [0]));
35 }
36 };
37
38 logObjectA = {
39 _timestamp: Date.now() + .134,
40 _cologneLog: '1.0.0',
41 _from: 'Dummy Logger',
42 _level: 6,
43 _levelString: 'info',
44 message: 'MessageOne'
45 };
46
47 logObjectB = {
48 _timestamp: Date.now() + .134,
49 _cologneLog: '1.0.0',
50 _from: 'Dummy Logger',
51 _level: 6,
52 _levelString: 'info',
53 message: 'MessageTwo'
54 };
55
56 params = [logObjectA, logObjectB];
57
58 regularLogger = new ConsoleLogger({});
59 overrideLogger = new ConsoleLogger({
60 console: dummyConsole
61 });
62 formattedLogger = new ConsoleLogger({
63 console: dummyConsole,
64 formatter: dummyFormatter
65 });
66
67 /**
68 * TEST: #log() - regular
69 */
70
71 tap.equal(regularLogger.console, global.console,
72 'It should default to the global console');
73
74 /**
75 * TEST: #log() - override
76 */
77
78 logObjectA._level = 5;
79 logObjectB._level = 6;
80 overrideLogger.log.apply(overrideLogger, params); // should go to info
81
82 logObjectA._level = 4;
83 logObjectB._level = 4;
84 overrideLogger.log.apply(overrideLogger, params); // should go to warn
85
86 logObjectA._level = 1;
87 logObjectB._level = 3;
88 overrideLogger.log.apply(overrideLogger, params); // should go to error
89
90 logObjectA._level = 7;
91 logObjectB._level = 7;
92 overrideLogger.log.apply(overrideLogger, params); // should go to log
93
94 tap.equal(dummyConsole.values.log.length, params.length,
95 'It should send debug messages to console\'s #log');
96 tap.equal(dummyConsole.values.info.length, params.length,
97 'It should send info and notice messages to console\'s #info');
98 tap.equal(dummyConsole.values.warn.length, params.length,
99 'It should send warn messages to console\'s #warn');
100 tap.equal(dummyConsole.values.error.length, params.length,
101 'It should send error messages to console\'s #error');
102
103 /**
104 * TEST: #log() - with formatter
105 */
106
107 formattedLogger.log.apply(formattedLogger, params); // should go to log
108
109 tap.similar(dummyFormatter.values, params,
110 'If available, it should send the objects to the formatter');