Skip to content

Commit

Permalink
fix: clean up logging configuration
Browse files Browse the repository at this point in the history
- Fix layer ownership in logging setup
- Remove unused log_level variable
- Set default startup log level to INFO
- Preserve trace_frames configuration when enabled
  • Loading branch information
aljen committed Dec 6, 2024
1 parent f33dd84 commit 0d92c2f
Showing 1 changed file with 21 additions and 23 deletions.
44 changes: 21 additions & 23 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,40 +39,38 @@ pub fn setup_logging(config: Option<&RelayConfig>) -> Result<(), RelayError> {
time::format_description::well_known::Rfc3339,
);

// Build initial subscriber
let layer = tracing_subscriber::fmt::layer()
.with_target(false)
.with_thread_ids(true)
.with_thread_names(true)
.with_timer(timer);
// Configure log level filter
let mut env_filter = EnvFilter::default();

// Configure based on config or defaults
let layer = if let Some(cfg) = config {
let mut env_filter =
EnvFilter::default().add_directive(cfg.logging.get_level_filter().into());
// Configure based on config
let include_location = if let Some(cfg) = config {
// Use configured log level
env_filter = env_filter.add_directive(cfg.logging.get_level_filter().into());

if cfg.logging.trace_frames {
env_filter = env_filter
.add_directive("modbus_relay::protocol=trace".parse().unwrap())
.add_directive("modbus_relay::transport=trace".parse().unwrap());
}

layer
.with_file(cfg.logging.include_location)
.with_line_number(cfg.logging.include_location)
.with_level(true)
.with_filter(env_filter)
cfg.logging.include_location
} else {
// Default trace-level logging for startup
let env_filter = EnvFilter::default().add_directive(LevelFilter::TRACE.into());

layer
.with_file(true)
.with_line_number(true)
.with_level(true)
.with_filter(env_filter)
// Use INFO level for startup
env_filter = env_filter.add_directive(LevelFilter::INFO.into());
true
};

// Build subscriber with all configuration
let layer = tracing_subscriber::fmt::layer()
.with_target(false)
.with_thread_ids(true)
.with_thread_names(true)
.with_timer(timer)
.with_file(include_location)
.with_line_number(include_location)
.with_level(true)
.with_filter(env_filter);

let subscriber = Registry::default().with(layer);

// Set up global subscriber only once
Expand Down

0 comments on commit 0d92c2f

Please sign in to comment.