|
| 1 | +import { beforeEach, describe, expect, it } from 'vitest'; |
| 2 | +import type { LogFormatter, LogMessage } from '../../types.js'; |
| 3 | +import { LogLevel } from '../../types.js'; |
| 4 | +import { jsonLogFormatter } from '../json-log-formatter.js'; |
| 5 | + |
| 6 | +describe('json-log-formatter', () => { |
| 7 | + const logDate = new Date('2022-01-02T00:00:00Z'); |
| 8 | + |
| 9 | + let formatter: LogFormatter; |
| 10 | + |
| 11 | + beforeEach(() => { |
| 12 | + formatter = jsonLogFormatter({ colors: false }); |
| 13 | + }); |
| 14 | + |
| 15 | + it('formats log message', () => { |
| 16 | + const message: LogMessage = { |
| 17 | + level: LogLevel.INFO, |
| 18 | + scope: 'test', |
| 19 | + message: 'hello world', |
| 20 | + timestamp: logDate, |
| 21 | + }; |
| 22 | + |
| 23 | + const result = formatter([message]); |
| 24 | + |
| 25 | + expect(result).toEqual( |
| 26 | + `{ level: 'info', scope: 'test', message: 'hello world', timestamp: ${logDate.toISOString()} }\n` |
| 27 | + ); |
| 28 | + }); |
| 29 | + |
| 30 | + it('formats log message with custom properties', () => { |
| 31 | + const message: LogMessage = { |
| 32 | + level: LogLevel.INFO, |
| 33 | + scope: 'test', |
| 34 | + message: 'hello world', |
| 35 | + timestamp: logDate, |
| 36 | + // Custom properties |
| 37 | + foo: 'bar', |
| 38 | + }; |
| 39 | + |
| 40 | + const result = formatter([message]); |
| 41 | + |
| 42 | + expect(result).toEqual( |
| 43 | + `{ level: 'info', scope: 'test', message: 'hello world', timestamp: ${logDate.toISOString()}, foo: 'bar' }\n` |
| 44 | + ); |
| 45 | + }); |
| 46 | + |
| 47 | + it('formats log message with complex properties', () => { |
| 48 | + const message: LogMessage = { |
| 49 | + level: LogLevel.INFO, |
| 50 | + scope: 'test', |
| 51 | + message: 'hello world', |
| 52 | + timestamp: logDate, |
| 53 | + // Custom properties |
| 54 | + boolean: false, |
| 55 | + number: 42, |
| 56 | + array: [1, 2, 3], |
| 57 | + date: logDate, |
| 58 | + set: new Set(['A', 'B', 'C']), |
| 59 | + map: new Map<string, any>([ |
| 60 | + ['A', 1], |
| 61 | + ['B', logDate], |
| 62 | + ['C', { foo: 'bar' }], |
| 63 | + ]), |
| 64 | + }; |
| 65 | + |
| 66 | + const result = formatter([message]); |
| 67 | + |
| 68 | + expect(result).toEqual( |
| 69 | + `{ level: 'info', scope: 'test', message: 'hello world', timestamp: ${logDate.toISOString()}, boolean: false, number: 42, array: [ 1, 2, 3 ], date: ${logDate.toISOString()}, set: [ 'A', 'B', 'C' ], map: { A: 1, B: '${logDate.toISOString()}', C: { foo: 'bar' } } }\n` |
| 70 | + ); |
| 71 | + }); |
| 72 | + |
| 73 | + it('formats log message with sensitive properties', () => { |
| 74 | + const message: LogMessage = { |
| 75 | + level: LogLevel.INFO, |
| 76 | + scope: 'test', |
| 77 | + message: 'hello world', |
| 78 | + timestamp: logDate, |
| 79 | + // Custom properties |
| 80 | + password: 'secret', |
| 81 | + accessToken: 'secret', |
| 82 | + apiKey: 'secret', |
| 83 | + }; |
| 84 | + |
| 85 | + const result = formatter([message]); |
| 86 | + |
| 87 | + expect(result).toEqual( |
| 88 | + `{ level: 'info', scope: 'test', message: 'hello world', timestamp: ${logDate.toISOString()}, password: '***REDACTED***', accessToken: '***REDACTED***', apiKey: '***REDACTED***' }\n` |
| 89 | + ); |
| 90 | + }); |
| 91 | + |
| 92 | + it('formats log messages with colors', () => { |
| 93 | + formatter = jsonLogFormatter({ colors: true }); |
| 94 | + |
| 95 | + const messages: Array<LogMessage> = [ |
| 96 | + { |
| 97 | + level: LogLevel.ERROR, |
| 98 | + scope: 'test', |
| 99 | + message: 'hello world', |
| 100 | + timestamp: logDate, |
| 101 | + }, |
| 102 | + { |
| 103 | + level: LogLevel.WARN, |
| 104 | + scope: 'test', |
| 105 | + message: 'hello world', |
| 106 | + timestamp: logDate, |
| 107 | + }, |
| 108 | + { |
| 109 | + level: LogLevel.INFO, |
| 110 | + scope: 'test', |
| 111 | + message: 'hello world', |
| 112 | + timestamp: logDate, |
| 113 | + }, |
| 114 | + { |
| 115 | + level: LogLevel.DEBUG, |
| 116 | + scope: 'test', |
| 117 | + message: 'hello world', |
| 118 | + timestamp: logDate, |
| 119 | + }, |
| 120 | + { |
| 121 | + level: LogLevel.TRACE, |
| 122 | + scope: 'test', |
| 123 | + message: 'hello world', |
| 124 | + timestamp: logDate, |
| 125 | + }, |
| 126 | + ]; |
| 127 | + |
| 128 | + const result = formatter(messages); |
| 129 | + const lines = result.trim().split('\n'); |
| 130 | + |
| 131 | + expect(lines).toHaveLength(messages.length); |
| 132 | + |
| 133 | + // Note, the json formatter colorizes the values based on data type. |
| 134 | + // The log levels are strings and so all have the same color. |
| 135 | + // This behavior differs from the pretty formatter. |
| 136 | + |
| 137 | + expect(lines[0]).toEqual( |
| 138 | + `{ level: \u001b[32m'error'\u001b[39m, scope: \u001b[32m'test'\u001b[39m, message: \u001b[32m'hello world'\u001b[39m, timestamp: \u001b[35m2022-01-02T00:00:00.000Z\u001b[39m }` |
| 139 | + ); |
| 140 | + |
| 141 | + expect(lines[1]).toEqual( |
| 142 | + `{ level: \u001b[32m'warn'\u001b[39m, scope: \u001b[32m'test'\u001b[39m, message: \u001b[32m'hello world'\u001b[39m, timestamp: \u001b[35m2022-01-02T00:00:00.000Z\u001b[39m }` |
| 143 | + ); |
| 144 | + |
| 145 | + expect(lines[2]).toEqual( |
| 146 | + `{ level: \u001b[32m'info'\u001b[39m, scope: \u001b[32m'test'\u001b[39m, message: \u001b[32m'hello world'\u001b[39m, timestamp: \u001b[35m2022-01-02T00:00:00.000Z\u001b[39m }` |
| 147 | + ); |
| 148 | + |
| 149 | + expect(lines[3]).toEqual( |
| 150 | + `{ level: \u001b[32m'debug'\u001b[39m, scope: \u001b[32m'test'\u001b[39m, message: \u001b[32m'hello world'\u001b[39m, timestamp: \u001b[35m2022-01-02T00:00:00.000Z\u001b[39m }` |
| 151 | + ); |
| 152 | + |
| 153 | + expect(lines[4]).toEqual( |
| 154 | + `{ level: \u001b[32m'trace'\u001b[39m, scope: \u001b[32m'test'\u001b[39m, message: \u001b[32m'hello world'\u001b[39m, timestamp: \u001b[35m2022-01-02T00:00:00.000Z\u001b[39m }` |
| 155 | + ); |
| 156 | + }); |
| 157 | +}); |
0 commit comments