Skip to content

[Feature] Default log level to INFO/WARN in release builds to reduce overhead #100

@vmarcella

Description

@vmarcella

Overview

The global logger defaults to TRACE level and eagerly formats messages when enabled, which can be extremely costly in inner loops. This issue proposes changing the default log level to reduce unnecessary overhead in release builds while preserving detailed logging capability during development.

Current State

The global logger currently defaults to TRACE level regardless of build configuration:

// crates/lambda-rs-logging/src/lib.rs:81-88
pub fn global() -> &'static Arc<Self> {
  static GLOBAL: OnceLock<Arc<Logger>> = OnceLock::new();
  GLOBAL.get_or_init(|| {
    let logger = Logger::new(LogLevel::TRACE, "lambda-rs");  // Always TRACE
    logger.add_handler(Box::new(handler::ConsoleHandler::new("lambda-rs")));
    Arc::new(logger)
  })
}

This means that in release builds, all log messages down to TRACE level are formatted and processed, even when such verbosity is rarely needed in production. Eager formatting of messages in hot paths (e.g., inner render loops) incurs significant CPU overhead.

Scope

Goals:

  • Reduce logging overhead in release builds by defaulting to a higher (less verbose) log level
  • Preserve detailed logging capability during development via debug builds
  • Allow runtime override of log level via environment variables

Non-Goals:

  • Removing TRACE/DEBUG log statements from code
  • Changing the logging API surface
  • Adding compile-time log level filtering (macro-based elision)

Proposed API

No new public API required. The change is internal to the default log level selection:

// crates/lambda-rs-logging/src/lib.rs

impl Logger {
  pub fn global() -> Self {
    #[cfg(debug_assertions)]
    let default_level = Level::DEBUG;
    
    #[cfg(not(debug_assertions))]
    let default_level = Level::INFO; // or Level::WARN
    
    Self {
      level: default_level,
      // ...
    }
  }
}

Recommended Usage:

  • Call logging::env::init_global_from_env() early in application startup to allow environment-based override
  • Document the default levels in crate documentation

Acceptance Criteria

  • Default log level is INFO (or WARN) in release builds (cfg(not(debug_assertions)))
  • Default log level is DEBUG in debug builds (cfg(debug_assertions))
  • Environment variable override continues to function correctly
  • Documentation updated to describe default log levels per build configuration
  • Existing examples and tests pass without modification

Affected Crates

lambda-rs-logging

Notes

  • Consider also calling logging::env::init_global_from_env() early in app startup in examples to demonstrate best practice
  • This change improves performance without breaking existing functionality since users can still opt into TRACE via environment variables

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions