]>
Commit | Line | Data |
---|---|---|
bfd26c00 RBR |
1 | 'use strict'; |
2 | ||
b3847a2e | 3 | const Utilities = require('../utilities'); |
bfd26c00 RBR |
4 | |
5 | /** | |
6 | * Simple formatter. Outputs a predefined format: | |
7 | * `[{{_timestamp}}][{{_levelString}}] {{_from}}: {{message}}`; | |
8 | * | |
b3847a2e RBR |
9 | * @memberof Formatters |
10 | * @implements IFormatter | |
bfd26c00 RBR |
11 | * @class Simple |
12 | */ | |
b3847a2e | 13 | module.exports = class SimpleFormatter { |
bfd26c00 | 14 | |
b3847a2e | 15 | constructor(config) { |
bfd26c00 RBR |
16 | |
17 | /** | |
18 | * Flag that tells us whether or not to use ANSI color. Defaults to | |
19 | * false. | |
20 | * | |
21 | * @name colorize | |
22 | * @instance | |
b3847a2e | 23 | * @memberof Formatters.Simple |
bfd26c00 RBR |
24 | * @type Boolean |
25 | * @default false | |
26 | */ | |
27 | this.colorize = false; | |
28 | ||
29 | Object.assign(this, config || {}); | |
30 | } | |
31 | ||
32 | /** | |
33 | * Main entry point, it will read the incoming log object and convert | |
34 | * it to the output string. | |
35 | * | |
36 | * @function format | |
37 | * @instance | |
b3847a2e RBR |
38 | * @memberof Formatters.Simple |
39 | * @param {tCologneLog} logObjet the log to format | |
bfd26c00 RBR |
40 | * @return {String} the formatted object |
41 | */ | |
b3847a2e | 42 | format(logObject) { |
bfd26c00 | 43 | |
b3847a2e RBR |
44 | const date = (new Date(Number(logObject._timestamp) / 1000000)).toISOString(); |
45 | const levelString = this._colorize(logObject._levelString, logObject._level); | |
bfd26c00 RBR |
46 | |
47 | return `[${date}][${levelString}] ${logObject._from}: ${logObject.message}`; | |
48 | } | |
49 | ||
b3847a2e | 50 | _colorize(levelString, level) { |
bfd26c00 RBR |
51 | |
52 | if (!this.colorize) { | |
53 | return levelString; | |
54 | } | |
55 | ||
b3847a2e RBR |
56 | const escapeCode = String.fromCharCode(27); |
57 | const color = escapeCode + Utilities.getAnsiCode(Utilities.getLevelAnsi(level)); | |
58 | const reset = escapeCode + Utilities.getAnsiCode('reset'); | |
bfd26c00 RBR |
59 | |
60 | return color + levelString + reset; | |
61 | } | |
62 | }; |