Skip to main content

Crash handling

In Logly, LoggerCrashHandler.install() sets up two mechanisms: a handler for uncaught Objective-C exceptions and handlers for six fatal POSIX signals.

Setting up the LoggerCrashHandler​

Call install() of the LoggerCrashHandler once, as early as possible, after configuring Logly. The following example shows this in a SwiftUI app:

import SwiftUI
import Logly
import os

@main
struct MyApp: App {
init() {
let config = LoggerConfiguration.shared
config.logFilePath = Self.logFileURL()
config.asynchronousLogging = false // so the crash entry reliably reaches the file

LoggerCrashHandler.install()
}

var body: some Scene {
WindowGroup { ContentView() }
}

private static func logFileURL() -> URL {
let directory = FileManager.default
.urls(for: .applicationSupportDirectory, in: .userDomainMask)[0]
.appendingPathComponent("Logs")
try? FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true)
return directory.appendingPathComponent("app.log")
}
}

With UIKit you call LoggerCrashHandler.install() in the app delegate:

final class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
LoggerCrashHandler.install()
return true
}
}

Uncaught exceptions in the LoggerCrashHandler​

Logly registers a handler through NSSetUncaughtExceptionHandler. When an NSException is not caught, it writes a message at level .fault to the Crash category, for example:

2026-10-03T14:10:52Z - FAULT - Crash - Logly.swift:832 - 💥 Uncaught Exception: *** -[__NSArrayM objectAtIndex:]: index 5 beyond bounds [0 .. 2]
Stack trace:
0 CoreFoundation 0x0000000180a1b2c0 __exceptionPreprocess + 176
1 libobjc.A.dylib 0x00000001804f2d4c objc_exception_throw + 60
…

In Logly the crash message goes through normal processing: console, log file and sinks. File and line refer to the location in Logly's source, not to your code. The cause is in the stack trace.

Asynchronous logging and crashes

With asynchronousLogging = true the crash message is only put into the queue. If the process ends first, it is missing from the file. Set asynchronousLogging = false if you need crash messages in the file reliably.

Fatal signals in the LoggerCrashHandler​

Logly registers a handler for these signals. The following table lists them with their typical cause:

SignalTypical cause
SIGABRTabort(), failed assertions in C/Objective-C
SIGILLIllegal instruction
SIGSEGVAccess to invalid memory
SIGFPEArithmetic error, e.g. integer division by zero in C
SIGBUSBad memory access
SIGPIPEWriting to a closed pipe or socket

The Logly signal handler is deliberately minimal, because only a few system functions are safe inside a signal handler. It writes one line to standard error, resets the signal to its default action and raises it again. The line looks like this:

💥 Logly: caught fatal signal SIGSEGV — terminating (see crash report for details)

This signal line does not go to the log file or to the sinks. Details come from the system crash report (Xcode Organizer, device logs) or from a crash-reporting service such as Sentry.

What the LoggerCrashHandler does not capture​

The Logly LoggerCrashHandler does not capture these crashes:

  • Swift runtime errors such as fatalError(), force-unwrapping nil or an array index out of range are not NSExceptions. They end the process through a trap. On Apple silicon devices this results in SIGTRAP, and Logly does not handle that signal.
  • Termination by the system, e.g. because of memory pressure or a watchdog timeout, sends SIGKILL. No app can catch that.

Important notes on the LoggerCrashHandler​

When you use the Logly LoggerCrashHandler, keep this in mind:

  • Call it once. Every call sets the handlers again.
  • Existing handlers are replaced. install() overwrites an exception handler that is already set and the signal handlers for the six signals. They are not chained.
  • Do not combine with Sentry. When LoglySentry activates Sentry, Sentry handles crashes. Do not call install() in that case.
  • SIGPIPE and network code. If your app deliberately ignores SIGPIPE (signal(SIGPIPE, SIG_IGN)), set that after install(). Otherwise a closed socket terminates the app.
  • Debugger. When Xcode is attached, the debugger usually stops on signals first.

Triggering a crash on purpose to test the LoggerCrashHandler​

This function raises an exception so you can test the Logly LoggerCrashHandler. Only run it in a debug build configuration and without a debugger attached:

#if DEBUG
func triggerTestException() {
NSException(name: .genericException, reason: "Logly test", userInfo: nil).raise()
}
#endif

After restarting the app, the FAULT message is in the log file if asynchronousLogging = false was set.