X-Git-Url: https://git.r.bdr.sh/rbdr/cologne/blobdiff_plain/a181aeae67a9c9c86e9df92ab5c0de6ce00f47fb..0499d2037c5d4b0c9e5c3ce79a222b8fbd25b3a9:/lib/formatters/simple.js diff --git a/lib/formatters/simple.js b/lib/formatters/simple.js index 6d2ad7d..bfaa76c 100644 --- a/lib/formatters/simple.js +++ b/lib/formatters/simple.js @@ -1,9 +1,62 @@ -Module(Cobalt.Formatter, 'Simple')({ - format : function (logObject, opts){ - var indent; +'use strict'; - indent = Array(logObject._indentLevel + 1).join(' '); +const Utilities = require('../utilities'); - return indent + logObject.message; +/** + * Simple formatter. Outputs a predefined format: + * `[{{_timestamp}}][{{_levelString}}] {{_from}}: {{message}}`; + * + * @memberof Formatters + * @implements IFormatter + * @class Simple + */ +module.exports = class SimpleFormatter { + + constructor(config) { + + /** + * Flag that tells us whether or not to use ANSI color. Defaults to + * false. + * + * @name colorize + * @instance + * @memberof Formatters.Simple + * @type Boolean + * @default false + */ + this.colorize = false; + + Object.assign(this, config || {}); + } + + /** + * Main entry point, it will read the incoming log object and convert + * it to the output string. + * + * @function format + * @instance + * @memberof Formatters.Simple + * @param {tCologneLog} logObjet the log to format + * @return {String} the formatted object + */ + format(logObject) { + + const date = (new Date(Number(logObject._timestamp) / 1000000)).toISOString(); + const levelString = this._colorize(logObject._levelString, logObject._level); + + return `[${date}][${levelString}] ${logObject._from}: ${logObject.message}`; + } + + _colorize(levelString, level) { + + if (!this.colorize) { + return levelString; } -}); + + const escapeCode = String.fromCharCode(27); + const color = escapeCode + Utilities.getAnsiCode(Utilities.getLevelAnsi(level)); + const reset = escapeCode + Utilities.getAnsiCode('reset'); + + return color + levelString + reset; + } +};