DS Common Logger Rust Library
A high-performance, production-ready logging library for Rust applications that provides structured logging with comprehensive tracing capabilities.
Features
- Environment-driven filtering - Respects
RUST_LOGenvironment variable - Dual output formats - JSON and compact formats via
LOG_FORMATenvironment variable - Error context capture - Automatic span trace capture for better debugging
- Panic logging - Converts panics to structured log events
- Thread-safe initialization - Idempotent initialization safe for concurrent use
- Zero-cost abstractions - Minimal runtime overhead when logging is disabled
Installation
Add this to your Cargo.toml:
[]
= "0.1.0"
Or use cargo add:
Quick Start
Basic Usage
use init_tracing;
use ;
Structured Logging
use init_tracing;
use ;
Error Handling with Context
use init_tracing;
use ;
use io;
Configuration
Log Level Filtering
Control log levels using the RUST_LOG environment variable:
# Show all logs at info level and above
RUST_LOG=info
# Show only error logs
RUST_LOG=error
# Show debug logs for specific modules
RUST_LOG=debug,my_crate::module=info
# Show all logs for your crate, but only errors for dependencies
RUST_LOG=debug,my_crate=debug
Output Format
Switch between compact and JSON output using the LOG_FORMAT environment variable:
# Compact format (default) - human-readable
# JSON format - machine-readable
LOG_FORMAT=json
Example Output
Compact Format:
2024-01-15T10:30:45.123Z INFO my_app: Application started
2024-01-15T10:30:45.124Z WARN my_app: This is a warning message
2024-01-15T10:30:45.125Z ERROR my_app: This is an error message
JSON Format:
Advanced Usage
Custom Spans and Events
use init_tracing;
use ;
Panic Logging
The library automatically captures panics as structured log events:
use init_tracing;
Thread Safety
The library is thread-safe and can be used in multi-threaded applications:
use init_tracing;
use info;
use thread;
Environment Variables
The library uses the following environment variables:
RUST_LOG- Controls log level filtering (e.g.,info,debug,error)LOG_FORMAT- Controls output format (jsonfor JSON format, anything else for compact)
License
This project is licensed under either of
- Apache License, Version 2.0, (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT License (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.