X-Git-Url: https://git.r.bdr.sh/rbdr/cologne/blobdiff_plain/39694ecf6a38515467817d63acef0869d69ee7bb..ea6a7aff0eac09745b215f14fac54a319a2064c2:/lib/loggers/file.js?ds=sidebyside diff --git a/lib/loggers/file.js b/lib/loggers/file.js index 5ba8a17..50a251e 100644 --- a/lib/loggers/file.js +++ b/lib/loggers/file.js @@ -1,48 +1,68 @@ -var fs = require('fs'); - -Class(Cobalt.Logger, 'File')({ - prototype : { - file : null, - formatterOpts : {}, - - init : function (config) { - if (config) { - for (property in config) { - this[property] = config[property]; - } - } - - this._stream = fs.createWriteStream(this.file); - }, - - log : function () { - var i, message = [], severity; - - for (i = 0; i < arguments.length; i++) { - // We're not formatting objects for now. - - if (!arguments[i].__skipConsole && !arguments[i].message.__skipConsole) { - message.push(this.format(arguments[i])); - if (!severity) { - severity = arguments[i]._level - } - } - } - - for (i = 0; i < message.length; i++) { - this._stream.write(message[i] + '\n'); - } - }, - - format : function (logObject) { - if (this.formatter) { - if (typeof logObject.message === 'object') { - return logObject.message; - } - return this.formatter.format(logObject, this.formatterOpts); - } - - return Cobalt.stringify(logObject); +'use strict'; + +const Fs = require('fs'); +const Utilities = require('../utilities'); + +/** + * Logger for files. + * + * @memberof Loggers + * @implements ILogger + * @class File + */ +module.exports = class FileLogger { + constructor(config) { + + /** + * Path to the file it will write to, must be readable. + * + * @name file + * @instance + * @memberof Loggers.File + * @type string + * @default null + */ + this.file = null; + + /** + * The formatter it will use to output the log. If not present it + * will output raw JSON + * + * @name formatter + * @instance + * @memberof Loggers.File + * @type IFormatter + * @default null + */ + this.formatter = null; + + Object.assign(this, config); + + this._stream = Fs.createWriteStream(this.file, { flags: 'a' }); + } + + /** + * Main entry point, for each incoming argument it will attempt to + * format and send to the stream to be written. + * + * @function log + * @instance + * @memberof Loggers.File + * @return {undefined} + */ + log(...logs) { + + for (const log of logs) { + this._stream.write(this._format(log) + '\n'); } } -}); + + _format(logObject) { + + if (this.formatter) { + return this.formatter.format(logObject); + } + + return Utilities.stringify(logObject); + } +};