Skip to content

Commit a36d5cf

Browse files
committed
Add back accidentally removed API
Motivation: - When `source:` was added in apple#135, the API was accidentally broken despite source compatability. Modifications: - Add back source-less API calls which defer to the decls with source. - Note we can't deprecate the calls we're adding back: `log.info("some message")` refers to the implementation we're adding back rather and would therefore require users to explicitly specify a source to suppress the deprecation warning message. Result: - API is no longer broken.
1 parent de30f5b commit a36d5cf

File tree

2 files changed

+237
-2
lines changed

2 files changed

+237
-2
lines changed

Sources/Logging/Logging.swift

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,29 @@ extension Logger {
7777
}
7878
}
7979

80+
/// Log a message passing the log level as a parameter.
81+
///
82+
/// If the `logLevel` passed to this method is more severe than the `Logger`'s `logLevel`, it will be logged,
83+
/// otherwise nothing will happen.
84+
///
85+
/// - parameters:
86+
/// - level: The log level to log `message` at. For the available log levels, see `Logger.Level`.
87+
/// - message: The message to be logged. `message` can be used with any string interpolation literal.
88+
/// - metadata: One-off metadata to attach to this log message.
89+
/// - file: The file this log message originates from (there's usually no need to pass it explicitly as it
90+
/// defaults to `#file`).
91+
/// - function: The function this log message originates from (there's usually no need to pass it explicitly as
92+
/// it defaults to `#function`).
93+
/// - line: The line this log message originates from (there's usually no need to pass it explicitly as it
94+
/// defaults to `#line`).
95+
@inlinable
96+
public func log(level: Logger.Level,
97+
_ message: @autoclosure () -> Logger.Message,
98+
metadata: @autoclosure () -> Logger.Metadata? = nil,
99+
file: String = #file, function: String = #function, line: UInt = #line) {
100+
self.log(level: level, message(), metadata: metadata(), source: nil, file: file, function: function, line: line)
101+
}
102+
80103
/// Add, change, or remove a logging metadata item.
81104
///
82105
/// - note: Logging metadata behaves as a value that means a change to the logging metadata will only affect the
@@ -133,6 +156,25 @@ extension Logger {
133156
self.log(level: .trace, message(), metadata: metadata(), source: source(), file: file, function: function, line: line)
134157
}
135158

159+
/// If `.trace` is at least as severe as the `Logger`'s `logLevel`, it will be logged,
160+
/// otherwise nothing will happen.
161+
///
162+
/// - parameters:
163+
/// - message: The message to be logged. `message` can be used with any string interpolation literal.
164+
/// - metadata: One-off metadata to attach to this log message
165+
/// - file: The file this log message originates from (there's usually no need to pass it explicitly as it
166+
/// defaults to `#file`).
167+
/// - function: The function this log message originates from (there's usually no need to pass it explicitly as
168+
/// it defaults to `#function`).
169+
/// - line: The line this log message originates from (there's usually no need to pass it explicitly as it
170+
/// defaults to `#line`).
171+
@inlinable
172+
public func trace(_ message: @autoclosure () -> Logger.Message,
173+
metadata: @autoclosure () -> Logger.Metadata? = nil,
174+
file: String = #file, function: String = #function, line: UInt = #line) {
175+
self.trace(message(), metadata: metadata(), source: nil, file: file, line: line)
176+
}
177+
136178
/// Log a message passing with the `Logger.Level.debug` log level.
137179
///
138180
/// If `.debug` is at least as severe as the `Logger`'s `logLevel`, it will be logged,
@@ -157,6 +199,27 @@ extension Logger {
157199
self.log(level: .debug, message(), metadata: metadata(), source: source(), file: file, function: function, line: line)
158200
}
159201

