Skip to content

Commit 12d3a86

Browse files
glbrnttktoso
andauthored
Add back accidentally removed API (apple#152)
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. Co-authored-by: Konrad `ktoso` Malawski <[email protected]>
1 parent b544051 commit 12d3a86

File tree

3 files changed

+239
-2
lines changed

3 files changed

+239
-2
lines changed

Sources/Logging/Logging.swift

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,29 @@ extension Logger {
8181
}
8282
}
8383

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

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

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

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

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

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

388+
/// Log a message passing with the `Logger.Level.error` log level.
389+
///
390+
/// If `.error` is at least as severe as the `Logger`'s `logLevel`, it will be logged,
391+
/// otherwise nothing will happen.
392+
///
393+
/// - parameters:
394+
/// - message: The message to be logged. `message` can be used with any string interpolation literal.
395+
/// - metadata: One-off metadata to attach to this log message.
396+
/// - file: The file this log message originates from (there's usually no need to pass it explicitly as it
397+
/// defaults to `#file`).
398+
/// - function: The function this log message originates from (there's usually no need to pass it explicitly as
399+
/// it defaults to `#function`).
400+
/// - line: The line this log message originates from (there's usually no need to pass it explicitly as it
401+
/// defaults to `#line`).
402+
@inlinable
403+
public func error(_ message: @autoclosure () -> Logger.Message,
404+
metadata: @autoclosure () -> Logger.Metadata? = nil,
405+
file: String = #file, function: String = #function, line: UInt = #line) {
406+
self.error(message(), metadata: metadata(), source: nil, file: file, function: function, line: line)
407+
}
408+
260409
/// Log a message passing with the `Logger.Level.critical` log level.
261410
///
262411
/// `.critical` messages will always be logged.
@@ -279,6 +428,28 @@ extension Logger {
279428
file: String = #file, function: String = #function, line: UInt = #line) {
280429
self.log(level: .critical, message(), metadata: metadata(), source: source(), file: file, function: function, line: line)
281430
}
431+
432+
/// Log a message passing with the `Logger.Level.critical` log level.
433+
///
434+
/// `.critical` messages will always be logged.
435+
///
436+
/// - parameters:
437+
/// - message: The message to be logged. `message` can be used with any string interpolation literal.
438+
/// - metadata: One-off metadata to attach to this log message.
439+
/// - source: The source this log messages originates to. Currently, it defaults to the folder containing the
440+
/// file that is emitting the log message, which usually is the module.
441+
/// - file: The file this log message originates from (there's usually no need to pass it explicitly as it
442+
/// defaults to `#file`).
443+
/// - function: The function this log message originates from (there's usually no need to pass it explicitly as
444+
/// it defaults to `#function`).
445+
/// - line: The line this log message originates from (there's usually no need to pass it explicitly as it
446+
/// defaults to `#line`).
447+
@inlinable
448+
public func critical(_ message: @autoclosure () -> Logger.Message,
449+
metadata: @autoclosure () -> Logger.Metadata? = nil,
450+
file: String = #file, function: String = #function, line: UInt = #line) {
451+
self.critical(message(), metadata: metadata(), source: nil, file: file, function: function, line: line)
452+
}
282453
}
283454

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

Tests/LoggingTests/LoggingTest+XCTest.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ extension LoggingTest {
3939
("testCustomFactory", testCustomFactory),
4040
("testAllLogLevelsExceptCriticalCanBeBlocked", testAllLogLevelsExceptCriticalCanBeBlocked),
4141
("testAllLogLevelsWork", testAllLogLevelsWork),
42+
("testAllLogLevelByFunctionRefWithSource", testAllLogLevelByFunctionRefWithSource),
43+
("testAllLogLevelByFunctionRefWithoutSource", testAllLogLevelByFunctionRefWithoutSource),
4244
("testLogMessageWithStringInterpolation", testLogMessageWithStringInterpolation),
4345
("testLoggingAString", testLoggingAString),
4446
("testMultiplexerIsValue", testMultiplexerIsValue),

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)
@@ -733,14 +797,14 @@ class LoggingTest: XCTestCase {
733797

734798
var logger = Logger(label: "test")
735799
logger.logLevel = .error
736-
logger.error(Dummy())
800+
logger.error(error: Dummy())
737801

738802
logging.history.assertExist(level: .error, message: "errorDescription")
739803
}
740804
}
741805

742806
extension Logger {
743-
public func error(_ error: Error,
807+
public func error(error: Error,
744808
metadata: @autoclosure () -> Logger.Metadata? = nil,
745809
file: String = #file, function: String = #function, line: UInt = #line) {
746810
self.error("\(error.localizedDescription)", metadata: metadata(), file: file, function: function, line: line)

0 commit comments

Comments
 (0)