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