Latest 0.3
Homepage https://github.com/SebastianMecklenburg/TagLog
License MIT
Platforms ios 8.0
Frameworks Foundation
Authors

A small logging framework that can filter messages via tags.

Rationale and motivation

When collaborating on a project, different programmers usually work on different tasks. If everyone is logging their debug messages to the console things get confusing very quickly. By adding tags to log messages this problem can be avoided. You can tag logs with "Login" when you work on the login feature and then you’re not distracted by your colleagues logs that are related to some view layout, for example.

The logger also sports log levels that allows to further fine tune the messages that are shown.

By default the logger logs everything, just like Swift’s print() method. To filter the messages some configuration needs to be done, probably reasonably when the app launches. Check out the setupLogger() method in the included sample app to see how.

Basic Usage

Log.trace("Entering example code")
Log.info("Some message")

Log.tag = "SomeTag"
Log.info("Some message that is not logged because the tag is missing")
Log.info("Some tagged message", tag: "SomeTag")
Log.info("Some tagged message that is not logged because the tag is wrong", tag: "SomeOtherTag")

Log.critical("Some important message that is logged no matter what")

Log.warn("Changing log level", tag: "SomeOtherTag")
Log.level = .Debug
Log.info("Some message that is logged because its log level is high enough", tag: "SomeTag")
Log.trace("Leaving example code, not logged because the log level is too low.", tag: "SomeTag")

Please note again: In a real world application I think the best practice would probably be to configure the logger at app start and then leave it that way. Changing the logger along the way would only lead to confusion and doesn’t really have any apparent benefit. See the included sample app.

Documentation

Zero config

The functionality is provided through the ‘Log’ struct. All its methods are static so you can start logging withou having to create or configure any objects, just by writing.

Log.info("Some message")

and the logger will print all messages to the console, just like Swift’s built in print() method.

Tags

You can add tags to log messages and then filter them by these tags. So you would tag a message with

Log.info("Some message", tag: "Some tag")

and then configure the logger to show only messages with this tag like this

Log.tag = "Some tag"

and the logger will only print the logs tagged with "Some tag".

You can also set multiple tags, for example

Log.tags = ["Some tag", "Some other tag", "MyTag"]

will print all logs with any of those tags.

Levels

The logger provides the following log levels, in order of urgency:

Level Purpose
All Print everything
Trace For very verbose messages, like "Entering someMethod" and "Leaving some method".
Debug For debug info, like "Value: (someVariable)"
Info For application events, like "App started" or "Login successful!"
Warning For application events that are may happen, but probably shouldn’t, like "Login failed!"
Error For application events that really shouldn’t happen but that don’t necessarily cause the app to malfunction, like an object being in an inconsistent state.
Critical For application events that surely will cause the app to crash or malfunction, like trying to present some user data but there is no user registered.
Off All output disabled.

TagLog provides the following logging methods:

Method Purpose
Log.trace() Prints a message with the ‘Trace’ log level
Log.debug() Prints a message with the ‘Debug’ log level
Log.info() Prints a message with the ‘Trace’ log level
Log.warn() Prints a message with the ‘Warning’ log level
Log.error() Prints a message with the ‘Error’ log level
Log.critical() Prints a message with the ‘Critical’ log level

The log class has the property ‘level’ where you can set the desired log level. It will then show all messages with that level and higher urgency.
If you want to see only warnings, errors and critical messaged you’ write:

Log.level = .Warning
Log.trace("Not printed")
Log.error("Printed")

To immediately see which message is of which urgency you can activate log level icons, like

Log.printLevelSymbol = true

then the output will look like this:

Log level screenshot

Time stamp

By default the logger adds a millisecond precision timestamp to the messages. You can disable it with

Log.printTimestamp = false
Location

By default the logger adds the location (file, line, function) of the log to the messages. You can disable it with

Log.printLocation = false
Printer

The ‘Printer’ is the class that does tha actual output. Currently there are two printers included, a console printer (the default) and a file printer. To activate printing to a file you write

Log.printer = LogFilePrinter()

and to access the log from the file you write

if let printer = Log.printer as? LogFilePrinter {
    let debugLog = printer.fullLog
}

In the example app you can show the contents of the log file in a text view. This can be useful if you want to see the debug output on a device that is not connected to Xcode or to a network.

Deactivate logging

With setting the log level ‘Off’ the logging can be disabled on the fly. However, the code will still be there, bloat the app and munch away precious processor cycles. To keep the logging related methods from being compiled into the app altogether you have to define the ‘DISABLE_TAGLOG’ compiler flag in the build settings, like so:

Compiler flag screenshot

Latest podspec

{
    "name": "TagLog",
    "version": "0.3",
    "summary": "A logging framework in Swift.",
    "description": "A small logging framework that can filter messages via tags.",
    "homepage": "https://github.com/SebastianMecklenburg/TagLog",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "Sebastian Mecklenburg": "mecklenburg@rechenmaschinist.de"
    },
    "source": {
        "git": "https://github.com/SebastianMecklenburg/TagLog.git",
        "tag": "0.3"
    },
    "platforms": {
        "ios": "8.0"
    },
    "source_files": "TagLog",
    "frameworks": "Foundation"
}

Pin It on Pinterest

Share This