]> git.r.bdr.sh - rbdr/cologne/blobdiff - lib/formatters/token.js
Constrain matching regex to only match 255 chars
[rbdr/cologne] / lib / formatters / token.js
index 4fa7d570aaa0c27fecf8ae2e8028094a31b82d96..05895fa400b77db0e8e53b56e39cba8f630f4065 100644 (file)
@@ -1,58 +1,93 @@
-Module(Cobalt.Formatter, 'Token')({
-  formatString : "{{message}}",
-  replaceRule : /{{(.*?)}}/g,
-  separatorLength : 60,
-  separatorType : "-",
-  format : function (logObject, opts){
-    var indent, indentSize,
-        separatorLength, separatorType,
-        output;
-    indentSize = logObject._indentLevel || 0;
-
-    indent = Array(indentSize + 1).join(' ');
-
-    if (logObject._separator) {
-      separatorLength = logObject._separatorLength || this.separatorLength;
-      separatorType = logObject._separatorType || this.separatorType;
-      output = indent + Array(separatorLength - indentSize + 1).join(separatorType);
-    } else {
-      output = indent + this.parseFormatString(logObject, opts.formatString);
-    }
-
-    if (opts.ansiColor) {
-      output = this.colorize(logObject._level, output);
-    }
-
-    return output;
-  },
-
-  parseFormatString : function (logObject, formatString) {
-    var resultString = '';
-    if (typeof formatString === 'undefined') {
-      formatString = this.formatString;
-    }
-
-    resultString = formatString.replace(this.replaceRule, function(match, paren){
-      return logObject[paren] || "-";
-    });
+'use strict';
+
+const Utilities = require('../utilities');
+
+/**
+ * Token formatter. Given a format string it will attempt to output
+ * a message.
+ *
+ * @memberof Formatters
+ * @implements IFormatter
+ * @class Token
+ */
+module.exports = class TokenFormatter {
+  constructor(config) {
+
+    /**
+     * The string to use as a template string. By default, any property
+     * inside double curly braces `{{likeThis}}` will be extracted from
+     * the object and replaced. If the object does not contain the
+     * property, it will leave it.
+     *
+     * @name formatString
+     * @instance
+     * @memberof Formatters.Token
+     * @type String
+     * @default '{{message}}'
+     */
+    this.formatString = '{{message}}';
+
+    /**
+     * The regex rule to use to match the tokens.
+     *
+     * @name replaceRule
+     * @instance
+     * @memberof Formatters.Token
+     * @type RegExp
+     * @default /{{(.{1,255}?)}}/g
+     */
+    this.replaceRule = /{{(.{1,255}?)}}/g;
+
+    /**
+     * Flag that specifies whether or not to use an isoDate when using
+     * `_timestamp`. If false it will output the raw timestamp.
+     *
+     * @name isoDate
+     * @instance
+     * @memberof Formatters.Token
+     * @type Boolean
+     * @default true
+     */
+    this.isoDate = true;
+
+    this._ansiRe = /_ansi:.+/;
+
+    Object.assign(this, config || {});
+  }
+
+  /**
+   * Main entry point, it will read the incoming log object and convert
+   * all the tokens to their corresponding representation, finally
+   * returning the string.
+   *
+   * @function format
+   * @instance
+   * @memberof Formatters.Token
+   * @param {tCologneLog} log the log to format
+   * @return {String} the formatted object
+   */
+  format(log) {
+
+    const escapeCode = String.fromCharCode(27);
+    return this.formatString.replace(this.replaceRule, (match, token) => {
+
+      if (token === '_timestamp' && this.isoDate) {
+        const date = new Date(Number(log._timestamp) / 1000000);
+        return date.toISOString();
+      }
 
-    return resultString;
-  },
-
-  colorize : function (level, message) {
-    switch(level) {
-      case 0:
-      case 1:
-      case 2:
-      case 3:
-        return message.red;
-      case 4:
-        return message.yellow;
-      case 5:
-      case 6:
-        return message.blue;
-      default:
-        return message;
-    }
+      if (token.match(this._ansiRe)) {
+        const ansiType = token.split(':')[1];
+
+        // Smartish coloring
+        if (ansiType === '_level') {
+          return escapeCode + Utilities.getAnsiCode(Utilities.getLevelAnsi(log._level));
+        }
+
+        return escapeCode + Utilities.getAnsiCode(ansiType);
+      }
+
+      return log[token] || match;
+    });
   }
-});
+};