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?
Crashlytics associates the logs with your crash data and displays them in the Crashlytics page of the Firebase console, under the Logs tab.
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! }
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.
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"]))
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With