3 const Tap
= require('tap');
5 const TokenFormatter
= require('../../lib/formatters/token');
12 _timestamp: BigInt(Date
.now()) * 1000000n
,
14 _from: 'Dummy Logger',
16 _levelString: 'error',
17 message: 'testing stuff!'
19 const isoDate
= (new Date(Number(logObject
._timestamp
) / 1000000)).toISOString();
21 const defaultFormatter
= new TokenFormatter();
22 const customFormatter
= new TokenFormatter({
23 formatString: '{{_level}} {{_cologneLog}} {{_timestamp}} {{_magic}}'
25 const ansiFormatter
= new TokenFormatter({
26 formatString: 'string {{_ansi:red}}with color:{{_ansi:reset}} {{message}}'
28 const ansiLevelFormatter
= new TokenFormatter({
29 formatString: 'string {{_ansi:_level}}with color:{{_ansi:reset}} {{message}}'
31 const plainDateFormatter
= new TokenFormatter({
33 formatString: '{{_timestamp}}'
35 const customSearchFormatter
= new TokenFormatter({
36 formatString: '[[message]]',
37 replaceRule: /\[\[(.*?)\]\]/g
41 * TEST: #format() - default
44 const defaultFormattedString
= defaultFormatter
.format(logObject
);
46 Tap
.equal(typeof defaultFormattedString
, 'string',
47 '#format() should output a string in default mode');
49 Tap
.equal(defaultFormattedString
, logObject
.message
,
50 '#format() should include the message in default mode');
53 * TEST: #format() - custom
56 const customFormattedString
= customFormatter
.format(logObject
);
58 Tap
.equal(typeof customFormattedString
, 'string',
59 '#format() should output a string in custom mode');
61 Tap
.ok(customFormattedString
.match(logObject
._level
),
62 '#format() with custom string should include the specified tokens (check 1)');
64 Tap
.ok(customFormattedString
.match(logObject
._cologneLog
),
65 '#format() with custom string should include the specified tokens (check 2)');
67 Tap
.ok(customFormattedString
.match(isoDate
),
68 '#format() with iso date should include the timestamp as an iso date');
70 Tap
.ok(customFormattedString
.match('{{_magic}}'),
71 '#format() should not replace tokens that don\'t match');
74 * TEST: #format() - ansi
77 const ansiFormattedString
= ansiFormatter
.format(logObject
);
79 Tap
.equal(typeof ansiFormattedString
, 'string',
80 '#format() should output a string in ansi mode');
82 Tap
.equal(ansiFormattedString
.split(String
.fromCharCode(27) + '[31m').length
, 2,
83 '#format() with ansi tokens should colorize the string');
85 Tap
.equal(ansiFormattedString
.split(String
.fromCharCode(27) + '[0m').length
, 2,
86 '#format() with ansi reset should reset the string');
89 * TEST: #format() - ansi based on level
92 const ansiLevelFormattedString
= ansiLevelFormatter
.format(logObject
);
94 Tap
.equal(ansiLevelFormattedString
.split(String
.fromCharCode(27) + '[34m').length
, 2,
95 '#format() with ansi tokens should colorize the string based on level');
100 * TEST: #format() - plain date
103 const plainDateFormattedString
= plainDateFormatter
.format(logObject
);
105 Tap
.equal(typeof plainDateFormattedString
, 'string',
106 '#format() should output a string in plain date mode');
108 Tap
.equal(plainDateFormattedString
, logObject
._timestamp
.toString(),
109 '#format() with plain date should include the timestamp as-is');
112 * TEST: #format() - custom search
115 const customSearchFormattedString
= customSearchFormatter
.format(logObject
);
117 Tap
.equal(typeof customSearchFormattedString
, 'string',
118 '#format() should output a string in custom search mode');
120 Tap
.equal(customSearchFormattedString
, logObject
.message
,
121 '#format() with a custom search, should properly match the new tokens');