Custom Log Formats

Not every log is the same...

Gonzo supports custom log formats through YAML configuration files, allowing you to parse logs from any application and convert them to OpenTelemetry (OTLP) attributes for analysis.

Using Built-in Formats

Gonzo includes pre-built formats in the formats directoryarrow-up-right:

Available formats:

  • loki-stream.yaml - Grafana Loki streaming (individual entries)

  • loki-batch.yaml - Loki batch format with multi-entry expansion

  • vercel-stream.yaml - Vercel logs

  • nodejs.yaml - Node.js application logs

  • apache-combined.yaml - Apache/Nginx access logs

Setup:

# Download and install format
mkdir -p ~/.config/gonzo/formats
cp <format-file>.yaml ~/.config/gonzo/formats/

# Use the format
gonzo --format=loki-stream -f logs.json

# List available formats
ls ~/.config/gonzo/formats/

Examples:

Creating Your Own Custom Formats

Quick Start

1. Create a Format File

Create a YAML file in ~/.config/gonzo/formats/ directory:

2. Define Your Format

3. Use the Format

Basic Structure

Format Types

text - Plain text logs with regex patterns:

json - JSON structured logs:

structured - Fixed position logs (Apache-style):

Common Regex Patterns

Pattern
Description
Example

[\d\-T:\.]+

ISO timestamp

2024-01-15T10:30:45.123

\w+

Word characters

ERROR, INFO

\d+

Digits

12345

[^\]]+

Everything except ]

Content inside brackets

.*

Any characters

Rest of line

\S+

Non-whitespace

Token or word

Time Formats

Format
Example
Description

rfc3339

2024-01-15T10:30:45Z

ISO 8601

unix

1705316445

Unix seconds

unix_ms

1705316445123

Unix milliseconds

unix_ns

1705316445123456789

Unix nanoseconds

auto

Various

Auto-detect format

"2006-01-02 15:04:05"

2024-01-15 10:30:45

Custom Go format

Field Transforms

  • uppercase: Convert to uppercase (info → INFO)

  • lowercase: Convert to lowercase (ERROR → error)

  • trim: Remove whitespace (" text " → "text")

  • status_to_severity: HTTP status to severity (200→INFO, 404→WARN, 500→ERROR)

Complete Examples

Example 1: Node.js Application Logs

Log format: [Backend] 5300 LOG [Module] Message +6ms

Example 2: Kubernetes/Docker JSON Logs

Format configuration:

Example 3: Apache Access Logs

Log format: 192.168.1.1 - - [14/Oct/2024:10:30:45 +0000] "GET /api/users HTTP/1.1" 200 1234

Advanced Features

Batch Processing

For logs where a single line contains multiple entries (like Loki batch format):

How it works:

  1. Original line: {"streams":[{"stream":{"service":"app"},"values":[["1234","msg1"],["5678","msg2"]]}]}

  2. Gets expanded to: 2 separate log entries

  3. Each entry retains the stream metadata

Common patterns:

  • logs[] - Expand top-level array

  • streams[].values[] - Expand nested arrays (Loki)

  • events[].entries[] - Multi-level expansion

Nested JSON Fields

Access nested fields using dot notation:

Pattern Extraction

Extract values from within a field:

Conditional Defaults

Use defaults when fields are missing:

HTTP Status Code to Severity Mapping

For web server logs, use the status_to_severity transform:

Status code mapping:

  • 1xx (100-199): DEBUG (Informational)

  • 2xx (200-299): INFO (Success)

  • 3xx (300-399): INFO (Redirection)

  • 4xx (400-499): WARN (Client Error)

  • 5xx (500-599): ERROR (Server Error)

Multiple Pattern Matching

Define additional patterns for specific fields:

Testing & Troubleshooting

Test your format:

Common issues:

  1. Pattern not matching: Test regex at regex101.com, verify named groups (?P<name>...)

  2. Wrong timestamps: Check time_format matches exactly, use Go format syntax

  3. Missing attributes: Verify field paths (use dot notation for nested: user.profile.name)

  4. Performance issues: Use specific patterns instead of .*, avoid overly complex regex

Debug tips:

  • Start with simple patterns, add complexity gradually

  • Use defaults for optional fields

  • Test with various log samples

  • Check Gonzo output for parsing errors

Best Practices

  • Document your format: Add description and example log lines

  • Use meaningful names: Descriptive field names aid understanding

  • Handle edge cases: Provide defaults for optional fields

  • Test thoroughly: Verify with various log samples

  • Version control: Keep formats in Git for team sharing

  • Optimize patterns: Specific patterns perform better than generic ones

Additional Resources

  • Format Examples: https://github.com/control-theory/gonzo/tree/main/formats

  • Full Guide: https://github.com/control-theory/gonzo/blob/main/guides/CUSTOM_FORMATS.md

  • Quick Reference: https://github.com/control-theory/gonzo/blob/main/guides/FORMAT_QUICK_REFERENCE.md

  • Issue Tracker: https://github.com/control-theory/gonzo/issues

Last updated