Very short – if not the shortest – Swift logging library.

I'm just a lone dude making risky-dink iOS apps. I do not want or need a fancy logging library. Here is what I do not need:

  • Colors.
  • Multiple channels.
  • Logging anywhere else other than standard output.
  • File rotation.
  • Complicated filters.
  • Helper functions that save me from typing 10 extra characters.
  • Verbosity.
  • Levels.
  • Formatters.
  • Thread safety.
  • Appenders.
  • XML anything.
  • Document Object Model hierarchies.
  • Targets.
  • Layouts.

I don't even need date and time information. That line prefix that NSLog kicks out? Too long! You know what I like column 1 of my log files to contain? The first character of my log message.

So just use print(), right? Well, the problem is, I throw all those print statements around, but I'm only debugging 1 or 2 entities at a time, and I don't want to look at all those print statements from 2 weeks ago confusing me while I debug something now.

So the only feature I need is categories. I specify a category when I write the call to my logging function along with a message. Somewhere central, I can turn on or off what categories get logged.

So my requirements are:
  • Print the message I pass in.
  • Unless I don't want that message printed right now.

So anyway, here is my fancy logging library.
struct L {
    // These are the only categories that will be logged. Adjust accordingly.
    private static let categories = ["Pilot"]
    typealias M = ((String) -> Void)
    static let c:[String : M] = {
        var tmp = [String : M]()
        for s in L.categories {
            tmp[s] = L.m
        return tmp
    static func m(message:String) {

And here is how you use it.

class Pilot {
    private let n = String(Pilot)
    L.c[n]?("radians = \(radians)")
    L.c["Err"]?("state was never set!")

class Location {
    private let c = String(Location)
    L.c[c]?("directionToLocation = \(directionToLocation)")

Because categories only has "Pilot", only the "radians" log line will get printed. I can chose to use the names of classes for the category or any string I want.

When I'm ready to push to the App Store, I can leave categories as "Err" to record any errors. As if I'll ever be able to talk a user through pulling and sending me their device console logs.

No comments:

Post a Comment