202+
/// Log a message passing with the `Logger.Level.debug` log level.
203+
///
204+
/// If `.debug` is at least as severe as the `Logger`'s `logLevel`, it will be logged,
205+
/// otherwise nothing will happen.
206+
///
207+
/// - parameters:
208+
/// - message: The message to be logged. `message` can be used with any string interpolation literal.
209+
/// - metadata: One-off metadata to attach to this log message.
210+
/// - file: The file this log message originates from (there's usually no need to pass it explicitly as it
211+
/// defaults to `#file`).
212+
/// - function: The function this log message originates from (there's usually no need to pass it explicitly as
213+
/// it defaults to `#function`).
214+
/// - line: The line this log message originates from (there's usually no need to pass it explicitly as it
215+
/// defaults to `#line`).
216+
@inlinable
217+
public func debug(_ message: @autoclosure () -> Logger.Message,
218+
metadata: @autoclosure () -> Logger.Metadata? = nil,
219+
file: String = #file, function: String = #function, line: UInt = #line) {
220+
self.debug(message(), metadata: metadata(), source: nil, file: file, function: function, line: line)
221+
}
222+
160223
/// Log a message passing with the `Logger.Level.info` log level.
161224
///
162225
/// If `.info` is at least as severe as the `Logger`'s `logLevel`, it will be logged,
@@ -181,6 +244,27 @@ extension Logger {
181244
self.log(level: .info, message(), metadata: metadata(), source: source(), file: file, function: function, line: line)
182245
}
183246

247+
/// Log a message passing with the `Logger.Level.info` log level.
248+
///
249+
/// If `.info` is at least as severe as the `Logger`'s `logLevel`, it will be logged,
250+
/// otherwise nothing will happen.
251+
///
252+
/// - parameters:
253+
/// - message: The message to be logged. `message` can be used with any string interpolation literal.
254+
/// - metadata: One-off metadata to attach to this log message.
255+
/// - file: The file this log message originates from (there's usually no need to pass it explicitly as it
256+
/// defaults to `#file`).
257+
/// - function: The function this log message originates from (there's usually no need to pass it explicitly as
258+
/// it defaults to `#function`).
259+
/// - line: The line this log message originates from (there's usually no need to pass it explicitly as it
260+
/// defaults to `#line`).
261+
@inlinable
262+
public func info(_ message: @autoclosure () -> Logger.Message,
263+
metadata: @autoclosure () -> Logger.Metadata? = nil,
264+
file: String = #file, function: String = #function, line: UInt = #line) {
265+
self.info(message(), metadata: metadata(), source: nil, file: file, function: function, line: line)
266+
}
267+
184268
/// Log a message passing with the `Logger.Level.notice` log level.
185269
///
186270
/// If `.notice` is at least as severe as the `Logger`'s `logLevel`, it will be logged,
@@ -205,6 +289,29 @@ extension Logger {
205289
self.log(level: .notice, message(), metadata: metadata(), source: source(), file: file, function: function, line: line)
206290
}
207291

292+
/// Log a message passing with the `Logger.Level.notice` log level.
293+
///
294+
/// If `.notice` is at least as severe as the `Logger`'s `logLevel`, it will be logged,
295+
/// otherwise nothing will happen.
296+
///
297+
/// - parameters:
298+
/// - message: The message to be logged. `message` can be used with any string interpolation literal.
299+
/// - metadata: One-off metadata to attach to this log message.
300+
/// - source: The source this log messages originates to. Currently, it defaults to the folder containing the
301+
/// file that is emitting the log message, which usually is the module.
302+
/// - file: The file this log message originates from (there's usually no need to pass it explicitly as it
303+
/// defaults to `#file`).
304+
/// - function: The function this log message originates from (there's usually no need to pass it explicitly as
305+
/// it defaults to `#function`).
306+
/// - line: The line this log message originates from (there's usually no need to pass it explicitly as it
307+
/// defaults to `#line`).
308+
@inlinable
309+
public func notice(_ message: @autoclosure () -> Logger.Message,
310+
metadata: @autoclosure () -> Logger.Metadata? = nil,
311+
file: String = #file, function: String = #function, line: UInt = #line) {
312+
self.notice(message(), metadata: metadata(), source: nil, file: file, function: function, line: line)
313+
}
314+
208315
/// Log a message passing with the `Logger.Level.warning` log level.
209316
///
210317
/// If `.warning` is at least as severe as the `Logger`'s `logLevel`, it will be logged,
@@ -229,6 +336,27 @@ extension Logger {
229336
self.log(level: .warning, message(), metadata: metadata(), source: source(), file: file, function: function, line: line)
230337
}
231338

