]>
Commit | Line | Data |
---|---|---|
db6bc3cb BB |
1 | Module(Cobalt.Formatter, 'Token')({ |
2 | formatString : "{{message}}", | |
3 | replaceRule : /{{(.*?)}}/g, | |
4 | separatorLength : 60, | |
3be15ee8 | 5 | isoDate : true, |
db6bc3cb BB |
6 | separatorType : "-", |
7 | format : function (logObject, opts){ | |
8 | var indent, indentSize, | |
9 | separatorLength, separatorType, | |
3be15ee8 | 10 | output, property; |
db6bc3cb BB |
11 | indentSize = logObject._indentLevel || 0; |
12 | ||
3be15ee8 BB |
13 | // Extend opts |
14 | if (opts) { | |
15 | for (property in opts) { | |
16 | if (opts.hasOwnProperty(property)) { | |
17 | this[property] = opts[property]; | |
18 | } | |
19 | } | |
20 | } | |
21 | ||
db6bc3cb BB |
22 | indent = Array(indentSize + 1).join(' '); |
23 | ||
24 | if (logObject._separator) { | |
25 | separatorLength = logObject._separatorLength || this.separatorLength; | |
26 | separatorType = logObject._separatorType || this.separatorType; | |
27 | output = indent + Array(separatorLength - indentSize + 1).join(separatorType); | |
28 | } else { | |
3be15ee8 | 29 | output = indent + this.parseFormatString(logObject, this.formatString); |
db6bc3cb | 30 | } |
bdedb5e5 | 31 | |
3be15ee8 | 32 | if (this.ansiColor) { |
db6bc3cb BB |
33 | output = this.colorize(logObject._level, output); |
34 | } | |
bdedb5e5 | 35 | |
db6bc3cb BB |
36 | return output; |
37 | }, | |
bdedb5e5 | 38 | |
db6bc3cb BB |
39 | parseFormatString : function (logObject, formatString) { |
40 | var resultString = ''; | |
41 | if (typeof formatString === 'undefined') { | |
42 | formatString = this.formatString; | |
bdedb5e5 | 43 | } |
bdedb5e5 | 44 | |
db6bc3cb | 45 | resultString = formatString.replace(this.replaceRule, function(match, paren){ |
3be15ee8 BB |
46 | var date; |
47 | if (paren === "_timestamp" && this.isoDate) { | |
48 | date = new Date(logObject[paren]); | |
49 | return date.toISOString(); | |
50 | } | |
db6bc3cb | 51 | return logObject[paren] || "-"; |
3be15ee8 | 52 | }.bind(this)); |
db6bc3cb BB |
53 | |
54 | return resultString; | |
55 | }, | |
56 | ||
57 | colorize : function (level, message) { | |
58 | switch(level) { | |
59 | case 0: | |
60 | case 1: | |
61 | case 2: | |
62 | case 3: | |
63 | return message.red; | |
64 | case 4: | |
65 | return message.yellow; | |
66 | case 5: | |
67 | case 6: | |
68 | return message.blue; | |
69 | default: | |
70 | return message; | |
71 | } | |
bdedb5e5 | 72 | } |
db6bc3cb | 73 | }); |