kelora 0.11.2

A command-line log analysis tool with embedded Rhai scripting
Documentation
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
# Error Handling

Understanding Kelora's error handling modes and how to diagnose issues.

## Processing Modes

Kelora offers two error handling modes:

| Mode | Behavior | Use Case |
|------|----------|----------|
| **Resilient (default)** | Skip errors, continue processing | Production log analysis, exploratory work |
| **Strict (`--strict`)** | Fail-fast on errors | Data validation, CI/CD pipelines |

## Resilient Mode

### Overview

In resilient mode (default), Kelora continues processing even when errors occur:

- **Parse errors**: Skip unparseable lines, continue with next line
- **Filter errors**: Treat as `false`, skip event
- **Transform errors**: Return original event unchanged (atomic rollback)
- **Summary**: Show error count at end

### When to Use

- Analyzing messy production logs
- Exploratory data analysis
- Real-time log streaming
- Mixed format log files

### Example Behavior

```bash
kelora -j app.log --exec 'e.result = e.value.to_int() * 2'
```

**Input:**
```json
{"value": "123"}
{"value": "invalid"}
{"value": "456"}
```

**Output:**
```
result=246
(skipped - error converting "invalid")
result=912
```

**Summary:**
```
🔹 Processed 3 lines, 2 events output, 1 error
```

### Error Recording

Errors are recorded but don't stop processing:

```bash
kelora -j app.log --filter 'e.timestamp.to_unix() > 1000000'
```

If `e.timestamp` is missing or invalid:

- Filter evaluates to `false`
- Event is skipped
- Error is recorded
- Processing continues

## Strict Mode

### Overview

In strict mode (`--strict`), Kelora fails immediately on the first error:

- **Parse errors**: Show error, abort immediately
- **Filter errors**: Show error, abort immediately
- **Transform errors**: Show error, abort immediately
- **Exit code**: Non-zero on any error

### When to Use

- Data validation pipelines
- CI/CD quality gates
- Critical processing where partial results aren't acceptable
- Debugging log parsing issues

### Example Behavior

```bash
kelora -j --strict app.log --exec 'e.result = e.value.to_int() * 2'
```

**Input:**
```json
{"value": "123"}
{"value": "invalid"}
{"value": "456"}
```

**Output:**
```
result=246
⚠️  kelora: line 2: exec error - cannot convert 'invalid' to integer
```

**Exit code:** `1`

Processing stops at the first error. Line 3 is never processed.

### Enabling Strict Mode

```bash
kelora -j --strict app.log
```

## Error Types

### Parse Errors

Occur when input lines can't be parsed in the specified format.

**JSON parse error:**
```bash
kelora -j app.log
```

**Input:**
```
{"valid": "json"}
{invalid json}
{"more": "valid"}
```

**Resilient behavior:**

- Line 1: Parsed successfully
- Line 2: Skipped (parse error recorded)
- Line 3: Parsed successfully

**Strict behavior:**

- Line 1: Parsed successfully
- Line 2: Error shown, processing aborts
- Line 3: Never processed

### Filter Errors

Occur when `--filter` expressions fail during evaluation.

**Example:**
```bash
kelora -j --filter 'e.timestamp.to_unix() > 1000000' app.log
```

If `e.timestamp` is missing:

**Resilient behavior:**

- Filter evaluates to `false`
- Event is skipped
- Error recorded

**Strict behavior:**

- Error shown immediately
- Processing aborts

### Transform Errors

Occur when `--exec` scripts fail during execution.

**Example:**
```bash
kelora -j --exec 'e.result = e.value.to_int()' app.log
```

If `e.value` is not a valid integer:

**Resilient behavior:**

- Transformation rolled back (atomic)
- Original event returned unchanged
- Error recorded

**Strict behavior:**

- Error shown immediately
- Processing aborts

## Verbose Error Reporting

### Default Error Reporting

By default, errors are collected and summarized at the end:

```bash
kelora -j app.log --exec 'e.result = e.value.to_int()'
```

**Summary:**
```
🔹 Processed 100 lines, 95 events output, 5 errors
```

### Verbose Mode (`--verbose`)

Show each error immediately as it occurs:

```bash
kelora -j --verbose app.log --exec 'e.result = e.value.to_int()'
```

**Output:**
```
⚠️  kelora: line 5: exec error - cannot convert 'abc' to integer
result=123
⚠️  kelora: line 12: exec error - cannot convert 'def' to integer
result=456
⚠️  kelora: line 23: exec error - field 'value' not found
result=789
```

