X-Git-Url: https://git.r.bdr.sh/rbdr/cologne/blobdiff_plain/b69497efe78c60742e0bb7ab5fed2b94ce89682e..ea6a7aff0eac09745b215f14fac54a319a2064c2:/lib/formatters/simple.js diff --git a/lib/formatters/simple.js b/lib/formatters/simple.js index 24f65b3..bfaa76c 100644 --- a/lib/formatters/simple.js +++ b/lib/formatters/simple.js @@ -1,30 +1,62 @@ -(function (global) { - var Module; - - // Load up dependencies - if (typeof require === 'function') { - var Ne = require('neon'); - Module = Ne.Module; - } else { - Module = global.Module; +'use strict'; + +const Utilities = require('../utilities'); + +/** + * 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 || {}); } - var Cobalt = {}; - Module(Cobalt, 'Formatter')({}); + /** + * 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); - Cobalt.Formatter.Simple = Module(Cobalt.Formatter, 'Simple')({ - format : function (logObject, opts){ - var indent; + return `[${date}][${levelString}] ${logObject._from}: ${logObject.message}`; + } - indent = Array(logObject._indentLevel + 1).join(' '); + _colorize(levelString, level) { - return indent + logObject.message; + if (!this.colorize) { + return levelString; } - }); - if (typeof require === 'function') { - global.Simple = Cobalt.Formatter.Simple; - } else { - global.Cobalt.Formatter.Simple = Cobalt.Formatter.Simple; + const escapeCode = String.fromCharCode(27); + const color = escapeCode + Utilities.getAnsiCode(Utilities.getLevelAnsi(level)); + const reset = escapeCode + Utilities.getAnsiCode('reset'); + + return color + levelString + reset; } -}(typeof window !== 'undefined' ? window : (typeof exports !== 'undefined' ? exports : self))); +};