Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 | 1x 1x 1x 1x 1x 1x 188x 188x 188x 188x 188x 188x 1x 69x 69x 69x 69x 69x 69x 1x 1x 1x 36x 36x 36x 36x 1x 1x 36x 1x 1x 44x 114x 114x 100x 100x 14x 14x 14x 134x 134x 14x 14x 114x 114x | import { terminalFormatter } from '../../utils/common/terminal-formatter.ts'; import { stringify } from '../../utils/converter/stringify.ts'; import type { LogLevel } from '../definition.ts'; import { decorateLogText } from '../implementation/level-utils.ts'; import { asLogEntry } from '../log-entry.ts'; /** * Function type for formatting log entries into strings. * * Log formatters convert the structured log data (level, message, arguments) into a final string representation that * will be written to the output destination. */ export type LogFormatter = (level: LogLevel, message: string, args?: readonly unknown[]) => string; /** * Basic formatter that outputs logs as "level - message". * * This is the simplest formatter available, suitable for development or situations where minimal formatting is desired. * * @example Output format * * ``` * info - Application started * error - Connection failed * ``` */ export const basicFormatter: LogFormatter = (level, message) => (message ? `[${level}] ${message}` : `[${level}]`); /** * Plain text formatter with timestamp and padded level labels. * * Produces clean, readable output with consistent column alignment. The level is padded to 9 characters for consistent * formatting across all levels. * * @example Output format * * ``` * "2024-01-15T10:30:45.123Z" [info ] Application started * "2024-01-15T10:30:46.456Z" [error ] Connection failed * ``` */ export const plainFormatter: LogFormatter = (level, message) => { const timestamp = stringify(new Date()); const paddedLevel = level.padEnd(9, ' '); const levelText = `[${paddedLevel}]`; const line = message ? `${timestamp} ${levelText} ${message}` : `${timestamp} ${levelText}`; return line; }; /** * Colorful formatter with ANSI color codes and timestamps. * * Similar to plainFormatter but with color coding for different log levels and dimmed timestamps for better visual * hierarchy. Best for terminal output. * * @example Output format * * ``` * \u001b[2m"2024-01-15T10:30:45.123Z"\u001b[22m \u001b[34m[info ]\u001b[39m Application started * \u001b[2m"2024-01-15T10:30:46.456Z"\u001b[22m \u001b[31m[error ]\u001b[39m Connection failed * ``` */ export const colorfulFormatter: LogFormatter = (level, message) => { const timestamp = terminalFormatter.dim(stringify(new Date())); const paddedLevel = level.padEnd(9, ' '); const levelText = decorateLogText(level, `[${paddedLevel}]`); const line = message ? `${timestamp} ${levelText} ${message}` : `${timestamp} ${levelText}`; return line; }; /** * JSON formatter that outputs compact, single-line JSON objects. * * Produces structured JSON-like output suitable for log aggregation systems, automated parsing, or storage in * JSON-based logging systems. * * Important: as shown in the example, each emitted entry is itself a valid JSON object - however the text with all * entries is not itself a valid JSON, lacking colons and a root delimiter. * * @example Output format * * ```json * {"level":"info","timestamp":1705312245123,"message":"Application started"} * {"level":"error","timestamp":1705312246456,"message":"Connection failed","args":[{"host":"db.example.com"}]} * ``` */ export const jsonCompactFormatter: LogFormatter = (level, message, args) => jsonFormatter(level, message, args, false); /** * JSON formatter that outputs pretty-printed, multi-line JSON objects. * * Produces readable JSON output with proper indentation. Useful for debugging or when human readability is more * important than compact size. * * Important: as shown in the example, each emitted entry is itself a valid JSON object - however the text with all * entries is not itself a valid JSON, lacking colons and a root delimiter. * * @example Output format * * ```json * { "level": "info", "timestamp": 1705312245123, "message": "Application started" } * { "level": "error", "timestamp": 1705312246456, "message": "Connection failed", "args": [ { "host": "db.example.com" } ] } * ``` */ export const jsonPrettyFormatter: LogFormatter = (level, message, args) => jsonFormatter(level, message, args, true); const jsonFormatter = (level: LogLevel, message: string, args: readonly unknown[] | undefined, pretty: boolean) => { const entry = asLogEntry(level, message, args); try { return pretty ? JSON.stringify(entry, undefined, 2) : JSON.stringify(entry); } catch { return stringify(entry, { pretty, excludeArrayTruncationElement: true, excludeObjectTruncationProperty: true }); } }; /** * Creates a formatter that appends formatted arguments to the base formatter output. * * This higher-order formatter wraps another formatter and adds detailed formatting of the arguments array. Each * argument is formatted on a separate line with an index. Useful for debugging when you need to see the full content of * logged objects. * * @example Usage * * ```ts * import { emitter } from 'emitnlog/logger'; * * const formatter = emitter.plainArgAppendingFormatter(emitter.plainFormatter); * // Now arguments will be formatted and appended to each log entry * ``` * * @example Output with arguments * * ``` * "2024-01-15T10:30:45.123Z" [info ] User logged in * [arg00] { userId: "123", email: "user@example.com" } * [arg01] { timestamp: "2024-01-15T10:30:45.123Z" } * ``` * * @param baseFormatter The base formatter to wrap * @param delimiter The string used to separate the base output from arguments (default: '\n') * @returns A new formatter that includes formatted arguments */ export const plainArgAppendingFormatter = (baseFormatter: LogFormatter, delimiter = '\n'): LogFormatter => (level, message, args) => { const formatted = baseFormatter(level, message, args); if (!args?.length) { return formatted; } const indexPadding = String(args.length).length; const formattedArgs = args .map((arg, i) => { const formattedArg = stringify(arg, { includeStack: true, pretty: true, maxDepth: 3 }); return `[arg${String(i).padStart(indexPadding, '0')}] ${formattedArg}`; }) .join('\n'); return formatted ? `${formatted}${delimiter}${formattedArgs}` : formattedArgs; }; |