]>
git.r.bdr.sh - rbdr/cologne/blob - lib/cologne/formatter/token.js
bb905ca260ed853fef0f521c6450c357e51303fa
3 let LogUtilities
= require('../log_utilities');
6 * Token formatter. Given a format string it will attempt to output
9 * @memberof Cologne.Formatter
10 * @implements Cologne.IFormatter
13 let TokenFormatter
= class TokenFormatter
{
14 constructor (config
) {
17 * The string to use as a template string. By default, any property
18 * inside double curly braces `{{likeThis}}` will be extracted from
19 * the object and replaced. If the object does not contain the
20 * property, it will leave it.
24 * @memberof Cologne.Formatter.Token
26 * @default '{{message}}'
28 this.formatString
= '{{message}}';
31 * The regex rule to use to match the tokens.
35 * @memberof Cologne.Formatter.Token
37 * @default /{{(.*)}}/g
39 this.replaceRule
= /{{(.*?)}}/g;
42 * Flag that specifies whether or not to use an isoDate when using
43 * `_timestamp`. If false it will output the raw timestamp.
47 * @memberof Cologne.Formatter.Token
53 this._ansiRe
= /_ansi:.+/;
55 Object
.assign(this, config
|| {});
59 * Main entry point, it will read the incoming log object and convert
60 * all the tokens to their corresponding representation, finally
61 * returning the string.
65 * @memberof Cologne.Formatter.Token
66 * @param {Cologne.tCologneLog} logObjet the log to format
67 * @return {String} the formatted object
70 let resultString
, escapeCode
;
72 escapeCode
= String
.fromCharCode(27);
74 resultString
= this.formatString
.replace(this.replaceRule
, function (match
, token
) {
77 if (token
=== '_timestamp' && this.isoDate
) {
78 date
= new Date(logObject
._timestamp
);
79 return date
.toISOString();
82 if (token
.match(this._ansiRe
)) {
83 ansiType
= token
.split(':')[1];
86 if (ansiType
=== '_level') {
87 return escapeCode
+ LogUtilities
.getAnsiCode(LogUtilities
.getLevelAnsi(logObject
._level
));
90 return escapeCode
+ LogUtilities
.getAnsiCode(ansiType
);
93 if (!logObject
.hasOwnProperty(token
)) {
97 return logObject
[token
];
104 module
.exports
= TokenFormatter
;