Skip to content

elegantchaos/Logger

Repository files navigation

Logger

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.

Motivation

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.

Basic Usage

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")

Configuration

The list of enabled channels is persistent between runs of your program, and all channels start disabled by 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 completely reset the list of enabled channels:

.build/debug/Example -logs "=someChannel,someOtherChannel"

UI

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.

About

Configurable logging for Swift.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages