Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Crashlytics logging in Swift?

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?

like image 704
Albert Bori Avatar asked Jan 20 '15 20:01

Albert Bori


People also ask

How do I access Crashlytics logs?

Crashlytics associates the logs with your crash data and displays them in the Crashlytics page of the Firebase console, under the Logs tab.

How do I log exception in Crashlytics?

You can also use Crashlytics below features to provide more information. Logging Non-Fatal Events: try { myMethodThatThrows(); } catch (Exception e) { Crashlytics. logException(e); // handle your exception here! }

How does Crashlytics work?

Crashlytics helps you to collect analytics and details about crashes and errors that occur in your app. It does this through three aspects: Logs: Log events in your app to be sent with the crash report for context if your app crashes. Crash reports: Every crash is automatically turned into a crash report and sent.


2 Answers

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"])) 
like image 163
Mike Bonnell Avatar answered Sep 24 '22 00:09

Mike Bonnell


Here is my version adapted from Dima's answer. I have no need of the arguments, since you can do all the formatting within the Swift string that you pass.

func DebugLog(_ message: String, file: StaticString = #file, function: StaticString = #function, line: Int = #line) {     let output: String     if let filename = URL(fileURLWithPath: file.description).lastPathComponent.components(separatedBy: ".").first {         output = "\(filename).\(function) line \(line) $ \(message)"     } else {         output = "\(file).\(function) line \(line) $ \(message)"     }      #if targetEnvironment(simulator)         NSLogv("%@", getVaList([output]))     #elseif DEBUG         CLSNSLogv("%@", getVaList([output]))     #else         CLSLogv("%@", getVaList([output]))     #endif } 

And you would use it like this:

DebugLog("this is a log message") DebugLog("this is a log message \(param1) \(param2)") 

EDIT: Updated to Swift 3.1

like image 32
ian Avatar answered Sep 22 '22 00:09

ian