**Summary:**
```
🔹 Processed 100 lines, 95 events output, 5 errors
```

### Multiple Verbosity Levels

```bash
-v      # Show errors immediately
-vv     # Show errors with more context
-vvv    # Show errors with full details
```

### Verbose with Strict

Combine for immediate errors and fail-fast:

```bash
kelora -j --strict --verbose app.log
```

Errors are shown immediately, then processing aborts.

## Quiet/Silent Controls

Use the new orthogonal toggles to control output for automation:

| Flag | Effect |
|------|--------|
| `-q` / `--no-events` | Suppress events (formatter output) |
| `--no-diagnostics` | Suppress diagnostics/summaries (fatal line still emitted) |
| `--silent` | Suppress all terminal output (events/diagnostics/stats/terminal metrics/script output); emit one fatal line on errors; metrics files still write |
| `--no-script-output` | Suppress Rhai `print`/`eprint` (implied by `--silent`) |
| `-s` / `--stats=FORMAT` | Show stats only (suppress events, script output; diagnostics stay on). Format: table, json |
| `-m` / `--metrics=FORMAT` | Show metrics only (suppress events, diagnostics except fatal line, stats, script output). Format: table, full, json |
| `--with-stats` | Show stats alongside events (rare case) |
| `--with-metrics` | Show metrics alongside events (rare case) |

Examples:

```bash
kelora -q -j app.log --with-stats                    # No events; stats emit
kelora --silent -j app.log && echo "Clean" || echo "Has errors"
kelora -m --metrics-file metrics.json app.log        # Metrics only
kelora --metrics=json app.log                        # Metrics in JSON format
```

## Exit Codes

Kelora uses standard Unix exit codes:

| Code | Meaning |
|------|---------|
| `0` | Success (no errors) |
| `1` | Processing errors (parse/filter/exec errors) |
| `2` | Invalid usage (CLI errors, file not found) |
| `130` | Interrupted (Ctrl+C) |
| `141` | Broken pipe (normal in Unix pipelines) |

### Using Exit Codes

**In shell scripts:**
```bash
if kelora -q -j app.log; then
    echo "✓ No errors found"
else
    echo "✗ Errors detected"
    exit 1
fi
```

**In CI/CD:**
```bash
kelora --silent --strict app.log || exit 1
```

**With automation:**
```bash
kelora --silent app.log; echo "Exit code: $?"
```

## Atomic Transformations

### How It Works

In resilient mode, `--exec` scripts execute **atomically**:

```bash
kelora -j --exec 'e.a = 1; e.b = e.value.to_int(); e.c = 3' app.log
```

If `e.value.to_int()` fails:

- Changes to `e.a` are **rolled back**
- `e.b` is never set
- `e.c` is never set
- **Original event** is returned unchanged

### Why Atomic?

Prevents partial transformations from corrupting data:

**Without atomicity:**
```json
// Input
{"value": "invalid"}

// Broken output (partial transformation)
{"value": "invalid", "a": 1}  // Missing b and c!
```

**With atomicity:**
```json
// Input
{"value": "invalid"}

// Output (unchanged)
{"value": "invalid"}  // Clean original event
```

### Multiple --exec Scripts

Each `--exec` script is atomic independently:

```bash
kelora -j \
    --exec 'e.a = e.x.to_int()' \
    --exec 'e.b = e.y.to_int()' \
    app.log
```

If first `--exec` fails:

- First transformation rolled back
- Second `--exec` **still runs** on original event

If second `--exec` fails:

- First transformation **preserved** (it succeeded)
- Second transformation rolled back

## Common Error Scenarios

### Missing Fields

**Problem:**
```bash
kelora -j --filter 'e.timestamp > "2024-01-01"' app.log
```

Some events missing `timestamp` field.

**Solution:** Use safe access:
```bash
kelora -j --filter 'e.has_path("timestamp") && e.timestamp > "2024-01-01"' app.log
```

### Type Mismatches

**Problem:**
```bash
kelora -j --exec 'e.result = e.value * 2' app.log
```

`e.value` is a string, not a number.

**Solution:** Use type conversion with defaults:
```bash
kelora -j --exec 'e.result = to_int_or(e.value, 0) * 2' app.log
```

### Invalid Timestamps

