X-Git-Url: https://git.r.bdr.sh/rbdr/cologne/blobdiff_plain/204d9a1e0a3dd36bc3f91800ab82e9075610b94e..refs/heads/main:/lib/loggers/file.js diff --git a/lib/loggers/file.js b/lib/loggers/file.js index 0512ace..50a251e 100644 --- a/lib/loggers/file.js +++ b/lib/loggers/file.js @@ -1,49 +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) { - if (typeof arguments[i].message === 'object') { - message.push(arguments[i].message); - } else { - 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) { - return this.formatter.format(logObject, this.formatterOpts); - } - - return logObject.message; +'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); + } +};