]> git.r.bdr.sh - rbdr/cologne/blobdiff - lib/loggers/file.js
Update the code.
[rbdr/cologne] / lib / loggers / file.js
index 0512ace110910559389916b3a63f870f0341edf3..50a251e1c58ab7a500b322c1aa76de91c990e324 100644 (file)
@@ -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);
+  }
+};