Skip to content

Include information from LogEvent.error in log output if present - #245

Open
samuelmurray wants to merge 3 commits into
vapor:mainfrom
samuelmurray:error-log-parameter
Open

Include information from LogEvent.error in log output if present#245
samuelmurray wants to merge 3 commits into
vapor:mainfrom
samuelmurray:error-log-parameter

Conversation

@samuelmurray

Copy link
Copy Markdown

In swift-log, a user can attach an error object to a log entry, which is passed as-is to the LogHandler for it to serialise. ConsoleLogger, and the default fragment, is updated to use this property, serialising it as [ Fully.Qualified.Type: string representation ].

Example:

do {
  try throwingFunc()
} catch {
  logger.warning("Something went wrong", error: error) // Outputs "[...] Something went wrong [ ThrownError: description ] [...]"
}

Fixes #239

public init(
level: Logger.Level,
message: Logger.Message,
error: (any Error)?,

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I'm assuming breaking changes are ok, since it's a beta release?

@samuelmurray

Copy link
Copy Markdown
Author

I didn't spend too much time thinking of a good output format for the error, so I'm happy to adjust if you have any suggestions. One alternative is to simply treat it as metadata, and use some sensible keys, eg. error.type and error.message. This is how StreamLogHandler in swift-log does it (https://github.com/apple/swift-log/blob/main/Sources/Logging/Handlers/StreamLogHandler.swift#L197-L200), though I'm not suggesting Vapor has to do the same.

@ptoffy
ptoffy requested a review from fpseverino August 14, 2026 08:55
@codecov

codecov Bot commented Aug 14, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 72.18%. Comparing base (5acfda6) to head (57a5041).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #245      +/-   ##
==========================================
+ Coverage   71.86%   72.18%   +0.31%     
==========================================
  Files          29       29              
  Lines         878      888      +10     
==========================================
+ Hits          631      641      +10     
  Misses        247      247              
Files with missing lines Coverage Δ
Sources/ConsoleLogger/ConsoleLogger.swift 87.27% <100.00%> (+0.23%) ⬆️
Sources/ConsoleLogger/LogRecord.swift 100.00% <100.00%> (ø)
...ogger/LoggerFragments/DefaultLoggerFragments.swift 100.00% <100.00%> (ø)
...ConsoleLogger/LoggerFragments/LoggerFragment.swift 92.15% <100.00%> (+0.37%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@fpseverino fpseverino left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM, the placement of the ErrorFragment is correct IMO, I'm not too sure about the output format, but we can change that later without breaking API

Comment thread Tests/ConsoleLoggerTests/TestError.swift Outdated

@0xTim 0xTim left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Error output should definitely go in metadata. Only static strings should be added to the actual message as those are what are queried so need to be the same

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.

Support error parameter from swift-log

3 participants