Skip to content

Logger metadata setter is not a no-op when MaxLogLevelNone trait is enabled #462

Description

@rnro

When the MaxLogLevelNone trait is enabled, all log-emitting methods on Logger (trace, debug, …, critical, and log(level:)) compile out to empty bodies — see for example the critical method's #if !MaxLogLevelNone guard at https://github.com/apple/swift-log/blob/main/Sources/Logging/Logger.swift#L989. The intent of the trait is to "achieve zero runtime overhead" for log levels that are turned off, with MaxLogLevelNone turning off everything.

However, the metadata subscript setter at https://github.com/apple/swift-log/blob/main/Sources/Logging/Logger.swift#L322-L330 is not guarded by the same trait check:

  public subscript(metadataKey metadataKey: String) -> Logger.Metadata.Value? {
      get {
          self.handler[metadataKey: metadataKey]
      }
      set {
          self.handler[metadataKey: metadataKey] = newValue
      }
  }

So code like logger[metadataKey: "user-id"] = "\(id)" still goes through the underlying LogHandler even when no log statement could ever observe that metadata.

We could wrap the setter body in #if !MaxLogLevelNone, mirroring the pattern used by the level-specific log methods. It might also be worth considering whether the logLevel setter at https://github.com/apple/swift-log/blob/main/Sources/Logging/Logger.swift#L338-L346 should also be modified.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions