rsnx - Rust Nginx Log Parser
A Rust library for parsing nginx access logs, inspired by the Go library gonx.
Installation
From crates.io (Future)
Add this to your Cargo.toml
:
[]
= "0.1.0"
Basic Usage
use Reader;
use Cursor;
let log_data = r#"127.0.0.1 [08/Nov/2013:13:39:18 +0000] "GET /api/foo HTTP/1.1" 200 612"#;
let format = r#"$remote_addr [$time_local] "$request" $status $body_bytes_sent"#;
let cursor = new;
let reader = new?;
for entry in reader
Nginx Configuration Integration
use NginxReader;
use Cursor;
let nginx_config = r#"
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
"#;
let log_data = r#"127.0.0.1 - - [08/Nov/2013:13:39:18 +0000] "GET /api/foo HTTP/1.1" 200 612 "-" "curl/7.64.1" "-""#;
let config_cursor = new;
let log_cursor = new;
let reader = new?;
for entry in reader
Supported Log Formats
The library supports any nginx log format that uses $variable
syntax. Common formats include:
Common Log Format
$remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent
Combined Log Format
$remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent"
Custom Formats
$remote_addr [$time_local] "$request" $status $request_time "$http_user_agent"
API Reference
Entry
The Entry
struct represents a parsed log line with methods for type-safe field access:
// String access
let ip = entry.field?;
// Integer access
let status = entry.int_field?; // i32
let bytes = entry.int64_field?; // i64
// Float access
let request_time = entry.float_field?; // f64
// Field manipulation
entry.set_field;
entry.set_uint_field;
entry.set_float_field;
// Utility methods
let partial = entry.partial;
let hash = entry.fields_hash;
entry.merge;
Reader
The Reader
struct provides an iterator interface for processing log files:
// Create reader
let reader = new?;
// Iterator interface
for entry in reader
// Collect all entries
let entries = reader.collect_all?;
// Process with closure
reader.process_entries?;
NginxReader
The NginxReader
extracts log formats from nginx configuration files:
let reader = new?;
Error Handling
The library provides comprehensive error handling with detailed error messages:
use Error;
match entry.field
Error types include:
FieldNotFound
: When a requested field doesn't existFieldParseError
: When type conversion failsLineFormatMismatch
: When a log line doesn't match the expected formatInvalidFormat
: When a format string is invalidNginxFormatNotFound
: When a log format isn't found in nginx configIo
: For I/O related errors
Performance
The library is designed for efficient log processing:
- Lazy Parsing: Log lines are parsed on-demand as you iterate
- Zero-Copy Field Access: String fields return references to avoid copying
- Compiled Regex: Format strings are compiled once and reused
- Memory Efficient: Suitable for processing large log files
Examples
See the examples directory for more comprehensive usage examples:
basic.rs
: Basic usage patterns and error handling
Run examples with:
Testing
Run the test suite:
# Run all tests
# Run with output
# Run integration tests only
Features
Optional Features
serde
: Enable serialization/deserialization support forEntry
[]
= { = "0.1.0", = ["serde"] }
Comparison with gonx
This library aims to provide similar functionality to the Go library gonx while following Rust idioms:
Feature | gonx (Go) | rsnx (Rust) |
---|---|---|
Format parsing | ✅ | ✅ |
Nginx config parsing | ✅ | ✅ |
Type-safe field access | ✅ | ✅ |
Iterator interface | ✅ | ✅ |
Error handling | (value, error) |
Result<T, Error> |
Memory management | GC | Ownership |
Concurrency | Goroutines | (Future: async/await) |
License
This project is licensed under the MIT License - see the LICENSE file for details.
Getting Started from GitHub
1. Clone and Test Locally
# Clone the repository
# Run tests to verify everything works
# Run the example to see it in action
# Build the library
2. Use in Your Project
Add to your Cargo.toml
:
[]
= { = "https://github.com/kktsuiac770/rsnx.git", = "v0.1.0" }
3. Available Tags/Versions
v0.1.0
- Initial stable release with full functionalitymain
- Latest development version (may include unreleased features)
4. Example Project Setup
Create a new Rust project and use rsnx:
# Create new project
# Add rsnx to Cargo.toml
Then use it in your src/main.rs
:
use Reader;
use File;
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.