-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathlog-formatter.ts
More file actions
138 lines (119 loc) · 4.01 KB
/
log-formatter.ts
File metadata and controls
138 lines (119 loc) · 4.01 KB
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
/**
* Log formatter for different output formats (raw, pretty, json)
*/
import chalk from 'chalk';
import { ParsedLogEntry, OutputFormat, EnhancedLogEntry } from '../types';
/**
* Options for log formatting
*/
interface LogFormatterOptions {
/** Output format */
format: OutputFormat;
/** Whether to colorize output (for pretty format). Defaults to true if stdout is TTY */
colorize?: boolean;
}
/**
* Formats parsed log entries into different output formats
*/
export class LogFormatter {
private format: OutputFormat;
private colorize: boolean;
constructor(options: LogFormatterOptions) {
this.format = options.format;
this.colorize = options.colorize ?? process.stdout.isTTY ?? false;
}
/**
* Formats a parsed log entry (supports both ParsedLogEntry and EnhancedLogEntry)
*
* @param entry - Parsed log entry (may include PID info)
* @returns Formatted string with newline
*/
formatEntry(entry: ParsedLogEntry | EnhancedLogEntry): string {
switch (this.format) {
case 'raw':
throw new Error('Cannot format parsed entry as raw - use formatRaw for raw lines');
case 'pretty':
return this.formatPretty(entry);
case 'json':
return this.formatJson(entry);
}
}
/**
* Formats a raw log line (pass-through)
*
* @param line - Raw log line
* @returns Line with newline appended
*/
formatRaw(line: string): string {
return line.endsWith('\n') ? line : line + '\n';
}
/**
* Formats an entry as pretty, human-readable output
*/
private formatPretty(entry: ParsedLogEntry | EnhancedLogEntry): string {
// Format timestamp as readable date
const date = new Date(entry.timestamp * 1000);
const timeStr = date.toISOString().replace('T', ' ').substring(0, 23);
// Format target (domain:port or just domain for standard ports)
const port = this.getDisplayPort(entry);
const target = port ? `${entry.domain}:${port}` : entry.domain;
// Status text
const statusText = entry.isAllowed ? 'ALLOWED' : 'DENIED';
// User agent (show if not empty/dash)
const userAgentPart =
entry.userAgent && entry.userAgent !== '-' ? ` [${entry.userAgent}]` : '';
// PID info (show if available)
const enhancedEntry = entry as EnhancedLogEntry;
const pidPart = enhancedEntry.pid !== undefined && enhancedEntry.pid !== -1
? ` <PID:${enhancedEntry.pid} ${enhancedEntry.comm || 'unknown'}>`
: '';
// Build message
const message = `[${timeStr}] ${entry.method} ${target} → ${entry.statusCode} (${statusText})${userAgentPart}${pidPart}`;
// Colorize based on allowed/denied
if (!this.colorize) {
return message + '\n';
}
return entry.isAllowed ? chalk.green(message) + '\n' : chalk.red(message) + '\n';
}
/**
* Formats an entry as JSON (newline-delimited)
*/
private formatJson(entry: ParsedLogEntry | EnhancedLogEntry): string {
return JSON.stringify(entry) + '\n';
}
/**
* Formats a batch of entries (primarily for JSON array output)
*/
formatBatch(entries: (ParsedLogEntry | EnhancedLogEntry)[]): string {
if (this.format === 'json') {
return entries.map(e => this.formatJson(e)).join('');
}
return entries.map(e => this.formatEntry(e)).join('');
}
/**
* Gets the port for display, returning undefined for standard ports
*/
private getDisplayPort(entry: ParsedLogEntry): string | undefined {
// Extract port from URL for CONNECT
if (entry.method === 'CONNECT') {
const colonIndex = entry.url.lastIndexOf(':');
if (colonIndex !== -1) {
const port = entry.url.substring(colonIndex + 1);
// Hide standard HTTPS port
if (port === '443') {
return undefined;
}
return port;
}
}
// For other methods, check destPort
if (entry.destPort && entry.destPort !== '-') {
// Hide standard ports
if (entry.destPort === '443' || entry.destPort === '80') {
return undefined;
}
return entry.destPort;
}
return undefined;
}
}