-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpretty_printer.v
More file actions
248 lines (215 loc) · 6.35 KB
/
pretty_printer.v
File metadata and controls
248 lines (215 loc) · 6.35 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
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
// Pretty printer for tree-formatted logging
module logger
import x.json2
import time
// Tree drawing characters
const tee = '├─'
const corner = '└─'
const pipe = '│'
const space = ' '
// pretty_print_event renders a wide event as a tree structure
pub fn pretty_print_event(event WideEvent, config LoggerConfig) {
mode := config.mode
field_configs := config.field_configs.clone()
// Build and print header line
header := build_header(event, field_configs)
println(header)
// Separate fields by priority
mut header_fields := []string{}
mut important_fields := []string{}
mut normal_fields := []string{}
mut boring_fields := []string{}
for field, _ in event.context {
cfg := get_field_config(field, field_configs)
match cfg.priority {
.header { header_fields << field }
.important { important_fields << field }
.normal { normal_fields << field }
.boring { boring_fields << field }
.internal {} // Skip
}
}
// Determine which fields to show based on mode
mut fields_to_show := []string{}
fields_to_show << important_fields
if mode == .normal || mode == .verbose {
fields_to_show << normal_fields
}
if mode == .verbose {
fields_to_show << boring_fields
}
// Calculate hidden count for compact mode
hidden_count := normal_fields.len + boring_fields.len
// Print tree
for i, field in fields_to_show {
value := event.context[field] or { json2.Any('') }
is_last := i == fields_to_show.len - 1
print_tree_node(field, value, 0, is_last, field_configs, mode)
}
// Show hidden count in compact mode
if mode == .compact && hidden_count > 0 {
println(' ${dim}└─ [${hidden_count} fields hidden, use --verbose]${reset}')
}
// Add spacing after event for readability
println('')
}
// build_header constructs the header line for a wide event
fn build_header(event WideEvent, field_configs map[string]FieldConfig) string {
mut parts := []string{}
// Timestamp
parts << '${dim}${event.timestamp.format_ss()}${reset}'
// Level with color
level_color := get_level_color(event.level)
parts << '${level_color}${event.level.str().to_upper()}${reset}'
// Service
parts << '${cyan}[${event.service}]${reset}'
// Configured header fields from context (in order of appearance in context)
for field, value in event.context {
cfg := get_field_config(field, field_configs)
if cfg.priority != .header {
continue
}
formatted := format_value(value, cfg)
color_code := if cfg.color != '' { cfg.color } else { reset }
parts << '${color_code}${formatted}${reset}'
}
return parts.join(' ')
}
// print_tree_node recursively prints a tree node
fn print_tree_node(field string, value json2.Any, depth int, is_last bool, configs map[string]FieldConfig, mode DisplayMode) {
indent := space.repeat(depth)
prefix := if is_last { corner } else { tee }
cfg := get_field_config(field, configs)
color_code := if cfg.color != '' { cfg.color } else { cyan }
// Format the value
formatted := format_value(value, cfg)
// Print the node
println('${indent}${dim}${prefix}${reset} ${color_code}${field}:${reset} ${formatted}')
// For objects, recurse if in verbose mode or if important
if value is map[string]json2.Any {
m := value as map[string]json2.Any
should_expand := mode == .verbose || cfg.priority == .important
if should_expand && m.len > 0 {
child_keys := m.keys()
for i, child_key in child_keys {
child_value := m[child_key] or { json2.Any('') }
child_is_last := i == child_keys.len - 1
print_tree_node(child_key, child_value, depth + 1, child_is_last, configs,
mode)
}
}
}
// For arrays, show items or summary
if value is []json2.Any {
arr := value as []json2.Any
max_items := if mode == .verbose { 10 } else { 3 }
if arr.len == 0 {
// Already shown as "[]" by format_value
} else if arr.len > max_items {
// Show first max_items
for i in 0 .. max_items {
child_indent := space.repeat(depth + 1)
child_prefix := if i == max_items - 1 { corner } else { tee }
item_value := arr[i]
item_formatted := format_simple_value(item_value)
println('${child_indent}${dim}${child_prefix}${reset} ${dim}[${i}]${reset} ${item_formatted}')
}
// Show "more items" indicator
println('${space.repeat(depth + 1)}${dim}└─ ... and ${arr.len - max_items} more${reset}')
} else {
// Show all items
for i, item_value in arr {
child_indent := space.repeat(depth + 1)
child_prefix := if i == arr.len - 1 { corner } else { tee }
item_formatted := format_simple_value(item_value)
println('${child_indent}${dim}${child_prefix}${reset} ${dim}[${i}]${reset} ${item_formatted}')
}
}
}
}
// format_value formats a value based on its field configuration
fn format_value(value json2.Any, cfg FieldConfig) string {
// Use custom formatter if provided
if formatter := cfg.formatter {
return formatter(value)
}
// Default formatting
return format_simple_value(value)
}
// format_simple_value provides default formatting for any json2.Any value
fn format_simple_value(value json2.Any) string {
return match value {
string {
value
}
bool {
value.str()
}
int {
value.str()
}
i8 {
value.str()
}
i16 {
value.str()
}
i64 {
value.str()
}
u8 {
value.str()
}
u16 {
value.str()
}
u32 {
value.str()
}
u64 {
value.str()
}
f32 {
'${value:.4f}'
}
f64 {
'${value:.4f}'
}
map[string]json2.Any {
// Summarize object
keys := value.keys()
return '{${keys.len} fields}'
}
[]json2.Any {
// Summarize array
return '[${value.len} items]'
}
time.Time {
value.format_rfc3339()
}
else {
value.str()
}
}
}
// format_value_with_config is a public helper for formatting with field config
pub fn format_value_with_config(value json2.Any, field_name string, configs map[string]FieldConfig) string {
cfg := get_field_config(field_name, configs)
return format_value(value, cfg)
}
// print_event_as_json outputs an event as JSON (for production)
pub fn print_event_as_json(event WideEvent) {
// Build a simple JSON representation
mut parts := []string{}
parts << '{'
parts << ' "timestamp": "${event.timestamp.format_rfc3339()}"'
parts << ' "level": "${event.level.str()}"'
parts << ' "service": "${event.service}"'
parts << ' "environment": "${event.environment}"'
// Add context fields
for key, value in event.context {
parts << ' "${key}": "${value.str()}"'
}
parts << '}'
println(parts.join(',\n'))
}