X-Git-Url: https://git.r.bdr.sh/rbdr/cologne/blobdiff_plain/b69497efe78c60742e0bb7ab5fed2b94ce89682e..ea6a7aff0eac09745b215f14fac54a319a2064c2:/lib/formatters/token.js?ds=sidebyside diff --git a/lib/formatters/token.js b/lib/formatters/token.js index 40d4f59..05895fa 100644 --- a/lib/formatters/token.js +++ b/lib/formatters/token.js @@ -1,79 +1,93 @@ -(function (global) { - var Module; +'use strict'; - // Load up dependencies - if (typeof require === 'function') { - var Ne = require('neon'); - Module = Ne.Module; - } else { - Module = global.Module; - } +const Utilities = require('../utilities'); - var Cobalt = {}; - Module(Cobalt, 'Formatter')({}); +/** + * 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) { - Cobalt.Formatter.Token = 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; + /** + * 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}}'; - indent = Array(indentSize + 1).join(' '); + /** + * 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; - 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); - } + /** + * 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 (opts.ansiColor) { - output = this.colorize(logObject._level, output); - } + 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) { - return output; - }, + const escapeCode = String.fromCharCode(27); + return this.formatString.replace(this.replaceRule, (match, token) => { - parseFormatString : function (logObject, formatString) { - var resultString = ''; - if (typeof formatString === 'undefined') { - formatString = this.formatString; + if (token === '_timestamp' && this.isoDate) { + const date = new Date(Number(log._timestamp) / 1000000); + return date.toISOString(); } - resultString = formatString.replace(this.replaceRule, function(match, paren){ - return logObject[paren] || "-"; - }); + if (token.match(this._ansiRe)) { + const ansiType = token.split(':')[1]; - return resultString; - }, + // Smartish coloring + if (ansiType === '_level') { + return escapeCode + Utilities.getAnsiCode(Utilities.getLevelAnsi(log._level)); + } - 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 escapeCode + Utilities.getAnsiCode(ansiType); } - } - }); - if (typeof require === 'function') { - global.Token = Cobalt.Formatter.Token; - } else { - global.Cobalt.Formatter.Token = Cobalt.Formatter.Token; + return log[token] || match; + }); } -}(typeof window !== 'undefined' ? window : (typeof exports !== 'undefined' ? exports : self))); +};