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