]> git.r.bdr.sh - rbdr/cologne/blame - lib/loggers/file.js
Give longer flush timeout
[rbdr/cologne] / lib / loggers / file.js
CommitLineData
58906d77
RBR
1'use strict';
2
ae85c067
RBR
3const Fs = require('fs');
4const Utilities = require('../utilities');
58906d77
RBR
5
6/**
7 * Logger for files.
8 *
ae85c067
RBR
9 * @memberof Loggers
10 * @implements ILogger
58906d77
RBR
11 * @class File
12 */
ae85c067
RBR
13module.exports = class FileLogger {
14 constructor(config) {
58906d77
RBR
15
16 /**
17 * Path to the file it will write to, must be readable.
18 *
19 * @name file
20 * @instance
ae85c067 21 * @memberof Loggers.File
58906d77
RBR
22 * @type string
23 * @default null
24 */
25 this.file = null;
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
ae85c067
RBR
33 * @memberof Loggers.File
34 * @type IFormatter
58906d77
RBR
35 * @default null
36 */
37 this.formatter = null;
38
ae85c067 39 Object.assign(this, config);
58906d77 40
ae85c067 41 this._stream = Fs.createWriteStream(this.file, { flags: 'a' });
58906d77
RBR
42 }
43
44 /**
45 * Main entry point, for each incoming argument it will attempt to
46 * format and send to the stream to be written.
47 *
48 * @function log
49 * @instance
ae85c067 50 * @memberof Loggers.File
58906d77
RBR
51 * @return {undefined}
52 */
ae85c067 53 log(...logs) {
58906d77 54
ae85c067
RBR
55 for (const log of logs) {
56 this._stream.write(this._format(log) + '\n');
58906d77
RBR
57 }
58 }
59
ae85c067
RBR
60 _format(logObject) {
61
58906d77
RBR
62 if (this.formatter) {
63 return this.formatter.format(logObject);
64 }
65
ae85c067 66 return Utilities.stringify(logObject);
58906d77
RBR
67 }
68};