339+
/// Log a message passing with the `Logger.Level.warning` log level.
340+
///
341+
/// If `.warning` is at least as severe as the `Logger`'s `logLevel`, it will be logged,
342+
/// otherwise nothing will happen.
343+
///
344+
/// - parameters:
345+
/// - message: The message to be logged. `message` can be used with any string interpolation literal.
346+
/// - metadata: One-off metadata to attach to this log message.
347+
/// - file: The file this log message originates from (there's usually no need to pass it explicitly as it
348+
/// defaults to `#file`).
349+
/// - function: The function this log message originates from (there's usually no need to pass it explicitly as
350+
/// it defaults to `#function`).
351+
/// - line: The line this log message originates from (there's usually no need to pass it explicitly as it
352+
/// defaults to `#line`).
353+
@inlinable
354+
public func warning(_ message: @autoclosure () -> Logger.Message,
355+
metadata: @autoclosure () -> Logger.Metadata? = nil,
356+
file: String = #file, function: String = #function, line: UInt = #line) {
357+
self.warning(message(), metadata: metadata(), source: nil, file: file, function: function, line: line)
358+
}
359+
232360
/// Log a message passing with the `Logger.Level.error` log level.
233361
///
234362
/// If `.error` is at least as severe as the `Logger`'s `logLevel`, it will be logged,
@@ -253,6 +381,27 @@ extension Logger {
253381
self.log(level: .error, message(), metadata: metadata(), source: source(), file: file, function: function, line: line)
254382
}
255383

384+
/// Log a message passing with the `Logger.Level.error` log level.
385+
///
386+
/// If `.error` is at least as severe as the `Logger`'s `logLevel`, it will be logged,
387+
/// otherwise nothing will happen.
388+
///
389+
/// - parameters:
390+
/// - message: The message to be logged. `message` can be used with any string interpolation literal.
391+
/// - metadata: One-off metadata to attach to this log message.
392+
/// - file: The file this log message originates from (there's usually no need to pass it explicitly as it
393+
/// defaults to `#file`).
394+
/// - function: The function this log message originates from (there's usually no need to pass it explicitly as
395+
/// it defaults to `#function`).
396+
/// - line: The line this log message originates from (there's usually no need to pass it explicitly as it
397+
/// defaults to `#line`).
398+
@inlinable
399+
public func error(_ message: @autoclosure () -> Logger.Message,
400+
metadata: @autoclosure () -> Logger.Metadata? = nil,
401+
file: String = #file, function: String = #function, line: UInt = #line) {
402+
self.error(message(), metadata: metadata(), source: nil, file: file, function: function, line: line)
403+
}
404+
256405
/// Log a message passing with the `Logger.Level.critical` log level.
257406
///
258407
/// `.critical` messages will always be logged.
@@ -275,6 +424,28 @@ extension Logger {
275424
file: String = #file, function: String = #function, line: UInt = #line) {
276425
self.log(level: .critical, message(), metadata: metadata(), source: source(), file: file, function: function, line: line)
277426
}
427+
428+
/// Log a message passing with the `Logger.Level.critical` log level.
429+
///
430+
/// `.critical` messages will always be logged.
431+
///
432+
/// - parameters:
433+
/// - message: The message to be logged. `message` can be used with any string interpolation literal.
434+
/// - metadata: One-off metadata to attach to this log message.
435+
/// - source: The source this log messages originates to. Currently, it defaults to the folder containing the
436+
/// file that is emitting the log message, which usually is the module.
437+
/// - file: The file this log message originates from (there's usually no need to pass it explicitly as it
438+
/// defaults to `#file`).
439+
/// - function: The function this log message originates from (there's usually no need to pass it explicitly as
440+
/// it defaults to `#function`).
441+
/// - line: The line this log message originates from (there's usually no need to pass it explicitly as it
442+
/// defaults to `#line`).
443+
@inlinable
444+
public func critical(_ message: @autoclosure () -> Logger.Message,
445+
metadata: @autoclosure () -> Logger.Metadata? = nil,
446+
file: String = #file, function: String = #function, line: UInt = #line) {
447+
self.critical(message(), metadata: metadata(), source: nil, file: file, function: function, line: line)
448+
}
278449
}
279450

280451
/// The `LoggingSystem` is a global facility where the default logging backend implementation (`LogHandler`) can be

Tests/LoggingTests/LoggingTest.swift

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,70 @@ class LoggingTest: XCTestCase {
408408
testLogging.history.assertExist(level: .critical, message: "yes: critical")
409409
}
410410

