mirror of
https://github.com/apple/swift-log
synced 2026-11-24 03:47:49 +00:00
motivation: add tests to confirm desired functionality changes: * add property for logger metadata (aka context) * improve pretty-metadata (aka pretty-context) generation * add unit tests covering local and global use cases * add [test only] implementation of MDC * format with swiftformat open issues: * should we name the new property metadata or context * should we include a default MDC implmentation as part of the logging API. if we dont, we will encourage libraries binding to specific logging implementations that support it (that was bad in java) * tests are a bit brittle * why is ExampleLoggerImplementation a reference type?
68 lines
2.5 KiB
Swift
68 lines
2.5 KiB
Swift
import ExampleLoggerImpl
|
|
import Foundation
|
|
import ServerLoggerAPI
|
|
|
|
enum RandomExample {
|
|
static func main() {
|
|
// let's first start off with whatever is the default
|
|
print("=== default ===")
|
|
var logger = LoggerFactory.make(identifier: "com.example.TestApp.LoggerBeforeConfig")
|
|
logger.trace("trace, not logged")
|
|
logger.debug("debug, not logged")
|
|
logger.info("default, info, no context")
|
|
logger.warn("default, warn, no context")
|
|
logger.error("default, error, no context")
|
|
|
|
logger[diagnosticKey: "foo"] = "bar"
|
|
logger[diagnosticKey: "UUID"] = UUID().description
|
|
|
|
logger.trace("trace, not logged")
|
|
logger.debug("debug, not logged")
|
|
logger.info("default, info, with context")
|
|
logger.warn("default, warn, with context")
|
|
logger.error("default, error, with context")
|
|
|
|
print("=== reconfiguring the LoggerFactory's default to have a minimum level of warn with StdoutLogger ===")
|
|
LoggerFactory.factory = { identifier in
|
|
let logger = StdoutLogger(identifier: identifier)
|
|
logger.logLevel = .warn
|
|
return logger
|
|
}
|
|
logger = LoggerFactory.make(identifier: "com.example.TestApp.LoggerAfterConfig1")
|
|
|
|
logger.trace("trace, not logged")
|
|
logger.debug("debug, not logged")
|
|
logger.info("info, no context")
|
|
logger.warn("warn, no context")
|
|
logger.error("error, no context")
|
|
|
|
logger[diagnosticKey: "bar"] = "buz"
|
|
logger[diagnosticKey: "UUID"] = UUID().description
|
|
|
|
logger.trace("trace, not logged")
|
|
logger.debug("debug, not logged")
|
|
logger.info("info, with context")
|
|
logger.warn("warn, with context")
|
|
logger.error("error, with context")
|
|
|
|
print("=== installing a custom logger ===")
|
|
LoggerFactory.factory = ExampleLoggerImplementation.init
|
|
logger = LoggerFactory.make(identifier: "com.example.TestApp.LoggerAfterConfig2")
|
|
|
|
logger.trace("trace, not logged")
|
|
logger.debug("debug, not logged")
|
|
logger.info("info, no context")
|
|
logger.warn("warn, no context")
|
|
logger.error("error, no context")
|
|
|
|
logger[diagnosticKey: "buz"] = "cux"
|
|
logger[diagnosticKey: "UUID"] = UUID().description
|
|
|
|
logger.trace("trace, not logged")
|
|
logger.debug("debug, not logged")
|
|
logger.info("info, with context")
|
|
logger.warn("warn, with context")
|
|
logger.error("error, with context")
|
|
}
|
|
}
|