Crates.io | cosmian_logger |
lib.rs | cosmian_logger |
version | 0.4.0 |
created_at | 2025-04-17 06:39:54.571812+00 |
updated_at | 2025-08-22 12:07:48.308275+00 |
description | Logger init |
homepage | |
repository | https://github.com/Cosmian/http_client_server |
max_upload_size | |
id | 1637292 |
size | 76,758 |
A versatile logging and tracing utility for Rust applications that provides:
Add the dependency to your Cargo.toml
:
[dependencies]
cosmian_logger = { path = "../path/to/crate/logger" }
For simple applications, use the log_init
function to set up logging:
use cosmian_logger::log_init;
use tracing::{debug, info};
fn main() {
// Initialize with custom log level
log_init(Some("debug"));
info!("This is an info message");
debug!("This is a debug message");
}
The log_init
function accepts an optional log level string parameter:
None
is provided, it falls back to the RUST_LOG
environment variableFor more advanced use cases with OpenTelemetry integration:
use cosmian_logger::{tracing_init, TelemetryConfig, TracingConfig};
use tracing::span;
use tracing_core::Level;
#[tokio::main]
async fn main() {
let config = TracingConfig {
service_name: "my_service".to_string(),
otlp: Some(TelemetryConfig {
version: Some("1.0.0".to_string()),
environment: Some("development".to_string()),
otlp_url: "http://localhost:4317".to_string(),
enable_metering: true,
}),
no_log_to_stdout: false,
#[cfg(not(target_os = "windows"))]
log_to_syslog: true,
rust_log: Some("debug".to_string()),
};
let _otel_guard = tracing_init(&config);
// Create and enter a span for better tracing context
let span = span!(Level::TRACE, "application");
let _span_guard = span.enter();
// Your application code here
tracing::info!("Application started");
}
To use OpenTelemetry, start a collector like Jaeger:
docker run -p16686:16686 -p4317:4317 -p4318:4318 \
-e COLLECTOR_OTLP_ENABLED=true -e LOG_LEVEL=debug \
jaegertracing/jaeger:2.5.0
Then access the Jaeger UI at http://localhost:16686
The TracingConfig
struct supports:
service_name
: Name of your service for tracingotlp
: OpenTelemetry configuration (optional)no_log_to_stdout
: Disable logging to stdoutlog_to_syslog
: Enable logging to system logrust_log
: Log level configurationThe log_init
function is safe to use in tests:
#[test]
fn test_something() {
cosmian_logger::log_init(Some("debug"));
// Your test code
}
The logger crate re-exports common tracing utilities:
use cosmian_logger::reexport::{tracing, tracing_subscriber};