411+
func testAllLogLevelByFunctionRefWithSource() {
412+
let testLogging = TestLogging()
413+
LoggingSystem.bootstrapInternal(testLogging.make)
414+
415+
var logger = Logger(label: "\(#function)")
416+
logger.logLevel = .trace
417+
418+
let trace = logger.trace(_:metadata:source:file:function:line:)
419+
let debug = logger.debug(_:metadata:source:file:function:line:)
420+
let info = logger.info(_:metadata:source:file:function:line:)
421+
let notice = logger.notice(_:metadata:source:file:function:line:)
422+
let warning = logger.warning(_:metadata:source:file:function:line:)
423+
let error = logger.error(_:metadata:source:file:function:line:)
424+
let critical = logger.critical(_:metadata:source:file:function:line:)
425+
426+
trace("yes: trace", [:], "foo", #file, #function, #line)
427+
debug("yes: debug", [:], "foo", #file, #function, #line)
428+
info("yes: info", [:], "foo", #file, #function, #line)
429+
notice("yes: notice", [:], "foo", #file, #function, #line)
430+
warning("yes: warning", [:], "foo", #file, #function, #line)
431+
error("yes: error", [:], "foo", #file, #function, #line)
432+
critical("yes: critical", [:], "foo", #file, #function, #line)
433+
434+
testLogging.history.assertExist(level: .trace, message: "yes: trace", source: "foo")
435+
testLogging.history.assertExist(level: .debug, message: "yes: debug", source: "foo")
436+
testLogging.history.assertExist(level: .info, message: "yes: info", source: "foo")
437+
testLogging.history.assertExist(level: .notice, message: "yes: notice", source: "foo")
438+
testLogging.history.assertExist(level: .warning, message: "yes: warning", source: "foo")
439+
testLogging.history.assertExist(level: .error, message: "yes: error", source: "foo")
440+
testLogging.history.assertExist(level: .critical, message: "yes: critical", source: "foo")
441+
}
442+
443+
func testAllLogLevelByFunctionRefWithoutSource() {
444+
let testLogging = TestLogging()
445+
LoggingSystem.bootstrapInternal(testLogging.make)
446+
447+
var logger = Logger(label: "\(#function)")
448+
logger.logLevel = .trace
449+
450+
let trace = logger.trace(_:metadata:file:function:line:)
451+
let debug = logger.debug(_:metadata:file:function:line:)
452+
let info = logger.info(_:metadata:file:function:line:)
453+
let notice = logger.notice(_:metadata:file:function:line:)
454+
let warning = logger.warning(_:metadata:file:function:line:)
455+
let error = logger.error(_:metadata:file:function:line:)
456+
let critical = logger.critical(_:metadata:file:function:line:)
457+
458+
trace("yes: trace", [:], #file, #function, #line)
459+
debug("yes: debug", [:], #file, #function, #line)
460+
info("yes: info", [:], #file, #function, #line)
461+
notice("yes: notice", [:], #file, #function, #line)
462+
warning("yes: warning", [:], #file, #function, #line)
463+
error("yes: error", [:], #file, #function, #line)
464+
critical("yes: critical", [:], #file, #function, #line)
465+
466+
testLogging.history.assertExist(level: .trace, message: "yes: trace")
467+
testLogging.history.assertExist(level: .debug, message: "yes: debug")
468+
testLogging.history.assertExist(level: .info, message: "yes: info")
469+
testLogging.history.assertExist(level: .notice, message: "yes: notice")
470+
testLogging.history.assertExist(level: .warning, message: "yes: warning")
471+
testLogging.history.assertExist(level: .error, message: "yes: error")
472+
testLogging.history.assertExist(level: .critical, message: "yes: critical")
473+
}
474+
411475
func testLogMessageWithStringInterpolation() {
412476
let testLogging = TestLogging()
413477
LoggingSystem.bootstrapInternal(testLogging.make)
@@ -711,14 +775,14 @@ class LoggingTest: XCTestCase {
711775

712776
var logger = Logger(label: "test")
713777
logger.logLevel = .error
714-
logger.error(Dummy())
778+
logger.error(error: Dummy())
715779

716780
logging.history.assertExist(level: .error, message: "errorDescription")
717781
}
718782
}
719783

720784
extension Logger {
721-
public func error(_ error: Error,
785+
public func error(error: Error,
722786
metadata: @autoclosure () -> Logger.Metadata? = nil,
723787
file: String = #file, function: String = #function, line: UInt = #line) {
724788
self.error("\(error.localizedDescription)", metadata: metadata(), file: file, function: function, line: line)

0 commit comments

Comments
 (0)