otel_logger/lib.rs
1#![deny(unsafe_code)]
2#![warn(
3 missing_docs,
4 missing_debug_implementations,
5 missing_copy_implementations,
6 trivial_casts,
7 trivial_numeric_casts,
8 unused_import_braces,
9 unused_qualifications
10)]
11//! OpenTelemetry logging configuration and initialization.
12//!
13//! This crate provides a flexible and configurable logging system that integrates
14//! OpenTelemetry tracing and metrics. It offers both programmatic configuration
15//! through a builder pattern and environment variable-based configuration.
16//!
17//! # Features
18//!
19//! - Builder-style configuration API
20//! - Environment variable support (with "env" feature)
21//! - Multiple log formats (compact, pretty, json)
22//! - SDK environment settings for sampling (`OTEL_TRACES_SAMPLER`) and metric export
23//! intervals (`OTEL_METRIC_EXPORT_INTERVAL`)
24//! - Custom resource attributes
25//! - Optional console output
26//! - Optional file output
27//!
28//! The default instrumentation scope name is `otel_logger`.
29//! The Logger service name identifies the tracer's instrumentation scope.
30//! Resource `service.name` uses SDK defaults or explicit resource attributes.
31//! Set `OTEL_SERVICE_NAME` to configure the resource service name.
32//!
33//! # Quick Start
34//!
35//! ```rust
36//! use otel_logger::Logger;
37//! use tracing::Level;
38//!
39//! #[tokio::main]
40//! async fn main() -> anyhow::Result<()> {
41//! // Basic initialization with console output
42//! let guard = Logger::new("my-service").init()?;
43//!
44//! // Your application code here...
45//!
46//! // The guard will automatically clean up when dropped
47//! Ok(())
48//! }
49//! ```
50//!
51//! # Advanced Configuration
52//!
53//! ```rust
54//! use otel_logger::Logger;
55//! use tracing::Level;
56//! use opentelemetry::KeyValue;
57//!
58//! #[tokio::main]
59//! async fn main() -> anyhow::Result<()> {
60//! let guard = Logger::new("my-service")
61//! .with_level(Level::DEBUG)
62//! .with_console_enabled(true)
63//! .with_attributes(vec![
64//! KeyValue::new("environment", "production"),
65//! KeyValue::new("version", "1.0.0"),
66//! ])
67//! .init()?;
68//!
69//! // Your application code here...
70//!
71//! Ok(())
72//! }
73//! ```
74
75mod config;
76mod deserialize;
77#[cfg(feature = "env")]
78mod env;
79mod guard;
80mod init;
81mod subscriber;
82
83// Re-exports
84pub use config::{LogFormat, LogRollingRotation, Logger, LoggerFileAppender};
85pub use deserialize::default;
86#[cfg(feature = "env")]
87pub use env::{init_logger_from_env, init_logging_from_env};
88pub use guard::LoggerGuard;
89pub use init::{init_logging, init_tracing_from_logger};
90pub use otel_init::{BoxLayer, OtelGuard, init_env_filter};
91pub use subscriber::{init_layer, setup_tracing};
92
93// Re-export FmtSpan
94pub use tracing_subscriber::fmt::format::FmtSpan;
95
96#[cfg(test)]
97mod tests;