]> git.r.bdr.sh - rbdr/cologne/blob - test/cologne/logger/file.js
01be09f150718c9a795730c9083da340e4c56a40
[rbdr/cologne] / test / cologne / logger / file.js
1 'use strict';
2
3 let fs = require('fs');
4
5 let tap = require('tap');
6
7 let FileLogger = require('../../../lib/cologne/logger/file');
8
9 // Prepare the test
10 let logObjectA, logObjectB, rawFile, formatterFile, rawLogger, formatterLogger,
11 params, dummyFormatter, formatterString;
12
13 rawFile = './raw.log';
14 formatterFile = './formatter.log';
15 formatterString = 'example';
16
17 logObjectA = {
18 _timestamp: Date.now() + .134,
19 _cologneLog: '1.0.0',
20 _from: 'Dummy Logger',
21 _level: 6,
22 _levelString: 'info',
23 message: 'MessageOne'
24 };
25
26 logObjectB = {
27 _timestamp: Date.now() + .134,
28 _cologneLog: '1.0.0',
29 _from: 'Dummy Logger',
30 _level: 6,
31 _levelString: 'info',
32 message: 'MessageTwo'
33 };
34
35 params = [logObjectA, logObjectB];
36
37
38 dummyFormatter = {
39 values: [],
40 format: function (logObject) {
41 this.values.push(logObject);
42 return formatterString;
43 }
44 };
45
46 rawLogger = new FileLogger({
47 file: rawFile
48 });
49 formatterLogger = new FileLogger({
50 file: formatterFile,
51 formatter: dummyFormatter
52 });
53
54 /**
55 * TEST: #log() - regular
56 */
57
58 rawLogger.log.apply(rawLogger, params);
59
60 setTimeout(function () {
61 tap.test('raw file', function (t) {
62 fs.readFile(rawFile, {encoding: 'utf8'}, function (error, contents) {
63 let lines;
64
65 lines = contents.trim().split('\n');
66
67 tap.equal(lines.length, params.length,
68 'it should send all params to the file');
69
70 tap.equal(JSON.stringify(logObjectA), lines[0],
71 'it should log the raw json object');
72
73 fs.unlink(rawFile, function () {
74 t.end();
75 });
76 });
77 });
78 }, 10); // allow for flush
79 /**
80 * TEST: #log() - formatter
81 */
82
83 formatterLogger.log.apply(formatterLogger, params);
84
85 setTimeout(function () {
86 tap.test('formatted file', function (t) {
87 fs.readFile(formatterFile, {encoding: 'utf8'}, function (error, contents) {
88 let lines;
89
90 lines = contents.trim().split('\n');
91
92 tap.equal(lines.length, params.length,
93 'it should send all params to the file');
94
95 tap.equal(formatterString, lines[0],
96 'it should log the formatted object');
97
98 fs.unlink(formatterFile, function () {
99 t.end();
100 });
101 });
102 });
103 }, 10);