]> git.r.bdr.sh - rbdr/cologne/blame - lib/formatters/token.js
Updates new repository address
[rbdr/cologne] / lib / formatters / token.js
CommitLineData
a181aeae
BB
1Module(Cobalt.Formatter, 'Token')({
2 formatString : "{{message}}",
3 replaceRule : /{{(.*?)}}/g,
4 separatorLength : 60,
6c37857f 5 isoDate : true,
a181aeae
BB
6 separatorType : "-",
7 format : function (logObject, opts){
8 var indent, indentSize,
9 separatorLength, separatorType,
6c37857f 10 output, property;
a181aeae
BB
11 indentSize = logObject._indentLevel || 0;
12
6c37857f
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
a181aeae
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 {
6c37857f 29 output = indent + this.parseFormatString(logObject, this.formatString);
a181aeae 30 }
b69497ef 31
6c37857f 32 if (this.ansiColor) {
a181aeae
BB
33 output = this.colorize(logObject._level, output);
34 }
b69497ef 35
a181aeae
BB
36 return output;
37 },
b69497ef 38
a181aeae
BB
39 parseFormatString : function (logObject, formatString) {
40 var resultString = '';
41 if (typeof formatString === 'undefined') {
42 formatString = this.formatString;
b69497ef 43 }
b69497ef 44
a181aeae 45 resultString = formatString.replace(this.replaceRule, function(match, paren){
6c37857f
BB
46 var date;
47 if (paren === "_timestamp" && this.isoDate) {
48 date = new Date(logObject[paren]);
49 return date.toISOString();
50 }
a181aeae 51 return logObject[paren] || "-";
6c37857f 52 }.bind(this));
a181aeae
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 }
b69497ef 72 }
a181aeae 73});