Files
swift-log/Sources/Logging/Docs.docc/DisableLogLevelsDuringCompilation.md
T
Rick Newton-Rogers 1069d31898 Skip handler dispatch for setters under MaxLogLevelNone (#465)
Skip handler dispatch for setters under `MaxLogLevelNone`

 ### Motivation

* When `MaxLogLevelNone` is enabled every
`Logger.trace`/`debug`/.../`critical` method and `Logger.log(level:)`
compiles down to an empty body, so no log statement can ever observe
what the underlying `LogHandler` sees.
* Despite this, the `subscript(metadataKey:)` and `logLevel` setters
still went through the handler. Code like `logger[metadataKey:
"user-id"] = "\(id)"` would CoW the logger storage and call into the
handler even though nothing could read the metadata back out via a log
statement.

### Modifications

* Wrap the bodies of `Logger.subscript(metadataKey:) { set }` and
`Logger.logLevel { set }` in `#if !MaxLogLevelNone`, mirroring the
existing guards on the level-specific log methods.
* Note this behaviour in `DisableLogLevelsDuringCompilation.md` so
callers know reads still go through the handler but writes do not.
* Add a new benchmark which ensures that the operations don't allocate.
* Update some missed swift-tools-version bumps.
* Loosen p90 instruction threshold to 10%. The same source produces
results that swing by up to ~8% between back-to-back runs.
### Result

* `logger[metadataKey:] = ...` and `logger.logLevel = ...` compile to
empty bodies when `MaxLogLevelNone` is set, matching the
zero-runtime-overhead guarantee the trait already provides for log
emission.
* Resolves https://github.com/apple/swift-log/issues/462
2026-05-21 13:55:40 +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.

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.