Logging for Swift (iOS/macOS): Overview of the Best Solutions
Logging is one of the key components of developing quality applications for iOS and macOS. It helps track errors, analyze user behavior, and debug complex scenarios. In the Swift ecosystem, there are several proven tools, each with its own strengths. In this article, we will look at three best solutions: SwiftyBeaver, CocoaLumberjack, and OSLog (Apple's built-in solution).
Why can't you rely only on print()?
Many beginner developers use print() to output debugging information. However, this approach has significant drawbacks:
- Lack of logging levels (debug, info, error).
- Inability to flexibly filter messages.
- Console output slows down the application in production.
- No built-in support for remote log collection.
Professional tools solve these problems and give the developer full control over the process.
1. SwiftyBeaver — Flexible and Beautiful Logging
SwiftyBeaver is one of the most popular libraries for Swift. It provides a convenient API, support for colored console output, and the ability to send logs to cloud services (for example, to its own cloud dashboard or to third-party systems).
Installation
Add the dependency to Package.swift or use CocoaPods:
// Swift Package Managerdependencies: [ .package(url: "https://github.com/SwiftyBeaver/SwiftyBeaver.git", from: "2.0.0")]Usage Example
import SwiftyBeaver
let log = SwiftyBeaver.self
// Configure console output (with color)let console = ConsoleDestination()console.format = "$DHH:mm:ss$d $L $M"log.addDestination(console)
// Logginglog.verbose("This is a verbose message")log.debug("Debug")log.info("Info")log.warning("Warning")log.error("Error")SwiftyBeaver Features
- Support for 5 logging levels (verbose, debug, info, warning, error).
- Colored output in the Xcode console (very convenient for debugging).
- Ability to send logs to cloud storage (SwiftyBeaver Cloud).
- Filtering by level and tags.
- Minimal impact on performance.
2. CocoaLumberjack — The Industry Standard
CocoaLumberjack is a powerful framework used in large projects such as Slack, Twitter, and many others. It is focused on high performance and configuration flexibility.
Installation
// CocoaPodspod 'CocoaLumberjack/Swift'Usage Example
import CocoaLumberjack
// ConfigurationDDLog.add(DDOSLogger.sharedInstance) // Console output
// LoggingDDLogDebug("Debug message")DDLogInfo("Info")DDLogWarn("Warning")DDLogError("Error")CocoaLumberjack Advantages
- Highest performance (asynchronous writing).
- Support for multiple "destinations" (console, file, database).
- Flexible log formatting system.
- Ability to create custom loggers.
- Excellent integration with Objective-C projects.
3. OSLog — Apple's Native Solution
Starting with iOS 10 and macOS 10.12, Apple introduced OSLog — a built-in logging system that works at the kernel level. It is ideal for production builds and does not require installing third-party libraries.
Usage Example
import OSLog
let logger = Logger(subsystem: "com.myapp", category: "network")
logger.debug("Connection established") Also in library
Rust Application Configuration: The `config` Library for Flexible Settings
Working with XML in Swift (iOS/macOS): Foundation and SwiftXML
golangci-lint for Go: Complete Guide with Examples
regex for Rust: complete guide with examples