]> git.r.bdr.sh - rbdr/cologne/commitdiff
Adds the file logger
authorBen Beltran <redacted>
Fri, 9 Jan 2015 16:22:58 +0000 (10:22 -0600)
committerBen Beltran <redacted>
Fri, 9 Jan 2015 16:22:58 +0000 (10:22 -0600)
lib/loggers/file.js [new file with mode: 0644]

diff --git a/lib/loggers/file.js b/lib/loggers/file.js
new file mode 100644 (file)
index 0000000..0512ace
--- /dev/null
@@ -0,0 +1,49 @@
+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;
+    }
+  }
+});