Skip to content

[SLG-0005]: LogEvent LogHandler API implementation#423

Open
kukushechkin wants to merge 9 commits intomainfrom
SLG-0005-log-event-LogHandler-API-implementation
Open

[SLG-0005]: LogEvent LogHandler API implementation#423
kukushechkin wants to merge 9 commits intomainfrom
SLG-0005-log-event-LogHandler-API-implementation

Conversation

@kukushechkin
Copy link
Contributor

Replace the flat-parameter log(level:message:metadata:source:file:function:line:) method on LogHandler with
log(event: LogEvent), enabling forward-compatible evolution of the LogHandler interface without breaking existing
handler implementations.

Motivation:

Changes to the LogHandler API require dance around creating new overloads, deprecating old overloads, which unnecessary bloats LogHandler protocol surface (like happened with the source: parameter). We have upcoming changes that would introduce new capabilities to the LogHandler protocol, for example https://forums.swift.org/t/proposal-slg-0003-standardized-error-metadata-via-logger-convenience/84518/35. It makes sense to introduce a single container structure for all the data associated with a log event before introducing any new overloads in LogHandler protocol.

Modifications:

  • New struct LogEvent is added.
  • LogHandler protocol now has an additional log(event: LogEvent) method.
  • Logger is calling handler's log(event: LogEvent) method.

Result:

Further changes to the LogEvent with new information passed from the Logger to a LogHandler does not require changes in the LogHandler API. As a bonus, LogEvent makes source a computable variable, improving performance for log handlers who do not need the default source created based on the log file location.

@kukushechkin kukushechkin added the 🆕 semver/minor Adds new public API. label Mar 9, 2026
@kukushechkin kukushechkin force-pushed the SLG-0005-log-event-LogHandler-API-implementation branch from 7ba9aed to 5409b18 Compare March 9, 2026 12:06
@kukushechkin kukushechkin force-pushed the SLG-0005-log-event-LogHandler-API-implementation branch from 5c6c628 to 0ea389f Compare March 19, 2026 14:37
@kukushechkin kukushechkin marked this pull request as ready for review March 19, 2026 15:18
#expect(event.source == "OverriddenSource")
}

@Test func sourceLazyDerivationDoesNotStoreUntilAccessed() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Maybe I'm reading this wrong, but to me it sounds like we expect it to "store after accessed". But what we test for is more like "sourceLazyDerivationDoesNotStoreEvenAfterAccessed"

Comment on lines +39 to +41
/// When no explicit source was provided at the call site, this is derived lazily from ``file``
/// the first time this property is accessed. Handlers that never read this property pay no
/// allocation cost for source computation.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: To me, this suggests that it will only be computed on first access. Is there a reason we don't store it in self._source after computing it? Otherwise, maybe the doc should say "[...] this is derived lazily from ``file`` when this property is accessed"?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🆕 semver/minor Adds new public API.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants