Files
swift-log/Sources/Logging/Docs.docc/DisableLogLevelsDuringCompilation.md
Vladimir KukushkinandFranz Busch 7dc6101ae4 [SLG-0004]: metadata value attributes implementation (#453)
Proposal discussion thread:
https://forums.swift.org/t/proposal-slg-0004-metadata-value-attributes-revision-2/86488

Add an extensible per-value attribute mechanism for metadata.

### Motivation:

Metadata values in `swift-log` are opaque strings by the time the
`LogHandler` receives them. The call site often
knows things about a value that the handler cannot infer — for example,
whether the value should be redacted in
different environments.

### Modifications:

- SLG-0004 proposal is updated based on the feedback. 
- Added `AttributedStringCarrier: CustomStringConvertible`-based
mechanism to `Logging`.
- Added benchmarks and benchmark thresholds for the new attributed
metadata.

### Result:

`swift-log` provides an optional extensible mechanism for attaching
attributes to metadata values.

---------

Co-authored-by: Franz Busch <privat@franz-busch.de>
2026-05-22 16:06:57 +01:00

3.1 KiB

Disable log levels during compilation

SwiftLog provides compile-time traits to eliminate less severe log levels from your binary reducing the runtime overhead.

Overview

Motivation

When deploying applications to production, you often know in advance which log levels will never be needed. For example, a production service might only need warning and above, while trace and debug levels are only useful during development. By using traits, you can completely remove these unnecessary log levels at compile time, achieving zero runtime overhead.

Available traits

SwiftLog defines seven maximum log level traits, ordered from most permissive to most restrictive:

  • MaxLogLevelDebug: Debug and above available (compiles out trace)
  • MaxLogLevelInfo: Info and above available (compiles out trace, debug)
  • MaxLogLevelNotice: Notice and above available (compiles out trace, debug, info)
  • MaxLogLevelWarning: Warning and above available (compiles out trace, debug, info, notice)
  • MaxLogLevelError: Error and above available (compiles out trace, debug, info, notice, warning)
  • MaxLogLevelCritical: Only critical available (compiles out all except critical)
  • MaxLogLevelNone: All logging compiled out (no log levels available)

By default (when no traits are specified), all log levels are available.

When you specify a maximum log level trait, all less severe levels are completely removed from your binary at compile time. This applies to both level-specific methods (e.g., logger.debug()) and calls to the generic logger.log(level:) method. Traits are additive. If multiple max level traits are specified, the most restrictive one takes effect.

Note: Traits should only be set by the applications and not libraries as any traits defined in a transitive dependency will affect the whole resolution tree.

Example

To enable a trait, specify it when declaring your package dependency:

// In your Package.swift:
dependencies: [
    .package(
        url: "https://github.com/apple/swift-log.git",
        from: "1.0.0",
        traits: ["MaxLogLevelWarning"]
    )
]

With MaxLogLevelWarning enabled, all trace, debug, info, and notice log statements are compiled out:

// These become no-ops (compiled out completely):
logger.trace("This will not be in the binary")
logger.debug("This will not be in the binary")
logger.info("This will not be in the binary")
logger.notice("This will not be in the binary")
logger.log(level: .debug, "This will not log anything")

// These work normally:
logger.warning("This still works")
logger.error("This still works")
logger.critical("This still works")
logger.log(level: .error, "This still works")

When MaxLogLevelNone is enabled no log statement can ever fire, so mutations that only configure the underlying LogHandler for future log statements are also compiled out. Specifically, the body of the Logger/subscript(metadataKey:) setter and the body of the Logger/logLevel setter become empty, so calls like logger[metadataKey: "user-id"] = "\(id)" and logger.logLevel = .info do not reach the handler. Reads continue to work and return whatever the handler reports.