Skip to content

feat: add hierarchical Logger::named, Logger::with_level_filter, and Record::logger_name - #314

Closed
tisonkun wants to merge 1 commit into
mainfrom
feat/named-logger
Closed

tisonkun wants to merge 1 commit into
mainfrom
feat/named-logger

Conversation

@tisonkun

@tisonkun tisonkun commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Summary

Subsystems and libraries receiving an injected Logger previously lacked a way to attach a hierarchical channel name or restrict handle verbosity locally without rebuilding dispatch pipelines, and logforth-bridge-log had to forward custom log targets (log::info!(target: "sqlx::query", ...)) into RustLogFilter through a synthetic "logforth.log_target" key-value entry that leaked into downstream layouts and appenders.

This PR introduces first-class hierarchical logger naming and monotonic handle-level narrowing on Logger, backed by Record::logger_name (related to #276):

  • Logger::{name, named, with_level_filter} (logforth-core): Splits Logger's internal Arc into LoggerState { level_mask, name, pipeline: Arc<Pipeline> } so Logger remains pointer-sized and Logger::enabled stays a single pointer dereference.
    • Logger::named joins segments with "::" (root.named("db").named("wal") -> "db::wal"), keeps Logger::noop() a zero-allocation no-op, treats "" as an identity clone, and preserves 'static provenance for single-segment static names.
    • Logger::with_level_filter intersects the handle's level_mask with LevelFilter, narrowing verbosity locally on the derived handle without starving parent/sibling handles or bypassing downstream dispatch filters.
  • Record::logger_name & target precedence (logforth-core, logforth-bridge-log, logforth-filter-rustlog):
    • Adds Record::{logger_name, logger_name_static} and RecordBuilder::{logger_name, logger_name_static}, preserved across Record::to_builder, Record::to_owned, and RecordOwned::with.
    • Logger::log stamps Logger::name() onto a stack-copied RecordBuilder only when record.logger_name().is_none(), so explicit per-record names (including custom log targets bridged by LogBridge) take precedence over outer handle names.
    • Removes the "logforth.log_target" magic key-value entry completely; RustLogFilter now matches prefix directives against record.logger_name().or_else(|| record.module_path()).
  • Layouts & OpenTelemetry (PlainTextLayout, TextLayout, JsonLayout, LogfmtLayout, GoogleCloudLoggingLayout, OpentelemetryLog):
Component Behavior when Record::logger_name() is Some(name) Behavior when None
PlainTextLayout Renders [name] immediately after severity (INFO [db::wal] app::storage: ...) Unchanged
TextLayout Renders [name] immediately after severity Unchanged
JsonLayout Serializes "logger": name before "module_path" Omitted
LogfmtLayout Encodes logger=<name> before module= Omitted
GoogleCloudLoggingLayout Routes "logger" to jsonPayload or logging.googleapis.com/labels (overridable by diagnostics/record KVs) Omitted
OpentelemetryLog Calls LogRecord::set_target so OTLP exports group records into ScopeLogs by logger name Preserves OpentelemetryLogBuilder::new(name, ...) scope

Test Plan

  • cargo x check
  • cargo x test
  • cargo x lint

…Record::logger_name

Subsystems and dependency libraries passed an explicit `Logger` previously had no first-class way to identify their logical channel or narrow verbosity locally without cloning dispatch pipelines, while `logforth-bridge-log` had to smuggle explicit `log` target overrides (`log::info!(target: "sqlx::query", ...)`) into `RustLogFilter` through a reserved `"logforth.log_target"` key-value pair that leaked into formatted records.

Split `Logger`'s shared state into `LoggerState` (`level_mask`, `name`, and `Arc<Pipeline>`) and introduce first-class logical logger names and handle-local level narrowing:

- Add `Logger::{name, named, with_level_filter}` in `logforth-core`: `named` joins hierarchical segments with `"::"` (`Logger::noop()` remains a no-op and empty segments preserve the current name), and `with_level_filter` narrows the handle's precomputed `level_mask` via bitwise intersection without affecting parent or sibling handles.
- Add `Record::{logger_name, logger_name_static}` and `RecordBuilder::{logger_name, logger_name_static}` (preserved across `to_builder`, `to_owned`, and `RecordOwned::with`), stamping a named `Logger`'s name onto records only when `Record::logger_name` is unset so explicit per-record overrides win.
- Replace the `"logforth.log_target"` key-value bridge workaround in `logforth-bridge-log` and `logforth-filter-rustlog`: `LogBridge` maps custom `log` targets to `RecordBuilder::logger_name`, and `RustLogFilter` matches prefix directives against `Record::logger_name` before falling back to `Record::module_path`.
- Emit optional logger names across built-in layouts and OpenTelemetry (`[logger]` in `PlainTextLayout` and `TextLayout`, `"logger"` in `JsonLayout` and `GoogleCloudLoggingLayout`, `logger=` in `LogfmtLayout`, and `LogRecord::set_target` in `OpentelemetryLog` when `Record::logger_name` is present).

Signed-off-by: tison <wander4096@gmail.com>
@tisonkun

tisonkun commented Dec 1, 2026

Copy link
Copy Markdown
Contributor Author

Closing after further design evaluation:

  1. Logger::with_level_filter turns Logger's precomputed level_mask (an optimization union cache over Dispatch masks) into a cross-dispatch gate that can starve downstream debug or dynamic-promotion dispatches, conflicting with Dispatch-level filter orthogonality.
  2. Logger::named and Record::logger_name introduce subtle precedence collisions across RustLogFilter (module_path shadowing and global LogBridge root contamination), LogBridge (log callsite target: overrides escaping named handles), and OpentelemetryLog (LogRecord::set_target clobbering or conflicting with explicit InstrumentationScope configuration).
  3. Existing orthogonal primitives already cover these use cases cleanly: compile-time module_path + RustLogFilter for module/crate scoping, independent Logger pipelines for isolated channels, shared Arc<Append> + StaticDiagnostic for multi-instance disambiguation, and "logforth.log_target" for explicit log target interop.

@tisonkun tisonkun closed this Dec 1, 2026
@tisonkun
tisonkun deleted the feat/named-logger branch December 1, 2026 13:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant