(entry: LogEntry, options: FormatOptions = {})
| 159 | } |
| 160 | |
| 161 | export function formatEntry(entry: LogEntry, options: FormatOptions = {}): FormattedEntry { |
| 162 | const ctx = entry.ctx ? redactCtx(entry.ctx) : undefined; |
| 163 | const omitContextKeys = new Set(options.omitContextKeys ?? []); |
| 164 | const msg = truncate(entry.msg, MSG_MAX_CHARS); |
| 165 | const pairs: string[] = []; |
| 166 | if (ctx) { |
| 167 | for (const [k, v] of Object.entries(ctx)) { |
| 168 | if (omitContextKeys.has(k)) continue; |
| 169 | if (v !== undefined) pairs.push(formatPair(k, v)); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | const time = new Date(entry.t).toISOString(); |
| 174 | const label = LEVEL_LABEL[entry.level]; |
| 175 | const rendered = pairs.length === 0 |
| 176 | ? `${time} ${label} ${msg}` |
| 177 | : `${time} ${label} ${msg} ${pairs.join(' ')}`; |
| 178 | |
| 179 | let head = Buffer.byteLength(rendered, 'utf-8') > ENTRY_MAX_BYTES |
| 180 | ? clipBytes(rendered, ENTRY_MAX_BYTES) |
| 181 | : rendered; |
| 182 | |
| 183 | if (options.ansi === true) { |
| 184 | head = `${ANSI_LEVEL[entry.level]}${head}${ANSI_RESET}`; |
| 185 | } |
| 186 | |
| 187 | if (entry.error?.stack) { |
| 188 | head = `${head}\n${indentStack(clipStack(redactString(entry.error.stack)))}`; |
| 189 | } else if (entry.error?.message) { |
| 190 | head = `${head}\n Error: ${redactString(entry.error.message)}`; |
| 191 | } |
| 192 | |
| 193 | return { text: head, dropped: false }; |
| 194 | } |
| 195 | |
| 196 | export function extractError(value: Error): { message: string; stack?: string } { |
| 197 | return typeof value.stack === 'string' |
no test coverage detected