Sending log files to Sentry

Hello. I’m using the Sentry-cocoa sdk. I already integrated crash reporting and it works great. I would like to have application logs attached to crash reports.
I’m using Cocoalumberjack for logging, and it was very easy to integrate that with Crashlytics.

I found the Native SDK has a sentry_options_add_attachment method for that, but I can’t understand if the cocoa sdk gives access to the same functionality.

Sentry events include breadcrumbs. Which are structured messages with timestamps, similar to a log.

In the link you shared, similarly you could pipe those logs to Sentry’s SDK like:

class SentryLogger: DDAbstractLogger {

    override init() {
        super.init()
        self.logFormatter = DDLogFileFormatterDefault()
    }

    override func log(message: DDLogMessage) {
        guard let formatter = self.value(forKey: "_logFormatter") as? DDLogFormatter,
            let logText = formatter.format(message: message) else { return }

        // TODO: convert message.level to SentryLevel, category
        let crumb = Breadcrumb(level: SentryLevel.info, category: "category")
        crumb.message = getVaList([logText])
        // crumb.type = message.type
        SentrySDK.addBreadcrumb(crumb: crumb)
    }
}

By default Sentry keeps the last 100 breadcrumbs in memory. You can increase the number of crumbs when initializing the SDK (through maxBreadcrumbs).

With regards to attachments, Sentry Cocoa SDK doesn’t support it yet but it’s something we plan to add support to.

Thank you Bruno. I didn’t think about integrating log messages in breadcrumbs.

1 Like

I wrote an integration (weekend project/community maintenance), if you’d like to give it a try:

Thank you Bruno, I will use it for sure. I’m wondering how resource intensive is logging via breadcrumbs. Do you think I can direct all my app’s log to sentry this way (and of course have only the latest entries before a crash in Sentry web pages) without taxing the devices too much?

This depends on many factors. The main one is how much logging your generate and how do you use levels.

Breadcrumbs is just a ring buffer in the app. It’s bound to 100 items (configurable) so it throws away the oldest item to make space for new ones. There’s no I/O in the process of storing these, it shouldn’t have any noticeable performance impact but I suggest you measure things with a profiler to understand the impact in your app.

By default that integration doesn’t do anything with log entries with level lower than info. With info and warning messages it just stores in memory as I described above. And error entries will generate an event which means storing an event file to disk (to make sure it’s not lost if the app crashes) which is sent to Sentry on the background.

If there’s call to the logger with level error, the logging integration doesn’t send any events but the breadcrumbs it collected are included in case the app crashes which is already super useful.