aboutsummaryrefslogtreecommitdiff
path: root/lib/formatters/token.js
blob: 4fa7d570aaa0c27fecf8ae2e8028094a31b82d96 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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);
    }

    if (opts.ansiColor) {
      output = this.colorize(logObject._level, output);
    }

    return output;
  },

  parseFormatString : function (logObject, formatString) {
    var resultString = '';
    if (typeof formatString === 'undefined') {
      formatString = this.formatString;
    }

    resultString = formatString.replace(this.replaceRule, function(match, paren){
      return logObject[paren] || "-";
    });

    return resultString;
  },

  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;
    }
  }
});