X-Git-Url: https://git.r.bdr.sh/rbdr/cologne/blobdiff_plain/6c37857f165d160ede4bf1a1b02b1204655633f3..a19a990d7ab1af30e4045f24871ebd1b7daf3d6f:/lib/formatters/token.js?ds=sidebyside diff --git a/lib/formatters/token.js b/lib/formatters/token.js index f3afba2..2ec3a17 100644 --- a/lib/formatters/token.js +++ b/lib/formatters/token.js @@ -1,73 +1,93 @@ -Module(Cobalt.Formatter, 'Token')({ - formatString : "{{message}}", - replaceRule : /{{(.*?)}}/g, - separatorLength : 60, - isoDate : true, - separatorType : "-", - format : function (logObject, opts){ - var indent, indentSize, - separatorLength, separatorType, - output, property; - indentSize = logObject._indentLevel || 0; +'use strict'; - // Extend opts - if (opts) { - for (property in opts) { - if (opts.hasOwnProperty(property)) { - this[property] = opts[property]; - } - } - } +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 /{{(.*)}}/g + */ + this.replaceRule = /{{(.*?)}}/g; - indent = Array(indentSize + 1).join(' '); + /** + * 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; - 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, this.formatString); - } + this._ansiRe = /_ansi:.+/; - if (this.ansiColor) { - output = this.colorize(logObject._level, output); - } + Object.assign(this, config || {}); + } - return output; - }, + /** + * 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) { - parseFormatString : function (logObject, formatString) { - var resultString = ''; - if (typeof formatString === 'undefined') { - formatString = this.formatString; - } + const escapeCode = String.fromCharCode(27); + return this.formatString.replace(this.replaceRule, (match, token) => { - resultString = formatString.replace(this.replaceRule, function(match, paren){ - var date; - if (paren === "_timestamp" && this.isoDate) { - date = new Date(logObject[paren]); + if (token === '_timestamp' && this.isoDate) { + const date = new Date(Number(log._timestamp) / 1000000); return date.toISOString(); } - return logObject[paren] || "-"; - }.bind(this)); - return resultString; - }, + 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); + } - 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; - } + return log[token] || match; + }); } -}); +};