diff options
| author | Ruben Beltran del Rio <ruben@unlimited.pizza> | 2020-09-21 01:00:19 +0200 |
|---|---|---|
| committer | Ruben Beltran del Rio <ruben@unlimited.pizza> | 2020-09-21 01:00:19 +0200 |
| commit | ae85c067634676251e812765c81adfdef8f85f9d (patch) | |
| tree | ea1557df2172aeb30a363afec6a2183f05469d22 /lib/formatters | |
| parent | 58906d77975b35fe93569f8083b90140124f9c41 (diff) | |
Update the code.
Diffstat (limited to 'lib/formatters')
| -rw-r--r-- | lib/formatters/simple.js | 62 | ||||
| -rw-r--r-- | lib/formatters/token.js | 93 |
2 files changed, 155 insertions, 0 deletions
diff --git a/lib/formatters/simple.js b/lib/formatters/simple.js new file mode 100644 index 0000000..bfaa76c --- /dev/null +++ b/lib/formatters/simple.js @@ -0,0 +1,62 @@ +'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 || {}); + } + + /** + * 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; + } +}; diff --git a/lib/formatters/token.js b/lib/formatters/token.js new file mode 100644 index 0000000..2ec3a17 --- /dev/null +++ b/lib/formatters/token.js @@ -0,0 +1,93 @@ +'use strict'; + +const Utilities = require('../utilities'); + +/** + * Token formatter. Given a format string it will attempt to output + * a message. + * + * @memberof Formatters + * @implements IFormatter + * @class Token + */ +module.exports = class TokenFormatter { + constructor(config) { + + /** + * The string to use as a template string. By default, any property + * inside double curly braces `{{likeThis}}` will be extracted from + * the object and replaced. If the object does not contain the + * property, it will leave it. + * + * @name formatString + * @instance + * @memberof Formatters.Token + * @type String + * @default '{{message}}' + */ + this.formatString = '{{message}}'; + + /** + * The regex rule to use to match the tokens. + * + * @name replaceRule + * @instance + * @memberof Formatters.Token + * @type RegExp + * @default /{{(.*)}}/g + */ + this.replaceRule = /{{(.*?)}}/g; + + /** + * Flag that specifies whether or not to use an isoDate when using + * `_timestamp`. If false it will output the raw timestamp. + * + * @name isoDate + * @instance + * @memberof Formatters.Token + * @type Boolean + * @default true + */ + this.isoDate = true; + + this._ansiRe = /_ansi:.+/; + + Object.assign(this, config || {}); + } + + /** + * Main entry point, it will read the incoming log object and convert + * all the tokens to their corresponding representation, finally + * returning the string. + * + * @function format + * @instance + * @memberof Formatters.Token + * @param {tCologneLog} log the log to format + * @return {String} the formatted object + */ + format(log) { + + const escapeCode = String.fromCharCode(27); + return this.formatString.replace(this.replaceRule, (match, token) => { + + if (token === '_timestamp' && this.isoDate) { + const date = new Date(Number(log._timestamp) / 1000000); + return date.toISOString(); + } + + if (token.match(this._ansiRe)) { + const ansiType = token.split(':')[1]; + + // Smartish coloring + if (ansiType === '_level') { + return escapeCode + Utilities.getAnsiCode(Utilities.getLevelAnsi(log._level)); + } + + return escapeCode + Utilities.getAnsiCode(ansiType); + } + + return log[token] || match; + }); + } +}; |