Logging in Swift (iOS/macOS): SwiftyBeaver, XCGLogger, OSLog

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Logging for Swift (iOS/macOS): Best Solutions



Logging is an integral part of any application development. For Swift (iOS/macOS), there are many tools available to effectively track errors, debug code, and analyze application behavior. In this article, we will review the best libraries and built-in solutions for logging, their features, advantages, and usage examples.



Why is logging needed in Swift?

Logging helps developers:

  • Track code execution in real time.
  • Identify and fix errors at early stages.
  • Analyze application performance.
  • Collect data for subsequent auditing.


Built-in Solution: OSLog (Unified Logging)

Starting with iOS 10 and macOS Sierra, Apple introduced OSLog — a powerful logging system built into the operating systems. It provides high performance and security.



Example of using OSLog

import os.log

let log = OSLog(subsystem: "com.example.app", category: "network")os_log("Request sent: %@", log: log, type: .info, "https://api.example.com")

// For errorsos_log("Connection error: %@", log: log, type: .error, error.localizedDescription)


Advantages of OSLog:

  • No need to install third-party libraries.
  • Works at the system level, supports filtering in Console.app.
  • Automatically encrypts data in production mode.


SwiftyBeaver — Cross-platform Logger

SwiftyBeaver is one of the most popular logging libraries for Swift. It supports iOS, macOS, tvOS, and watchOS. Key features: colored console output, file logging, sending to cloud services (e.g., Loggly, Elasticsearch).



Installation via Swift Package Manager

// In Package.swiftdependencies: [    .package(url: "https://github.com/SwiftyBeaver/SwiftyBeaver.git", from: "2.0.0")]


Example of using SwiftyBeaver

import SwiftyBeaver

let log = SwiftyBeaver.self

// Configuring destinationslet console = ConsoleDestination() // console outputlet file = FileDestination() // file logging

// Formattingconsole.format = "$DHH:mm:ss$d $L $N.$F:$l - $M"

// Adding destinationslog.addDestination(console)log.addDestination(file)

// Logginglog.info("Application started")log.error("Data loading error: \\(error.localizedDescription)")


Features of SwiftyBeaver:

  • Support for color-coded levels (verbose, debug, info, warning, error).
  • Ability to send logs to a server via HTTP.
  • Flexible output format configuration.


XCGLogger — Lightweight Logger for iOS/macOS

XCGLogger is another popular solution focused on simplicity and speed. It is ideal for small projects and rapid prototyping.



Example of using XCGLogger

import XCGLogger

let log = XCGLogger.default

// Configurationlog.setup(level: .debug, showLogIdentifier: false, showFunctionName: true, showThreadName: true, showLevel: true, showFileNames: true, showLineNumbers: true, showDate: true)

// Logginglog.debug("Debug: value x = \\(x)")log.warning("Warning: low battery level")


Advantages of XCGLogger:

  • Minimal dependencies.
  • Support for filtering by logging levels.
  • Integration with Crashlytics and other services.


Comparison of Logging Libraries

Recommendations

Library Performance