Skip to content

Commit de3fc5e

Browse files
committed
Add option to pass Errors to log functions
Motivation: Addresses #291 Modifications: Add new API to Logger, that allows the caller to pass an Error object to any log function. Extend LogHandler to allow an implementation to access the error, in order to format the log appropriately. Provide default implementaions to preserve backwards compatibility. Result: By providing default implementations, the change should be compatible between old and new code, both from the API and implementation side. I.e. a client can use the new API and it will still work with 'old' implementations, and vice versa.
1 parent a90a3d1 commit de3fc5e

File tree

6 files changed

+468
-29
lines changed

6 files changed

+468
-29
lines changed

Sources/Logging/LogHandler.swift

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,20 @@ public protocol LogHandler: _SwiftLogSendableLogHandler {
136136
/// - file: The file the log message was emitted from.
137137
/// - function: The function the log line was emitted from.
138138
/// - line: The line the log message was emitted from.
139+
func log(
140+
level: Logger.Level,
141+
message: Logger.Message,
142+
error: Error?,
143+
metadata: Logger.Metadata?,
144+
source: String,
145+
file: String,
146+
function: String,
147+
line: UInt
148+
)
149+
150+
/// SwiftLog 1.6 compatibility method. Please do _not_ implement, implement
151+
/// `log(level:message:error:metadata:source:file:function:line:)` instead.
152+
@available(*, deprecated, renamed: "log(level:message:error:metadata:source:file:function:line:)")
139153
func log(
140154
level: Logger.Level,
141155
message: Logger.Message,
@@ -147,8 +161,8 @@ public protocol LogHandler: _SwiftLogSendableLogHandler {
147161
)
148162

149163
/// SwiftLog 1.0 compatibility method. Please do _not_ implement, implement
150-
/// `log(level:message:metadata:source:file:function:line:)` instead.
151-
@available(*, deprecated, renamed: "log(level:message:metadata:source:file:function:line:)")
164+
/// `log(level:message:error:metadata:source:file:function:line:)` instead.
165+
@available(*, deprecated, renamed: "log(level:message:error:metadata:source:file:function:line:)")
152166
func log(
153167
level: Logging.Logger.Level,
154168
message: Logging.Logger.Message,
@@ -196,6 +210,7 @@ extension LogHandler {
196210
level: .warning,
197211
message:
198212
"Attempted to set metadataProvider on \(Self.self) that did not implement support for them. Please contact the log handler maintainer to implement metadata provider support.",
213+
error: nil,
199214
metadata: nil,
200215
source: "Logging",
201216
file: #file,
@@ -210,6 +225,20 @@ extension LogHandler {
210225

211226
extension LogHandler {
212227
@available(*, deprecated, message: "You should implement this method instead of using the default implementation")
228+
public func log(
229+
level: Logger.Level,
230+
message: Logger.Message,
231+
error: Error?,
232+
metadata: Logger.Metadata?,
233+
source: String,
234+
file: String,
235+
function: String,
236+
line: UInt
237+
) {
238+
self.log(level: level, message: message, metadata: metadata, source: source, file: file, function: function, line: line)
239+
}
240+
241+
@available(*, deprecated, renamed: "log(level:message:error:metadata:source:file:function:line:)")
213242
public func log(
214243
level: Logger.Level,
215244
message: Logger.Message,
@@ -222,7 +251,7 @@ extension LogHandler {
222251
self.log(level: level, message: message, metadata: metadata, file: file, function: function, line: line)
223252
}
224253

225-
@available(*, deprecated, renamed: "log(level:message:metadata:source:file:function:line:)")
254+
@available(*, deprecated, renamed: "log(level:message:error:metadata:source:file:function:line:)")
226255
public func log(
227256
level: Logging.Logger.Level,
228257
message: Logging.Logger.Message,
@@ -234,6 +263,7 @@ extension LogHandler {
234263
self.log(
235264
level: level,
236265
message: message,
266+
error: nil,
237267
metadata: metadata,
238268
source: Logger.currentModule(filePath: file),
239269
file: file,

0 commit comments

Comments
 (0)