A LoggerFactory
defines all the categies within one particular subsystem and makes it easy to create consistent Logger
s within your codebase.
Most of us simply use print()
, but there are a variety of problems with printing including:
- It's easy to accidentally leak private user data.
print()
gives you no context for what calledprint()
or where it was called.
To address these issues and others Apple provides the Unified Logging System which can be used through the Logger type.
- separates "print" calls into subsystems
- separates subsystems into categories
- automatically redacts sensitive user data
- It's just easier to
print()
... until now.
Now it's easy to define reusable, consistent, organized Logger
s which can be used throughout your codebase.
import Foundation
struct MyAppLogger: LoggerFactory {
static let subsystem = Bundle.main.bundleIdentifier ?? "MyApp"
typealias Categories = MyCategories
enum MyCategories: String, StringRawRepresentable {
case settings, networking, appLifeTime
}
}
MyAppLogger.logger(.appLifeTime).info("App is now active.")
Or you can reuse loggers.
let logger = MyAppLogger.logger(.appLifeTime)
logger.info("App is now active.")
// somewhere else...
logger.info("App will soon be inactive.")
The library also includes convenience methods for logging Swift Error
s.
do {
try throwingFunction()
} catch {
logger.error(error)
logger.error(error) { e in
"throwingFunction threw Error: \(e.localizedDescription)"
}
}
The library is released under the MIT License.