How to use CocoaLumberjack in Swift

Background

I have been struggling with this a bit and couldn’t find a documented solution online. So upon solving the problem I decided to write this blog post just to share my findings with the world and hopefully help someone else. Please note that I used prerelease versions of both CocoaPods and CocoaLumberjack.

Prerequisites

You need to have the release candidate of CocoaPods installed. I used version 0.36.0.rc.1. To install the prerelease version type the following command into the terminal:

You can check to see which version you have with the command:

Installing CocoaLumberjack

First we need to create a Podfile in the projects root folder. That is usually the same folder where your .xcodeproj file is.

In the Podfile add the following text:

Let’s break this down a bit. The first line tells us if we’re building an iOS or an OSX application and the system target version. In this case I’m targeting iOS 8.1.

The second line specifies that we want to install CocoaLumberjack and that we explicitly want the version 2.0.0-rc2. You can find all available releases on CocoaLumberjacks github (https://github.com/CocoaLumberjack/CocoaLumberjack/releases).

The last line tells CocoaPods that we want to use dynamic frameworks instead of static libraries.

After saving the Podfile to your project root, run this command inside your project root:

When it is done you can close your Xcode project and open the newly created .xcworkspace file located in your project root, right next to your .xcodeproj file. Now this will not work entirely as expected.

Finishing the job

We need to help it out a bit by adding CocoaLumberjack.swift to the project that wasn’t added. So go to your project root and navigate into CocoaLumberJackTest/Pods/CocoaLumberjack/Classes. There is a file called CocoaLumberjack.swift, drag that file into the Pods project, I suggest into the Pods/CocoaLumberjack/Default group. When XCode gives you options for adding the files make sure to leave Copy items if needed unchecked. Also uncheck it from the Pods target and check it to the Pods-CocoaLumberjack target.

xcode add file options

That’s it, it should work now!

Go ahead and import CocoaLumberjack into your own swift files and DDLog all you want.

Petar Mataic

Petar is an ambitious young developer who hungers for new technology. Because new technology often means that his work becomes easier and allow him to do things faster, better, cooler. Experienced in .NET, he is currently focused on the iOS platform. Born in a country that doesn't exist anymore, he aspires to make the whole world his home, sharing his passion with other software developers attending and speaking at international conferences.

This Post Has 5 Comments

  1. thanks for posting this! i tried this, but it isn’t working for me. since i’m also importing other libraries that are now swift-ready, so using the ‘use_frameworks!’ flag gives me all sorts of errors. is there a way to get this to work without the flag?

    1. As far as I know there is no other easy way. You could try to alter Cocoa Lumberjack to get it to work without dynamic frameworks. Though I would probably try to fix the issues with the other pods when using the ‘use_frameworks!’ flag instead. Also, please note that both Cocoa Lumberjack and Cocoapods have been released in stable releases supporting Swift since this blog post was written.

  2. This is really helpful. Thank you.

  3. is there a way to incorporate debugPrint and DDLog ?

Leave a Reply

Close Menu