]>
git.r.bdr.sh - rbdr/cologne/blob - lib/formatters/token.js
05895fa400b77db0e8e53b56e39cba8f630f4065
3 const Utilities
= require('../utilities');
6 * Token formatter. Given a format string it will attempt to output
10 * @implements IFormatter
13 module
.exports
= class TokenFormatter
{
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 Formatters.Token
26 * @default '{{message}}'
28 this.formatString
= '{{message}}';
31 * The regex rule to use to match the tokens.
35 * @memberof Formatters.Token
37 * @default /{{(.{1,255}?)}}/g
39 this.replaceRule
= /{{(.{1,255}?)}}/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 Formatters.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 Formatters.Token
66 * @param {tCologneLog} log the log to format
67 * @return {String} the formatted object
71 const escapeCode
= String
.fromCharCode(27);
72 return this.formatString
.replace(this.replaceRule
, (match
, token
) => {
74 if (token
=== '_timestamp' && this.isoDate
) {
75 const date
= new Date(Number(log
._timestamp
) / 1000000);
76 return date
.toISOString();
79 if (token
.match(this._ansiRe
)) {
80 const ansiType
= token
.split(':')[1];
83 if (ansiType
=== '_level') {
84 return escapeCode
+ Utilities
.getAnsiCode(Utilities
.getLevelAnsi(log
._level
));
87 return escapeCode
+ Utilities
.getAnsiCode(ansiType
);
90 return log
[token
] || match
;