kelora 0.6.1

A command-line log analysis tool with embedded Rhai scripting
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
# Exit Codes

Complete reference for Kelora's exit codes and their meanings.

## Standard Exit Codes

Kelora uses standard Unix exit codes to indicate success or failure:

| Code | Name | Meaning | Cause |
|------|------|---------|-------|
| `0` | Success | No errors occurred | Clean processing, no parse/filter/exec errors |
| `1` | General Error | Processing errors occurred | Parse errors, filter errors, exec errors |
| `2` | Usage Error | Invalid command-line usage | Invalid flags, missing files, configuration errors |

## Signal Exit Codes

When Kelora is interrupted by signals:

| Code | Signal | Meaning |
|------|--------|---------|
| `130` | SIGINT | Interrupted by Ctrl+C |
| `141` | SIGPIPE | Broken pipe (normal in Unix pipelines) |
| `143` | SIGTERM | Terminated by system or user |

## Exit Code 0: Success

### When It Occurs

Exit code `0` indicates **successful processing** with no errors:

- All lines parsed successfully
- No filter errors
- No exec/transform errors
- Processing completed normally

### Example

```bash
> kelora -j app.log --levels info
> echo $?
0
```

**Note:** Filtering events is **not** considered an error. If all events are filtered out, exit code is still `0` (as long as no errors occurred).

### Filtering vs Errors

```bash
# This returns 0 (filtering is not an error)
> kelora -j app.log --levels critical
> echo $?
0

# This returns 1 (parse error occurred)
> kelora -j malformed.log
> echo $?
1
```

## Exit Code 1: Processing Errors

### When It Occurs

Exit code `1` indicates **processing errors** occurred:

- **Parse errors**: Lines that couldn't be parsed in specified format
- **Filter errors**: `--filter` expressions that failed during evaluation
- **Exec errors**: `--exec` scripts that failed during execution

### Resilient Mode (Default)

In resilient mode, errors are recorded but processing continues. Exit code `1` is returned at the end if any errors occurred:

```bash
> kelora -j app.log --exec 'e.result = e.value.to_int()'
# ... processing continues despite errors ...
> echo $?
1  # Errors occurred but processing completed
```

### Strict Mode

In strict mode (`--strict`), processing aborts immediately on first error:

```bash
> kelora -j --strict app.log
# ... aborts on first error ...
> echo $?
1  # Processing aborted due to error
```

### Parse Errors

**Example:**
```bash
> echo '{"valid": "json"}' > test.log
> echo '{invalid json}' >> test.log
> kelora -j test.log
> echo $?
1  # Parse error occurred
```

### Filter Errors

**Example:**
```bash
> kelora -j app.log --filter 'e.timestamp.to_unix() > 1000000'
# If e.timestamp is missing or invalid
> echo $?
1  # Filter error occurred
```

### Exec Errors

**Example:**
```bash
> kelora -j app.log --exec 'e.result = e.value.to_int()'
# If e.value is not a valid integer
> echo $?
1  # Exec error occurred
```

## Exit Code 2: Usage Errors

### When It Occurs

Exit code `2` indicates **command-line usage errors**:

- **Invalid flags**: Unknown or malformed options
- **Missing required arguments**: Required values not provided
- **File not found**: Input files don't exist
- **Configuration errors**: Invalid configuration file
- **Permission denied**: Can't read input files

### Invalid Flags

```bash
> kelora --invalid-flag app.log
kelora: error: unrecognized option '--invalid-flag'
> echo $?
2
```

### File Not Found

```bash
> kelora -j nonexistent.log
kelora: error: failed to open file 'nonexistent.log': No such file or directory
> echo $?
2
```

### Invalid Configuration

```bash
> kelora --config-file invalid.ini app.log
kelora: error: failed to parse configuration file
> echo $?
2
```

## Signal Exit Codes

### Exit Code 130: SIGINT (Ctrl+C)

User interrupted processing with Ctrl+C:

```bash
> kelora -j large.log
# Press Ctrl+C
^C
> echo $?
130
```

### Exit Code 141: SIGPIPE (Broken Pipe)

Output pipe closed (normal in Unix pipelines):

```bash
> kelora -j large.log | head -n 10
# Kelora receives SIGPIPE when head closes
> echo $?
141
```

**Note:** Exit code `141` is **normal** in pipelines and typically not an error condition.

### Exit Code 143: SIGTERM

Process terminated by system or user:

```bash
> kelora -j large.log &
[1] 12345
> kill 12345
[1]+  Terminated              kelora -j large.log
> echo $?
143
```

## Using Exit Codes in Scripts

### Basic Success/Failure Check

```bash
if kelora -j app.log > /dev/null 2>&1; then
    echo "✓ No errors in logs"
else
    echo "✗ Errors detected in logs"
    exit 1
fi
```

### Check Specific Exit Codes

```bash
kelora -j app.log
exit_code=$?

case $exit_code in
    0)
        echo "✓ Success - no errors"
        ;;
    1)
        echo "✗ Processing errors occurred"
        exit 1
        ;;
    2)
        echo "✗ Usage error - check command syntax"
        exit 2
        ;;
    130)
        echo "⚠️  Interrupted by user"
        exit 130
        ;;
    *)
        echo "✗ Unknown error (exit code: $exit_code)"
        exit $exit_code
        ;;
esac
```

### With Quiet Mode

Use quiet modes to suppress output, rely on exit code:

```bash
# Level 1: Suppress diagnostics
kelora -q -j app.log > /dev/null
if [ $? -eq 0 ]; then
    echo "No errors"
fi

# Level 2: Suppress events too
kelora -qq -j app.log
if [ $? -eq 0 ]; then
    echo "No errors"
fi

# Level 3: Complete silence
kelora -qqq -j app.log
if [ $? -eq 0 ]; then
    echo "No errors"
fi
```

