]> git.r.bdr.sh - rbdr/cologne/blame_incremental - lib/formatters/simple.js
Delete npmignore
[rbdr/cologne] / lib / formatters / simple.js
... / ...
CommitLineData
1'use strict';
2
3const Utilities = require('../utilities');
4
5/**
6 * Simple formatter. Outputs a predefined format:
7 * `[{{_timestamp}}][{{_levelString}}] {{_from}}: {{message}}`;
8 *
9 * @memberof Formatters
10 * @implements IFormatter
11 * @class Simple
12 */
13module.exports = class SimpleFormatter {
14
15 constructor(config) {
16
17 /**
18 * Flag that tells us whether or not to use ANSI color. Defaults to
19 * false.
20 *
21 * @name colorize
22 * @instance
23 * @memberof Formatters.Simple
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
38 * @memberof Formatters.Simple
39 * @param {tCologneLog} logObjet the log to format
40 * @return {String} the formatted object
41 */
42 format(logObject) {
43
44 const date = (new Date(Number(logObject._timestamp) / 1000000)).toISOString();
45 const levelString = this._colorize(logObject._levelString, logObject._level);
46
47 return `[${date}][${levelString}] ${logObject._from}: ${logObject.message}`;
48 }
49
50 _colorize(levelString, level) {
51
52 if (!this.colorize) {
53 return levelString;
54 }
55
56 const escapeCode = String.fromCharCode(27);
57 const color = escapeCode + Utilities.getAnsiCode(Utilities.getLevelAnsi(level));
58 const reset = escapeCode + Utilities.getAnsiCode('reset');
59
60 return color + levelString + reset;
61 }
62};