How to use Crashlytics logging in Swift?

Albert Bori picture Albert Bori · Jan 20, 2015 · Viewed 19.1k times · Source

This article describes how to use Crashlytics logging in objective-c. However, after going throught the installation steps for propertly referencing Crashlytics and Fabric into my project, I don't seem to have access to that method.

Looking at the Crashlytics.h file, I can see it defined using compiler flags:

#ifdef DEBUG
#define CLS_LOG(__FORMAT__, ...) CLSNSLog((@"%s line %d $ " __FORMAT__), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__)
#else
#define CLS_LOG(__FORMAT__, ...) CLSLog((@"%s line %d $ " __FORMAT__), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__)
#endif

This block just appears to wrap the CLSNLog and the CLSLog functions depending on the compiler flag.

So, thinking I'd just go straight to the source, I tried to reference CLSLog directly from a swift file. Still no luck:

My-Bridging-Header.h:

#import <Crashlytics/Crashlytics.h>

Log.swift:

import Foundation
import Fabric
import Crashlytics

func Log(message: String) {
    NSLog("%@", message)
    CLS_LOG("%@", message)
    CLSLog("%@", message)
}

The last two lines in the Log function throw the error, Use of unresolved identifier. Crashlytics crash reporting works just fine, except for the logging feature. According to this article, logging support for Swift has been implemented.

As far as versions go, I'm running the latest version of Fabric/Crashlytics (December release, at the time of this post).

(Interesting note, I can see/use CLSLogv()...)

Does anyone know the correct way to incorporate CLS_LOG for use in a Swift project?

Answer

Mike Bonnell picture Mike Bonnell · Jan 20, 2015

Mike from Crashlytics here.

To use custom logging in Swift, just use CLSLogv or CLSNSLogv. You need to make an array and then call getVaList function on that array.

Here's a snippet:

CLSLogv("Log something %d %d %@", getVaList([1, 2, "three"]))

For CLSNSLogv:

CLSNSLogv("hello %@", getVaList(["goodbye"]))