| 81 | * Formatter that outputs log events as human-readable text with color highlighting. |
| 82 | */ |
| 83 | export class TextFormatter implements ConsoleFormatter { |
| 84 | format(event: LogEvent): unknown[] { |
| 85 | const { fmt, params } = mergeFragments([ |
| 86 | TextFormatter.formatLevel(event.level), |
| 87 | TextFormatter.formatTimestamp(event.timestamp), |
| 88 | TextFormatter.formatMessage(event.message), |
| 89 | ...TextFormatter.formatAttributes(event.attributes), |
| 90 | ]) |
| 91 | return [fmt, ...params] |
| 92 | } |
| 93 | |
| 94 | private static maxLevelLength = Math.max(...validLogLevels.map((level) => level.length)) |
| 95 | |
| 96 | private static formatLevel(level: LogLevel): Fragment { |
| 97 | return { |
| 98 | fmt: '%c%s%c', |
| 99 | params: [levelStyles[level], level.toUpperCase().padEnd(TextFormatter.maxLevelLength), 'color: initial'], |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | private static formatTimestamp(timestamp: Temporal.Instant): Fragment { |
| 104 | return { |
| 105 | fmt: '%c%s%c', |
| 106 | params: ['color: gray', timestamp.toString(), 'color: initial'], |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | private static formatMessage(message: string): Fragment { |
| 111 | return { fmt: '%s', params: [message] } |
| 112 | } |
| 113 | |
| 114 | private static formatAttributes(attributes: Record<string, unknown>): Fragment[] { |
| 115 | return Object.entries(attributes).map(([key, value]) => ({ |
| 116 | fmt: '%c%s%c=%o', |
| 117 | params: ['color: cyan', key, 'color: initial', value], |
| 118 | })) |
| 119 | } |
| 120 | } |
nothing calls this directly
no outgoing calls
no test coverage detected