Configurable logging for Swift.
Declare multiple log channels. Send log messages and objects to them. Enable individual channels at runtime, with minimal overhead for the disabled ones.
When debugging complex problems, it's often useful to be able to write extensive logging code.
For this to scale to a large application with many subsystems, you need to be able to separate log output into functional areas.
It's healthy to be able to leave this code in place, but enable it only when needed.
That way you can keep the console output clean, and just turn on the bits you're interested in, when you need them - eg when debugging a regression in a particlar subsystem.
For maximum flexibility, it useful to be able to do this at runtime, sometimes even in release versions, without a negative performance impact from disabled logging code.
It's also useful to be able to log to different backends and destinations, such as the console, disk, or the network.
Just import the module and make one or more channels.
import Logger
let logger = Channel("Log")
let detailLogger = Channel("Details")To log output, just write it to a channel. Different kinds or levels of info can go to different channels as required:
logger.log("Hello world!")
detailLogger.log("We just logged hello world in the main channel")To log for debug builds only:
logger.debug("This will never show up in a release build")Channels start disabled unless they are declared with default: .enabled:
let startup = Channel("Startup", default: .enabled)Changes to a channel's state are saved between runs of your program. Only channels that differ from their default are saved, so a channel that is back at its default follows any later change to that default.
You can enable specific channels from the command line when you run:
.build/debug/Example -logs "+myChannel,+anotherChannel"
You can also disable channels:
.build/debug/Example -logs "-myChannel,-anotherChannel"
Or return every channel to its default and then enable the named ones:
.build/debug/Example -logs "=someChannel,someOtherChannel"
Channels can be named by their full identifier (subsystem.name) or their short name. Changes made on the command line are saved.
You can import LoggerUI to get a simple UI for configuring channels at runtime.
This allows you to view the list of channels, and enable or disable them.
LoggerUI provides a LoggerChannelsView section for use inside a List or Form. The host owns a ChannelWatcher, usually in @State, and passes it to the section:
@State private var watcher = ChannelWatcher()
var body: some View {
List {
LoggerChannelsView(watcher: watcher)
}
}AllChannelsToggleView and IndividualChannelsToggleView can also be composed into a menu; share one watcher between the surfaces so they stay in step. The SwiftUI example uses them in a macOS Debug → Channels menu. The master toggle shows a mixed state when only some channels are enabled, and each choice enables or disables every registered channel in one change. ResetChannelsButton returns the channels to their defaults; LoggerChannelsView includes one. Channel names include their subsystem when needed to distinguish otherwise identical labels.
By default the controls change the channels directly. An app that routes changes through its own action layer, such as commands with undo, gives the watcher a ChannelActions implementation; every control then calls it instead. Logger does not depend on any command framework.
Manager.setEnabled(_:), Manager.setAllEnabled(_:) and Manager.resetToDefaults() apply the changes. Each returns the previous states of only the channels that changed, which restore them exactly when passed back to setEnabled(_:), so a command can use the result for undo:
@MainActor
final class CommandChannelActions<C: LoggingCommands>: ChannelActions {
let centre: C
init(centre: C) {
self.centre = centre
}
func setEnabled(_ isEnabled: Bool, for id: Channel.ID) {
centre.performWithoutWaiting(SetChannelStates(states: [id: isEnabled]))
}
func setAllEnabled(_ isEnabled: Bool) {
centre.performWithoutWaiting(SetAllChannels(isEnabled: isEnabled))
}
func resetToDefaults() {
centre.performWithoutWaiting(ResetChannels())
}
}
let watcher = ChannelWatcher(actions: CommandChannelActions(centre: centre))The controls show a change once the manager reports it.