X-Git-Url: https://git.r.bdr.sh/rbdr/cologne/blobdiff_plain/71a808fe142f1c10f9e7de90877fb6e4ffc70158..97a7ece70e89b973468b62e1775a33762a37258e:/lib/formatters/token.js?ds=sidebyside diff --git a/lib/formatters/token.js b/lib/formatters/token.js index f562f1c..f3afba2 100644 --- a/lib/formatters/token.js +++ b/lib/formatters/token.js @@ -1,83 +1,73 @@ -(function (global) { - var Module; +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; - // Load up dependencies - if (typeof require === 'function') { - var Ne = require('neon'); - Module = Ne.Module; - } else { - Module = global.Module; - } - - var Cobalt = {}; - Module(Cobalt, 'Formatter')({}); - - 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; - - 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); + // Extend opts + if (opts) { + for (property in opts) { + if (opts.hasOwnProperty(property)) { + this[property] = opts[property]; + } } + } - if (opts.ansiColor) { - output = this.colorize(logObject._level, output); - } + indent = Array(indentSize + 1).join(' '); - return output; - }, + 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); + } - parseFormatString : function (logObject, formatString) { - var resultString = ''; - if (typeof formatString === 'undefined') { - formatString = this.formatString; - } + if (this.ansiColor) { + output = this.colorize(logObject._level, output); + } - resultString = formatString.replace(this.replaceRule, function(match, paren){ - return logObject[paren] || "-"; - }); + return output; + }, - return resultString; - }, + parseFormatString : function (logObject, formatString) { + var resultString = ''; + if (typeof formatString === 'undefined') { + formatString = this.formatString; + } - 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; + resultString = formatString.replace(this.replaceRule, function(match, paren){ + var date; + if (paren === "_timestamp" && this.isoDate) { + date = new Date(logObject[paren]); + return date.toISOString(); } - } - }); + return logObject[paren] || "-"; + }.bind(this)); - if (Cobalt.Formatter.Token.__objectSpy) { - Cobalt.Formatter.Token.__objectSpy.destroy(); - } + return resultString; + }, - if (typeof require === 'function') { - global.Token = Cobalt.Formatter.Token; - } else { - global.Cobalt.Formatter.Token = Cobalt.Formatter.Token; + 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; + } } -}(typeof window !== 'undefined' ? window : (typeof exports !== 'undefined' ? exports : self))); +});