Awesome
LoggingSwiftyBeaver
A logging backend for SwiftLog that sends log messages to SwiftyBeaver.
Installation 📦
Add the LoggingSwiftyBeaver package as a dependency to your Package.swift
file.
.package(url: "https://github.com/ShivaHuang/swift-log-SwiftyBeaver.git", from: "0.1.0")
Add LoggingSwiftyBeaver to your target's dependencies.
.target(
name: "Example",
dependencies: [
.product(name: "LoggingSwiftyBeaver", package: "swift-log-SwiftyBeaver")
])
Usage 📝
1. Let's import the logging API package:
import Logging
import LoggingSwiftyBeaver
2. Create a logger
, the label works similarly to a DispatchQueue
label:
let logger = Logger(label: "Example") { (label) in
SwiftyBeaver.LogHandler(label, destinations: [
ConsoleDestination()
])
}
Alternatively, you can use SwiftyBeaver
only in DEBUG
build and don't print anything in RELEASE
build:
let logger: Logger = {
Logger(label: "Example") { (label) in
#if DEBUG
return SwiftyBeaver.LogHandler(label, destinations: [
ConsoleDestination()
])
#else
return Logging.SwiftLogNoOpLogHandler()
#endif
}
}()
Futher, you can custom format and set console output to short time, log level & message:
let logger: Logger = {
Logger(label: "Example") { (label) in
#if DEBUG
let console: ConsoleDestination = {
let destination = ConsoleDestination()
destination.format = "$DHH:mm:ss$d $L $M"
return destination
}()
return SwiftyBeaver.LogHandler(label, destinations: [
console
])
#else
return Logging.SwiftLogNoOpLogHandler()
#endif
}
}()
3. We're now ready to use it:
// logging an informational message
logger.info("Hello World!")
// ouch, something went wrong
logger.error("Houston, we have a problem: \(problem)")
Log Levels 💜💚💙💛❤️
It's a good habit to distinguish logs by levels. However, SwiftLog
defines 7 levels while SwiftyBeaver
has only 5. So an 1-to-1 mapping between SwiftLog
and SwiftyBeaver
is not possible. Following is a table for the mapping:
SwiftLog | SwiftyBeaver |
---|---|
trace | verbose |
debug | debug |
info | info |
notice | warning |
warning | warning |
error | error |
critical | error |
Origin 🗄
This program was developed by @shivahuang as part of Taiwan Social Distancing.