]> git.r.bdr.sh - rbdr/cologne/blob - lib/cologne/logger/file.js
e9a56e72071d1fdd658738a68d9809f51bf1a741
[rbdr/cologne] / lib / cologne / logger / file.js
1 'use strict';
2
3 let fs = require('fs');
4
5 let LogUtilities = require('../log_utilities');
6
7 /**
8 * Logger for files.
9 *
10 * @memberof Cologne.Logger
11 * @implements Cologne.ILogger
12 * @class File
13 */
14 let FileLogger = class FileLogger {
15 constructor (config) {
16
17 /**
18 * Path to the file it will write to, must be readable.
19 *
20 * @name file
21 * @instance
22 * @memberof Cologne.Logger.File
23 * @type string
24 * @default null
25 */
26 this.file = null;
27
28 /**
29 * The formatter it will use to output the log. If not present it
30 * will output raw JSON
31 *
32 * @name formatter
33 * @instance
34 * @memberof Cologne.Logger.File
35 * @type Cologne.IFormatter
36 * @default null
37 */
38 this.formatter = null;
39
40 Object.assign(this, config || {});
41
42 this._stream = fs.createWriteStream(this.file, {flags: 'a'});
43 }
44
45 /**
46 * Main entry point, for each incoming argument it will attempt to
47 * format and send to the stream to be written.
48 *
49 * @function log
50 * @instance
51 * @memberof Cologne.Logger.File
52 * @return {undefined}
53 */
54 log () {
55 let logObject;
56
57 for (logObject of arguments) {
58 this._stream.write(this.format(logObject) + '\n');
59 }
60 }
61
62 format (logObject) {
63 if (this.formatter) {
64 return this.formatter.format(logObject);
65 }
66
67 return LogUtilities.stringify(logObject);
68 }
69 };
70
71 module.exports = FileLogger;