]> git.r.bdr.sh - rbdr/cologne/blobdiff - lib/loggers/console.js
Update the code.
[rbdr/cologne] / lib / loggers / console.js
index c4031eb2e8c86ebed909bd4d3bf4df2cf1aa9c05..116b04be195342823d745f7039d472f997244adb 100644 (file)
@@ -1,70 +1,94 @@
-Class(Cobalt.Logger, 'JsConsole')({
-  prototype : {
-    console : null,
-    formatterOpts : {},
-
-    init : function (config) {
-      var logger = this,
-          property;
-
-      if (config) {
-        for (property in config) {
-          logger[property] = config[property];
-        }
-      }
-
-      if (!logger.console) {
-        logger.console = console;
-      }
-    },
-
-    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
-          }
-        }
-      }
-
-      switch (severity){
-        case 0:
-        case 1:
-        case 2:
-        case 3:
-          this.console.error.apply(this.console, message);
-          break;
-        case 4:
-          this.console.warn.apply(this.console, message);
-          break;
-        case 5:
-        case 6:
-          this.console.info.apply(this.console, message);
-          break;
-        case 7:
-        default:
-          this.console.log.apply(this.console, message);
-          break;
-      }
-    },
-
-    format : function (logObject) {
-      // Usually what you want to do here is format. Preferably using
-      // someone inside Cobalt.Formatter
-      if (this.formatter) {
-        return this.formatter.format(logObject, this.formatterOpts);
-      }
-
-      return logObject.message;
+'use strict';
+
+const Utilities = require('../utilities');
+
+/**
+ * Logger for the javascript console.
+ *
+ * @implements ILogger
+ * @class Console
+ */
+module.exports = class ConsoleLogger {
+  constructor(config) {
+
+    /**
+     * The console it will write to, can be any object that looks
+     * and acts like a console, including other cologne objects.
+     *
+     * @name console
+     * @instance
+     * @memberof Loggers.Console
+     * @type Object
+     * @default global.console
+     */
+    this.console = console;
+
+    /**
+     * The formatter it will use to output the log. If not present it
+     * will output raw JSON
+     *
+     * @name formatter
+     * @instance
+     * @memberof Loggers.Console
+     * @type IFormatter
+     * @default null
+     */
+    this.formatter = null;
+
+    Object.assign(this, config);
+  }
+
+
+  /**
+   * Main entry point, for each incoming argument it will attempt to
+   * format and send to the console.
+   *
+   * @function log
+   * @instance
+   * @memberof Loggers.Console
+   * @return {undefined}
+   */
+  log(...logs) {
+
+    const formattedLogs = logs.map((log) => ({ log: this._format(log), level: log._level }));
+
+    for (const { log, level } of formattedLogs) {
+      this._log(log, level );
     }
+
+  }
+
+  // Routes an individual log to the appropriatet console
+
+  _log(log, level) {
+
+    switch (level) {
+    case 0:
+    case 1:
+    case 2:
+    case 3:
+      this.console.error(log);
+      break;
+    case 4:
+      this.console.warn(log);
+      break;
+    case 5:
+    case 6:
+      this.console.info(log);
+      break;
+    case 7:
+    default:
+      this.console.log(log);
+      break;
+    }
+  }
+
+  _format(logObject) {
+
+    if (this.formatter) {
+      return this.formatter.format(logObject);
+    }
+
+    return Utilities.stringify(logObject);
   }
-});
+};