### CI/CD Pipeline

```bash
#!/bin/bash
set -e  # Exit on any error

# Validate log format
kelora -j --strict app.log > /dev/null || {
    echo "✗ Log validation failed"
    exit 1
}

# Check for errors in logs
if ! kelora -qq -j app.log --levels error,critical; then
    echo "✗ Found error-level events in logs"
    exit 1
fi

echo "✓ All checks passed"
```

### Makefile Integration

```makefile
.PHONY: check-logs
check-logs:
	@kelora -j logs/*.json --levels error || \
		(echo "Errors found in logs" && exit 1)

.PHONY: validate-logs
validate-logs:
	@kelora -j --strict logs/*.json > /dev/null || \
		(echo "Log validation failed" && exit 1)
```

### GitHub Actions

```yaml
- name: Validate logs
  run: |
    kelora -j --strict app.log || exit 1

- name: Check for errors
  run: |
    if kelora -qq -j app.log --levels error,critical; then
      echo "✓ No critical errors"
    else
      echo "✗ Critical errors found"
      exit 1
    fi
```

## Exit Codes with Different Modes

### Sequential vs Parallel

Exit codes work the same in both modes:

```bash
# Sequential
> kelora -j app.log
> echo $?

# Parallel
> kelora -j --parallel app.log
> echo $?
```

Both return `1` if any errors occurred.

### Resilient vs Strict

**Resilient mode (default):**

- Collects all errors
- Returns `1` if any errors occurred
- Processing completes

```bash
> kelora -j app.log
# Processes all lines despite errors
> echo $?
1  # Errors occurred
```

**Strict mode:**

- Aborts on first error
- Returns `1` immediately
- Processing incomplete

```bash
> kelora -j --strict app.log
# Aborts on first error
> echo $?
1  # Aborted due to error
```

### With Quiet Modes

Exit codes are preserved at all quiet levels:

```bash
# Level 1: Suppress diagnostics
> kelora -q -j app.log
> echo $?
1  # Error occurred (not shown, but exit code preserved)

# Level 2: Suppress events
> kelora -qq -j app.log
> echo $?
1  # Error occurred

# Level 3: Complete silence
> kelora -qqq -j app.log
> echo $?
1  # Error occurred
```

## Common Patterns

### Validation Pipeline

```bash
#!/bin/bash
# Validate log files before processing

for file in logs/*.json; do
    if ! kelora -j --strict "$file" > /dev/null 2>&1; then
        echo "✗ Invalid format: $file"
        exit 1
    fi
done

echo "✓ All log files valid"
```

### Data Quality Check

```bash
#!/bin/bash
# Check for errors in logs

kelora -qq -j app.log --levels error,critical
exit_code=$?

if [ $exit_code -eq 1 ]; then
    echo "✗ Parse errors in log file"
    exit 1
elif [ $exit_code -eq 0 ]; then
    # Check if any critical events found
    count=$(kelora -j app.log --levels critical -F none --stats 2>&1 | grep -oP '\d+(?= events output)')
    if [ "$count" -gt 0 ]; then
        echo "✗ Found $count critical events"
        exit 1
    fi
fi

echo "✓ No critical issues"
```

### Automation with Retry

```bash
#!/bin/bash
# Process logs with retry on failure

max_attempts=3
attempt=1

while [ $attempt -le $max_attempts ]; do
    if kelora -j app.log -F json > output.json; then
        echo "✓ Processing succeeded"
        exit 0
    else
        echo "✗ Attempt $attempt failed"
        attempt=$((attempt + 1))
        sleep 5
    fi
done

echo "✗ Processing failed after $max_attempts attempts"
exit 1
```

### Conditional Processing

```bash
#!/bin/bash
# Process only if no errors

if kelora -qq -j --strict app.log; then
    echo "✓ No errors, processing..."
    kelora -j app.log --exec 'track_count(e.service)' --metrics
else
    echo "✗ Errors detected, skipping processing"
    exit 1
fi
```

## Troubleshooting

### Exit Code 1 When Expecting 0

**Problem:** Getting exit code `1` but don't see errors.

**Solution:** Use `--verbose` to see errors:
```bash
> kelora -j --verbose app.log
```

Or check stats:
```bash
> kelora -j --stats app.log
```

### Exit Code 141 in Pipelines

**Problem:** Getting exit code `141` (SIGPIPE) in pipelines.

**This is normal:** When downstream commands close the pipe (like `head`), Kelora receives SIGPIPE.

```bash
> kelora -j large.log | head -n 10
# Exit code 141 is normal here
```

To ignore SIGPIPE:
```bash
> kelora -j large.log | head -n 10 || [ $? -eq 141 ]
```

### Exit Code 2 with Valid Syntax

**Problem:** Getting exit code `2` but command looks correct.

**Check:**

- File exists and is readable
- Configuration file is valid
- Permissions are correct

```bash
> ls -l app.log
> kelora -j app.log
```

### Different Exit Codes in CI vs Local

**Problem:** Different exit codes in CI/CD vs local development.

**Possible causes:**

- Different file permissions
- Different file paths
- Different configuration files
- Different environment variables

**Solution:** Test with same conditions:
```bash
> kelora --ignore-config -j app.log  # Ignore config files
```

## See Also

- [Error Handling]../concepts/error-handling.md - Error handling modes and strategies
- [CLI Reference]cli-reference.md - Complete flag documentation
- [Quiet Modes]../concepts/error-handling.md#quiet-modes - Suppressing output for automation