[go: up one dir, main page]

DEV Community

Wesley de Groot
Wesley de Groot

Posted on • Originally published at wesleydegroot.nl on

Loging using OSLog

OSLog is a Swift API that provides a unified logging system for all Apple platforms.

It is a replacement for the older print and NSLog functions.

OSLog is a more efficient and secure logging system that provides better performance and privacy protection.

In this article, we will explore the features of OSLog and how to use it in your Swift applications.

Installation

To use OSLog in your Swift application, you need to import the OSLog module.

You can do this by adding the following import statement to your Swift file:

import OSLog
Enter fullscreen mode Exit fullscreen mode

Then you can use the Logger class to create a logger instance.

The Logger class is a wrapper around the OSLog API that provides a more convenient way to log messages.

let logger = Logger(
    subsystem: "nl.wesleydegroot.demoApp",
    category: "myCategory"
)
Enter fullscreen mode Exit fullscreen mode

The subsystem parameter is a string that identifies the subsystem that the logger belongs to.

The category parameter is a string that identifies the category of the logger.

Sample Use

import OSLog

let logger = Logger(
    subsystem: "nl.wesleydegroot.demoApp",
    category: "myCategory"
)

logger.fault("This is a fault") // Shows in red
logger.error("This is a error") // Shows in yellow
logger.warning("This is a warning") // Shows in yellow
logger.info("Information") // Shows in default color
logger.debug("Debug message") // Shows in default color
logger.trace("Trace message") // Shows in default color
Enter fullscreen mode Exit fullscreen mode

Caveats

Unfortunately, OSLog is only available on Apple platforms and it cannot be used within SwiftUI views.

If you need to log messages within a SwiftUI view, use the view extension below:

import SwiftUI

extension View {
    func log(_ closure: () -> Void) -> some View {
         _ = closure()
         return self
     }
}
Enter fullscreen mode Exit fullscreen mode

Now you can log messages within a SwiftUI view like this:

import SwiftUI
import OSLog

struct ContentView: View {
    let logger = Logger(
        subsystem: "nl.wesleydegroot.exampleapp",
        category: "MyCategory"
    )

    var body: some View {
        Text("Hello, World!")
            .log {
                logger.info("Hello, World!")
            }
    }
}
Enter fullscreen mode Exit fullscreen mode

Displaying Log Messages (in app)

If you want to display log messages in your app, you can use the OSLog API to log messages to the console.

I've created a Swift Package to make this easier, you can find it here (OSLogViewer).

How to use OSLogViewer:

import SwiftUI
import OSLogViewer

struct ContentView: View {
    var body: some View {
        NavigationView {
            // The default configuration will show the log messages.
            OSLogViewer()

            // Custom configuration
            // OSLogViewer(
            // subsystem: "nl.wesleydegroot.exampleapp",
            // since: Date().addingTimeInterval(-7200) // 2 hours
            // )
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Download Swift Playground

OSLogViewer Features

Simple Interface

OSLog Viewer

Beautiful export

This is the OSLog archive for exampleapp
Generated on 2/6/2024, 11:53
Generator https://github.com/0xWDG/OSLogViewer

Info message
ℹī¸ 2/6/2024, 11:53 🏛ī¸ exampleapp ⚙ī¸ nl.wesleydegroot.exampleapp 🌐 myCategory

Error message
❗ 2/6/2024, 11:53 🏛ī¸ exampleapp ⚙ī¸ nl.wesleydegroot.exampleapp 🌐 myCategory

Error message
❗ 2/6/2024, 11:53 🏛ī¸ exampleapp ⚙ī¸ nl.wesleydegroot.exampleapp 🌐 myCategory

Critical message
â€ŧī¸ 2/6/2024, 11:53 🏛ī¸ exampleapp ⚙ī¸ nl.wesleydegroot.exampleapp 🌐 myCategory

Log message
🔔 2/6/2024, 11:53 🏛ī¸ exampleapp ⚙ī¸ nl.wesleydegroot.exampleapp 🌐 myCategory

Log message
🔔 2/6/2024, 11:53 🏛ī¸ exampleapp ⚙ī¸ nl.wesleydegroot.exampleapp 🌐 myCategory
Enter fullscreen mode Exit fullscreen mode

Wrap up

OSLog is a more efficient and secure logging system that provides better performance and privacy protection.

It is a great replacement for the older print and NSLog functions, that provides a unified logging system for all Apple platforms.

Resources:

Top comments (0)