**Problem:**
```bash
kelora -j --filter 'e.timestamp.to_unix() > 1000000' app.log
```

`e.timestamp` is not a valid timestamp.

**Solution:** Use safe access:
```bash
kelora -j --filter 'e.has_path("timestamp") && e.timestamp.to_unix() > 1000000' app.log
```

### Array Index Out of Bounds

**Problem:**
```bash
kelora -j --exec 'e.first = e.items[0]' app.log
```

`e.items` is empty or missing.

**Solution:** Check array length:
```bash
kelora -j --exec 'if e.has_path("items") && e.items.len() > 0 { e.first = e.items[0] }' app.log
```

### Division by Zero

**Problem:**
```bash
kelora -j --exec 'e.ratio = e.success / e.total' app.log
```

`e.total` is zero.

**Solution:** Add guard:
```bash
kelora -j --exec 'if e.total > 0 { e.ratio = e.success / e.total } else { e.ratio = 0.0 }' app.log
```

## Debugging Strategies

### Use Verbose Mode

See errors as they happen:

```bash
kelora -j --verbose app.log --exec 'e.result = e.value.to_int()'
```

### Enable Strict Mode

Find first error quickly:

```bash
kelora -j --strict app.log
```

### Inspect Problematic Lines

Use `--take` to limit processing:

```bash
kelora -j --strict --take 100 app.log
```

Process only first 100 lines to find issues faster.

### Check Field Existence

Verify fields exist before accessing:

```bash
kelora -j --exec 'if !e.has_path("value") { eprint("Line missing value: " + e) }' app.log
```

### Use Type Checking

Verify field types before operations:

```bash
kelora -j --exec 'if type_of(e.value) != "i64" { eprint("Value is not integer: " + e.value) }' app.log
```

### Validate Input Format

Test parsing with strict mode:

```bash
kelora -j --strict -s app.log
```

No output, but exits with error if parsing fails.

## Error Messages

### Parse Error Format

```
⚠️  kelora: line 42: parse error - invalid JSON at position 15
```

- `line 42`: Line number in input
- `parse error`: Error category
- Details: Specific error message

### Filter Error Format

```
⚠️  kelora: line 42: filter error - field 'timestamp' not found
```

### Exec Error Format

```
⚠️  kelora: line 42: exec error - cannot convert 'abc' to integer
```

### Enhanced Error Summaries

With `--verbose`, get example errors:

```
🔹 Processed 1000 lines, 950 events output, 50 errors

Error examples:
  line 42: exec error - cannot convert 'abc' to integer
  line 103: exec error - field 'value' not found
  line 287: filter error - timestamp is null
```

## Best Practices

### Use Resilient Mode for Production

Production logs are messy - resilient mode handles gracefully:

```bash
kelora -j app.log --levels error --keys timestamp,message
```

### Use Strict Mode for Validation

Validate data quality in pipelines:

```bash
kelora -j --strict app.log > /dev/null && echo "✓ Valid"
```

### Combine Quiet and Exit Codes

For automation, use exit codes:

```bash
kelora --silent app.log
if [ $? -eq 0 ]; then
    echo "No errors"
else
    echo "Has errors"
fi
```

### Add Defensive Checks

Use safe field access patterns:

```bash
kelora -j --exec 'e.result = e.get_path("nested.value", 0) * 2' app.log
```

### Log Errors to File

Capture errors for later analysis:

```bash
kelora -j --verbose app.log 2> errors.log
```

### Use Stats for Summary

Get error counts without verbose output:

```bash
kelora -j --stats app.log
```

Shows error count in summary.

## Parallel Processing

### Error Handling in Parallel Mode

When using `--parallel`, error handling works the same:

```bash
kelora -j --parallel app.log
```

- Errors still recorded per event
- Summary shows total errors across all threads
- Exit code reflects any errors from any thread

### Verbose with Parallel

Verbose errors are shown immediately, but may be interleaved:

```bash
kelora -j --parallel --verbose app.log
```

Errors from different threads may appear out of order.

### Strict with Parallel

First error from any thread aborts all processing:

```bash
kelora -j --parallel --strict app.log
```

## See Also

- [Pipeline Model]pipeline-model.md - How error handling fits into processing stages
- [Scripting Stages]scripting-stages.md - Error handling in --begin/--exec/--end
- [CLI Reference]../reference/cli-reference.md - All error handling flags
- [Exit Codes Reference]../reference/exit-codes.md - Complete exit code documentation