mirror of
https://github.com/apple/swift-log
synced 2026-11-27 04:14:20 +00:00
-log remove the LoggerWithSource for the time being
This commit is contained in:
1 parent
c37c434930
commit
024f44d4d5
6 files changed
+1
-365
No files matched your search
@@ -1,293 +0,0 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This source file is part of the Swift Logging API open source project
|
||||
//
|
||||
// Copyright (c) 2020 Apple Inc. and the Swift Logging API project authors
|
||||
// Licensed under Apache License v2.0
|
||||
//
|
||||
// See LICENSE.txt for license information
|
||||
// See CONTRIBUTORS.txt for the list of Swift Logging API project authors
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
/// `LoggerWithSource` shares the same API as `Logger`, except that it automatically parses on the supplies `source`
|
||||
/// instead of requiring the user to supply source when logging a message.
|
||||
///
|
||||
/// - info: Do not accept or pass `LoggerWithSource` to/from other modules. The type you use publicly should always be
|
||||
/// `Logger`.
|
||||
public struct LoggerWithSource {
|
||||
/// The `Logger` we are logging with.
|
||||
public var logger: Logger
|
||||
|
||||
/// The source information we are supplying to `Logger`.
|
||||
public var source: String
|
||||
|
||||
/// Construct a `LoggerWithSource` logging with `logger` and `source`.
|
||||
@inlinable
|
||||
public init(_ logger: Logger, source: String) {
|
||||
self.logger = logger
|
||||
self.source = source
|
||||
}
|
||||
}
|
||||
|
||||
extension LoggerWithSource {
|
||||
/// Log a message passing the log level as a parameter.
|
||||
///
|
||||
/// If the `logLevel` passed to this method is more severe than the `Logger`'s `logLevel`, it will be logged,
|
||||
/// otherwise nothing will happen. The `source` is the one supplied to the initializer of `LoggerWithSource`.
|
||||
///
|
||||
/// - parameters:
|
||||
/// - level: The log level to log `message` at. For the available log levels, see `Logger.Level`.
|
||||
/// - message: The message to be logged. `message` can be used with any string interpolation literal.
|
||||
/// - metadata: One-off metadata to attach to this log message
|
||||
/// - file: The file this log message originates from (there's usually no need to pass it explicitly as it
|
||||
/// defaults to `#file`).
|
||||
/// - function: The function this log message originates from (there's usually no need to pass it explicitly as
|
||||
/// it defaults to `#function`).
|
||||
/// - line: The line this log message originates from (there's usually no need to pass it explicitly as it
|
||||
/// defaults to `#line`).
|
||||
@inlinable
|
||||
public func log(level: Logger.Level,
|
||||
_ message: @autoclosure () -> Logger.Message,
|
||||
metadata: @autoclosure () -> Logger.Metadata? = nil,
|
||||
file: String = #file, function: String = #function, line: UInt = #line) {
|
||||
self.logger.log(level: level,
|
||||
message(),
|
||||
metadata: metadata(),
|
||||
source: self.source,
|
||||
file: file, function: function, line: line)
|
||||
}
|
||||
|
||||
/// Add, change, or remove a logging metadata item.
|
||||
///
|
||||
/// The `source` is the one supplied to the initializer of `LoggerWithSource`.
|
||||
///
|
||||
/// - note: Logging metadata behaves as a value that means a change to the logging metadata will only affect the
|
||||
/// very `Logger` it was changed on.
|
||||
@inlinable
|
||||
public subscript(metadataKey metadataKey: String) -> Logger.Metadata.Value? {
|
||||
get {
|
||||
return self.logger[metadataKey: metadataKey]
|
||||
}
|
||||
set {
|
||||
self.logger[metadataKey: metadataKey] = newValue
|
||||
}
|
||||
}
|
||||
|
||||
/// Get or set the log level configured for this `Logger`.
|
||||
///
|
||||
/// The `source` is the one supplied to the initializer of `LoggerWithSource`.
|
||||
///
|
||||
/// - note: `Logger`s treat `logLevel` as a value. This means that a change in `logLevel` will only affect this
|
||||
/// very `Logger`. It it acceptable for logging backends to have some form of global log level override
|
||||
/// that affects multiple or even all loggers. This means a change in `logLevel` to one `Logger` might in
|
||||
/// certain cases have no effect.
|
||||
@inlinable
|
||||
public var logLevel: Logger.Level {
|
||||
get {
|
||||
return self.logger.logLevel
|
||||
}
|
||||
set {
|
||||
self.logger.logLevel = newValue
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension LoggerWithSource {
|
||||
/// Log a message passing with the `Logger.Level.trace` log level.
|
||||
///
|
||||
/// The `source` is the one supplied to the initializer of `LoggerWithSource`.
|
||||
///
|
||||
/// If `.trace` is at least as severe as the `Logger`'s `logLevel`, it will be logged,
|
||||
/// otherwise nothing will happen.
|
||||
///
|
||||
/// - parameters:
|
||||
/// - message: The message to be logged. `message` can be used with any string interpolation literal.
|
||||
/// - metadata: One-off metadata to attach to this log message
|
||||
/// - file: The file this log message originates from (there's usually no need to pass it explicitly as it
|
||||
/// defaults to `#file`).
|
||||
/// - function: The function this log message originates from (there's usually no need to pass it explicitly as
|
||||
/// it defaults to `#function`).
|
||||
/// - line: The line this log message originates from (there's usually no need to pass it explicitly as it
|
||||
/// defaults to `#line`).
|
||||
@inlinable
|
||||
public func trace(_ message: @autoclosure () -> Logger.Message,
|
||||
metadata: @autoclosure () -> Logger.Metadata? = nil,
|
||||
file: String = #file, function: String = #function, line: UInt = #line) {
|
||||
self.logger.trace(message(),
|
||||
metadata: metadata(),
|
||||
source: self.source,
|
||||
file: file,
|
||||
function: function,
|
||||
line: line)
|
||||
}
|
||||
|
||||
/// Log a message passing with the `Logger.Level.debug` log level.
|
||||
///
|
||||
/// The `source` is the one supplied to the initializer of `LoggerWithSource`.
|
||||
///
|
||||
/// If `.debug` is at least as severe as the `Logger`'s `logLevel`, it will be logged,
|
||||
/// otherwise nothing will happen.
|
||||
///
|
||||
/// - parameters:
|
||||
/// - message: The message to be logged. `message` can be used with any string interpolation literal.
|
||||
/// - metadata: One-off metadata to attach to this log message
|
||||
/// - file: The file this log message originates from (there's usually no need to pass it explicitly as it
|
||||
/// defaults to `#file`).
|
||||
/// - function: The function this log message originates from (there's usually no need to pass it explicitly as
|
||||
/// it defaults to `#function`).
|
||||
/// - line: The line this log message originates from (there's usually no need to pass it explicitly as it
|
||||
/// defaults to `#line`).
|
||||
@inlinable
|
||||
public func debug(_ message: @autoclosure () -> Logger.Message,
|
||||
metadata: @autoclosure () -> Logger.Metadata? = nil,
|
||||
file: String = #file, function: String = #function, line: UInt = #line) {
|
||||
self.logger.debug(message(),
|
||||
metadata: metadata(),
|
||||
source: self.source,
|
||||
file: file,
|
||||
function: function,
|
||||
line: line)
|
||||
}
|
||||
|
||||
/// Log a message passing with the `Logger.Level.info` log level.
|
||||
///
|
||||
/// The `source` is the one supplied to the initializer of `LoggerWithSource`.
|
||||
///
|
||||
/// If `.info` is at least as severe as the `Logger`'s `logLevel`, it will be logged,
|
||||
/// otherwise nothing will happen.
|
||||
///
|
||||
/// - parameters:
|
||||
/// - message: The message to be logged. `message` can be used with any string interpolation literal.
|
||||
/// - metadata: One-off metadata to attach to this log message
|
||||
/// - file: The file this log message originates from (there's usually no need to pass it explicitly as it
|
||||
/// defaults to `#file`).
|
||||
/// - function: The function this log message originates from (there's usually no need to pass it explicitly as
|
||||
/// it defaults to `#function`).
|
||||
/// - line: The line this log message originates from (there's usually no need to pass it explicitly as it
|
||||
/// defaults to `#line`).
|
||||
@inlinable
|
||||
public func info(_ message: @autoclosure () -> Logger.Message,
|
||||
metadata: @autoclosure () -> Logger.Metadata? = nil,
|
||||
file: String = #file, function: String = #function, line: UInt = #line) {
|
||||
self.logger.info(message(),
|
||||
metadata: metadata(),
|
||||
source: self.source,
|
||||
file: file,
|
||||
function: function,
|
||||
line: line)
|
||||
}
|
||||
|
||||
/// Log a message passing with the `Logger.Level.notice` log level.
|
||||
///
|
||||
/// The `source` is the one supplied to the initializer of `LoggerWithSource`.
|
||||
///
|
||||
/// If `.notice` is at least as severe as the `Logger`'s `logLevel`, it will be logged,
|
||||
/// otherwise nothing will happen.
|
||||
///
|
||||
/// - parameters:
|
||||
/// - message: The message to be logged. `message` can be used with any string interpolation literal.
|
||||
/// - metadata: One-off metadata to attach to this log message
|
||||
/// - file: The file this log message originates from (there's usually no need to pass it explicitly as it
|
||||
/// defaults to `#file`).
|
||||
/// - function: The function this log message originates from (there's usually no need to pass it explicitly as
|
||||
/// it defaults to `#function`).
|
||||
/// - line: The line this log message originates from (there's usually no need to pass it explicitly as it
|
||||
/// defaults to `#line`).
|
||||
@inlinable
|
||||
public func notice(_ message: @autoclosure () -> Logger.Message,
|
||||
metadata: @autoclosure () -> Logger.Metadata? = nil,
|
||||
file: String = #file, function: String = #function, line: UInt = #line) {
|
||||
self.logger.notice(message(),
|
||||
metadata: metadata(),
|
||||
source: self.source,
|
||||
file: file,
|
||||
function: function,
|
||||
line: line)
|
||||
}
|
||||
|
||||
/// Log a message passing with the `Logger.Level.warning` log level.
|
||||
///
|
||||
/// The `source` is the one supplied to the initializer of `LoggerWithSource`.
|
||||
///
|
||||
/// If `.warning` is at least as severe as the `Logger`'s `logLevel`, it will be logged,
|
||||
/// otherwise nothing will happen.
|
||||
///
|
||||
/// - parameters:
|
||||
/// - message: The message to be logged. `message` can be used with any string interpolation literal.
|
||||
/// - metadata: One-off metadata to attach to this log message
|
||||
/// - file: The file this log message originates from (there's usually no need to pass it explicitly as it
|
||||
/// defaults to `#file`).
|
||||
/// - function: The function this log message originates from (there's usually no need to pass it explicitly as
|
||||
/// it defaults to `#function`).
|
||||
/// - line: The line this log message originates from (there's usually no need to pass it explicitly as it
|
||||
/// defaults to `#line`).
|
||||
@inlinable
|
||||
public func warning(_ message: @autoclosure () -> Logger.Message,
|
||||
metadata: @autoclosure () -> Logger.Metadata? = nil,
|
||||
file: String = #file, function: String = #function, line: UInt = #line) {
|
||||
self.logger.warning(message(),
|
||||
metadata: metadata(),
|
||||
source: self.source,
|
||||
file: file,
|
||||
function: function,
|
||||
line: line)
|
||||
}
|
||||
|
||||
/// Log a message passing with the `Logger.Level.error` log level.
|
||||
///
|
||||
/// The `source` is the one supplied to the initializer of `LoggerWithSource`.
|
||||
///
|
||||
/// If `.error` is at least as severe as the `Logger`'s `logLevel`, it will be logged,
|
||||
/// otherwise nothing will happen.
|
||||
///
|
||||
/// - parameters:
|
||||
/// - message: The message to be logged. `message` can be used with any string interpolation literal.
|
||||
/// - metadata: One-off metadata to attach to this log message
|
||||
/// - file: The file this log message originates from (there's usually no need to pass it explicitly as it
|
||||
/// defaults to `#file`).
|
||||
/// - function: The function this log message originates from (there's usually no need to pass it explicitly as
|
||||
/// it defaults to `#function`).
|
||||
/// - line: The line this log message originates from (there's usually no need to pass it explicitly as it
|
||||
/// defaults to `#line`).
|
||||
@inlinable
|
||||
public func error(_ message: @autoclosure () -> Logger.Message,
|
||||
metadata: @autoclosure () -> Logger.Metadata? = nil,
|
||||
file: String = #file, function: String = #function, line: UInt = #line) {
|
||||
self.logger.error(message(),
|
||||
metadata: metadata(),
|
||||
source: self.source,
|
||||
file: file,
|
||||
function: function,
|
||||
line: line)
|
||||
}
|
||||
|
||||
/// Log a message passing with the `Logger.Level.critical` log level.
|
||||
///
|
||||
/// The `source` is the one supplied to the initializer of `LoggerWithSource`.
|
||||
///
|
||||
/// `.critical` messages will always be logged.
|
||||
///
|
||||
/// - parameters:
|
||||
/// - message: The message to be logged. `message` can be used with any string interpolation literal.
|
||||
/// - metadata: One-off metadata to attach to this log message
|
||||
/// - file: The file this log message originates from (there's usually no need to pass it explicitly as it
|
||||
/// defaults to `#file`).
|
||||
/// - function: The function this log message originates from (there's usually no need to pass it explicitly as
|
||||
/// it defaults to `#function`).
|
||||
/// - line: The line this log message originates from (there's usually no need to pass it explicitly as it
|
||||
/// defaults to `#line`).
|
||||
@inlinable
|
||||
public func critical(_ message: @autoclosure () -> Logger.Message,
|
||||
metadata: @autoclosure () -> Logger.Metadata? = nil,
|
||||
file: String = #file, function: String = #function, line: UInt = #line) {
|
||||
self.logger.critical(message(),
|
||||
metadata: metadata(),
|
||||
source: self.source,
|
||||
file: file,
|
||||
function: function,
|
||||
line: line)
|
||||
}
|
||||
}
|
||||
@@ -27,8 +27,6 @@ import Glibc
|
||||
/// and the selected `LogHandler`). Therefore, `Logger`s are suitable to be passed around between libraries if you want
|
||||
/// to preserve metadata across libraries.
|
||||
///
|
||||
/// - seeAlso: `LoggerWithSource` if you'd like to attach a customized source to every log message.
|
||||
///
|
||||
/// The most basic usage of a `Logger` is
|
||||
///
|
||||
/// logger.info("Hello World!")
|
||||
@@ -410,10 +408,6 @@ extension Logger {
|
||||
public init(label: String, factory: (String) -> LogHandler) {
|
||||
self = Logger(label: label, factory(label))
|
||||
}
|
||||
|
||||
public func withSource(_ source: String) -> LoggerWithSource {
|
||||
return LoggerWithSource(self, source: source)
|
||||
}
|
||||
}
|
||||
|
||||
extension Logger.Level {
|
||||
@@ -569,7 +563,7 @@ public struct MultiplexLogHandler: LogHandler {
|
||||
function: String,
|
||||
line: UInt) {
|
||||
for handler in self.handlers where handler.logLevel <= level {
|
||||
handler.log(level: level, message: message, metadata: metadata, file: file, function: function, line: line)
|
||||
handler.log(level: level, message: message, metadata: metadata, source: source, file: file, function: function, line: line)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -25,7 +25,6 @@ import XCTest
|
||||
extension CompatibilityTest {
|
||||
static var allTests: [(String, (CompatibilityTest) -> () throws -> Void)] {
|
||||
return [
|
||||
("testAllLogLevelsWorkWithOldSchoolLogHandlerButSourceIsNotPropagated", testAllLogLevelsWorkWithOldSchoolLogHandlerButSourceIsNotPropagated),
|
||||
("testAllLogLevelsWorkWithOldSchoolLogHandlerWorks", testAllLogLevelsWorkWithOldSchoolLogHandlerWorks),
|
||||
]
|
||||
}
|
||||
|
||||
@@ -16,32 +16,6 @@
|
||||
import XCTest
|
||||
|
||||
final class CompatibilityTest: XCTestCase {
|
||||
func testAllLogLevelsWorkWithOldSchoolLogHandlerButSourceIsNotPropagated() {
|
||||
let testLogging = OldSchoolTestLogging()
|
||||
|
||||
var logger = LoggerWithSource(Logger(label: "\(#function)",
|
||||
factory: testLogging.make),
|
||||
source: "my-fancy-source")
|
||||
logger.logLevel = .trace
|
||||
|
||||
logger.trace("yes: trace")
|
||||
logger.debug("yes: debug")
|
||||
logger.info("yes: info")
|
||||
logger.notice("yes: notice")
|
||||
logger.warning("yes: warning")
|
||||
logger.error("yes: error")
|
||||
logger.critical("yes: critical")
|
||||
|
||||
// Please note that the source is _not_ propagated (because the backend doesn't support it).
|
||||
testLogging.history.assertExist(level: .trace, message: "yes: trace", source: "no source")
|
||||
testLogging.history.assertExist(level: .debug, message: "yes: debug", source: "no source")
|
||||
testLogging.history.assertExist(level: .info, message: "yes: info", source: "no source")
|
||||
testLogging.history.assertExist(level: .notice, message: "yes: notice", source: "no source")
|
||||
testLogging.history.assertExist(level: .warning, message: "yes: warning", source: "no source")
|
||||
testLogging.history.assertExist(level: .error, message: "yes: error", source: "no source")
|
||||
testLogging.history.assertExist(level: .critical, message: "yes: critical", source: "no source")
|
||||
}
|
||||
|
||||
func testAllLogLevelsWorkWithOldSchoolLogHandlerWorks() {
|
||||
let testLogging = OldSchoolTestLogging()
|
||||
|
||||
|
||||
@@ -50,8 +50,6 @@ extension LoggingTest {
|
||||
("testStreamLogHandlerOutputFormatWithMetaData", testStreamLogHandlerOutputFormatWithMetaData),
|
||||
("testStdioOutputStreamFlush", testStdioOutputStreamFlush),
|
||||
("testOverloadingError", testOverloadingError),
|
||||
("testAllLogLevelsWorkOnLoggerWithSource", testAllLogLevelsWorkOnLoggerWithSource),
|
||||
("testLoggerWithSource", testLoggerWithSource),
|
||||
]
|
||||
}
|
||||
}
|
||||
@@ -715,42 +715,6 @@ class LoggingTest: XCTestCase {
|
||||
|
||||
logging.history.assertExist(level: .error, message: "errorDescription")
|
||||
}
|
||||
|
||||
func testAllLogLevelsWorkOnLoggerWithSource() {
|
||||
let testLogging = TestLogging()
|
||||
LoggingSystem.bootstrapInternal(testLogging.make)
|
||||
|
||||
var logger = LoggerWithSource(Logger(label: "\(#function)"), source: "my-fancy-source")
|
||||
logger.logLevel = .trace
|
||||
|
||||
logger.trace("yes: trace")
|
||||
logger.debug("yes: debug")
|
||||
logger.info("yes: info")
|
||||
logger.notice("yes: notice")
|
||||
logger.warning("yes: warning")
|
||||
logger.error("yes: error")
|
||||
logger.critical("yes: critical")
|
||||
|
||||
testLogging.history.assertExist(level: .trace, message: "yes: trace", source: "my-fancy-source")
|
||||
testLogging.history.assertExist(level: .debug, message: "yes: debug", source: "my-fancy-source")
|
||||
testLogging.history.assertExist(level: .info, message: "yes: info", source: "my-fancy-source")
|
||||
testLogging.history.assertExist(level: .notice, message: "yes: notice", source: "my-fancy-source")
|
||||
testLogging.history.assertExist(level: .warning, message: "yes: warning", source: "my-fancy-source")
|
||||
testLogging.history.assertExist(level: .error, message: "yes: error", source: "my-fancy-source")
|
||||
testLogging.history.assertExist(level: .critical, message: "yes: critical", source: "my-fancy-source")
|
||||
}
|
||||
|
||||
func testLoggerWithSource() {
|
||||
let testLogging = TestLogging()
|
||||
LoggingSystem.bootstrapInternal(testLogging.make)
|
||||
|
||||
var logger = Logger(label: "\(#function)").withSource("source")
|
||||
logger.logLevel = .trace
|
||||
|
||||
logger.critical("yes: critical")
|
||||
|
||||
testLogging.history.assertExist(level: .critical, message: "yes: critical", source: "source")
|
||||
}
|
||||
}
|
||||
|
||||
extension Logger {
|
||||
|
||||
Reference in new issue
Block a user