X-Git-Url: https://git.r.bdr.sh/rbdr/cologne/blobdiff_plain/58906d77975b35fe93569f8083b90140124f9c41..ae85c067634676251e812765c81adfdef8f85f9d:/test/loggers/file.js diff --git a/test/loggers/file.js b/test/loggers/file.js new file mode 100644 index 0000000..5e73d82 --- /dev/null +++ b/test/loggers/file.js @@ -0,0 +1,106 @@ +'use strict'; + +const Fs = require('fs'); + +const Tap = require('tap'); + +const FileLogger = require('../../lib/loggers/file'); + +// Prepare the test + +const rawFile = './raw.log'; +const formatterFile = './formatter.log'; +const formatterString = 'example'; + +const logObjectA = { + _timestamp: Date.now() + .134, + _cologneLog: '1.0.0', + _from: 'Dummy Logger', + _level: 6, + _levelString: 'info', + message: 'MessageOne' +}; + +const logObjectB = { + _timestamp: Date.now() + .134, + _cologneLog: '1.0.0', + _from: 'Dummy Logger', + _level: 6, + _levelString: 'info', + message: 'MessageTwo' +}; + +const logs = [logObjectA, logObjectB]; + + +const dummyFormatter = { + values: [], + format: function (logObject) { + + this.values.push(logObject); + return formatterString; + } +}; + +const rawLogger = new FileLogger({ + file: rawFile +}); +const formatterLogger = new FileLogger({ + file: formatterFile, + formatter: dummyFormatter +}); + +/** + * TEST: #log() - regular + */ + +rawLogger.log(...logs); + +setTimeout(() => { + + Tap.test('raw file', (t) => { + + Fs.readFile(rawFile, { encoding: 'utf8' }, (_, contents) => { + + const lines = contents.trim().split('\n'); + + Tap.equal(lines.length, logs.length, + 'it should send all params to the file'); + + Tap.equal(JSON.stringify(logObjectA), lines[0], + 'it should log the raw json object'); + + Fs.unlink(rawFile, () => { + + t.end(); + }); + }); + }); +}, 10); // allow for flush +/** + * TEST: #log() - formatter + */ + +formatterLogger.log(...logs); + +setTimeout(() => { + + Tap.test('formatted file', (t) => { + + Fs.readFile(formatterFile, { encoding: 'utf8' }, (_, contents) => { + + const lines = contents.trim().split('\n'); + + Tap.equal(lines.length, logs.length, + 'it should send all params to the file'); + + Tap.equal(formatterString, lines[0], + 'it should log the formatted object'); + + Fs.unlink(formatterFile, () => { + + t.end(); + }); + }); + }); +}, 10);