A Swift-based API for reading from & writing to the Apple System Log (more commonly known somewhat inaccurately as "the console")

Related tags

Logging CleanroomASL
Overview

HBC Digital logo      Gilt Tech logo

CleanroomASL

Deprecation notice

Notice: CleanroomASL is no longer supported

The Apple System Log facility has been deprecated by Apple.

As a result, we've deprecated CleanroomASL as well. Beyond critical maintenance fixes, we will no longer be actively developing the CleanroomASL project.


CleanroomASL is an iOS framework providing a Swift-based API for writing to and reading from the Apple System Log (ASL) facility.

CleanroomASL is designed as a thin wrapper around ASL’s native C API that makes use of Swift concepts where appropriate to make coding easier-to-understand and less error-prone.

CleanroomASL is part of the Cleanroom Project from Gilt Tech.

Swift compatibility

This is the master branch. It uses Swift 3.1 and requires Xcode 8.3 to compile.

Current status

Branch Build status
master Build status: master branch

Who It’s For

If you need to read from your application’s log on the device, CleanroomASL is for you. CleanroomASL is also useful if you need low-level access to writing to the Apple System Log.

Who It’s Not For

Because CleanroomASL is a low-level API, it may be cumbersome to use for common logging tasks.

If you’d like to use a simple, high-level Swift API for logging within your iOS application, consider using the CleanroomLogger project.

CleanroomLogger uses CleanroomASL under the hood, but provides a simpler API that will be more familiar to developers who’ve used other logging systems such as CocoaLumberjack or log4j.

CleanroomLogger is also extensible, allowing you to multiplex log output to multiple destinations and to add your own logger implementations.

License

CleanroomASL is distributed under the MIT license.

CleanroomASL is provided for your use—free-of-charge—on an as-is basis. We make no guarantees, promises or apologies. Caveat developer.

Adding CleanroomASL to your project

Carthage compatible

The simplest way to integrate CleanroomASL is with the Carthage dependency manager.

First, add this line to your Cartfile:

github "gilt/CleanroomASL" ~> 2.1.0

Then, use the carthage command to update your dependencies.

Finally, you’ll need to integrate CleanroomASL into your project in order to use the API it provides.

Once successfully integrated, just add the following statement to any Swift file where you want to use CleanroomASL:

import CleanroomASL

See the Integration document for additional details on integrating CleanroomASL into your project.

Using CleanroomASL

The CleanroomASL framework provides a simple mechanism for writing to the Apple System Log, and it can also be used to query the contents of the Apple System Log in order to find messages matching specific criteria.

The ASLClient class

The ASLClient class provides a log() function for writing to the Apple System Log, and also provides a search() function for reading.

Each ASLClient instance represents a connection to the ASL daemon. If you're only writing to ASL, a single ASLClient instance per application is sufficient for most uses, but if you find cases where you need to use multiple instances for writing, that will work as well.

Because the underlying ASL connections are not inherently thread-safe, the ASLClient class maintains its own Grand Central Dispatch queue which it uses to serialize use of the connection. This enforces a reliable ordering of log writes when using a single ASLClient and also ensures safe access to the shared client resource.

Note: Because of this design, each ASLClient instance may be used safely from any thread without any additional work on your part.

If your application is going to be writing to ASL and querying it, you may want to use a separate ASLClient instance for each individual search session to ensure that multiple concurrent searches do not slow down log writing or each other in the GCD queue.

By default, logging is performed asynchronously, which also provides performance benefits; on device, writing to ASL and mirroring to stderr can be expensive. Using NSLog() indiscriminately from the main thread can cause a noticeable performance degradation for UI operations such as scrolling and refreshing the display. ASLClient avoids this and allows your scrolling to be buttery smooth—and if scrolling isn't buttery smooth, at least you'll know it's not the fault of your logging code!

Writing to the Apple System Log

To write to the Apple System Log, construct an ASLMessageObject and pass it to the log() function of an ASLClient instance:

let client = ASLClient()
let message = ASLMessageObject(priorityLevel: .Notice, message: "This is my message. There are many like it, but this one is mine.")
client.log(message)

In the example above, the text "This is my message. There are many like it, but this one is mine." will be written asynchronously to the Apple System Log at the .Notice priority level.

Querying the Apple System Log

The ASLQueryObject class is used to perform search queries of the Apple System Log. Using the setQueryKey() function, you can specify search criteria for the messages you want to find:

let query = ASLQueryObject()
query.setQueryKey(.Message, value: nil, operation: .KeyExists, modifiers: .None)
query.setQueryKey(.Level, value: ASLPriorityLevel.Warning.priorityString, operation: .LessThanOrEqualTo, modifiers: .None)
query.setQueryKey(.Time, value: Int(NSDate().timeIntervalSince1970 - (60 * 5)), operation: .GreaterThanOrEqualTo, modifiers: .None)

The code above creates a search query that will find all log entries with a minimum priority level of .Warning recorded in the last 5 minutes that also have a value for the .Message attribute key.

Note: The sort order of ASL priority levels is counter-intuitive; the highest priority level (.Emergency) has the lowest numeric value (0) whereas the lowest priority level (.Debug) has the highest numeric value (7). Because CleanroomASL aims to be as thin a wrapper around ASL as possible, we do not change this behavior. That's why the .LessThanOrEqualTo operation is used to find messages with an ASLPriorityLevel of .Warning and higher.

To start the search, pass the query object to the client's search() function and provide a callback that will be executed once for each log entry matching the search criteria specified by query:

client.search(query) { record in
    if let record = record {
      // we have a search query result record; process it here
    } else {
      // there are no more records to process; no further callbacks will be issued
    }
    return true   // returning true to indicate we want more results if available
}

The second parameter to the search() function is of type ASLQueryObject.ResultCallback, a closure having the signature (ResultRecord?) -> Bool. The callback is passed a non-nil ASLQueryObject.ResultRecord instance for each record matching the search criteria, and when no more results are available, nil is passed.

Using its return value, the callback can control whether subsequent records are reported by the search operation. As long as the callback is willing to accept further results, it should return true. When the callback no longer wishes to process results, it should return false.

Once nil is passed to the callback or the callback returns false, the callback will not be executed again for the given search operation.

API documentation

For detailed information on using CleanroomASL, API documentation is available.

About the Apple System Log

ASL may be most familiar to Mac and iOS developers as the subsystem that underlies the NSLog() function.

When running within Xcode, NSLog() output shows up in the Console view.

On the Mac, messages written to ASL are available in the Console application.

For these reasons, people sometimes think of ASL as “the console,” even though that’s a bit of a misnomer:

  • Xcode’s Console view shows the stdout and stderr streams of the running process. Because NSLog() uses ASL configured in such a way that log messages are echoed to stderr, those messages show up in Xcode’s Console view. But the Console view can also show messages that weren’t sent through ASL.

  • The Console application on the Mac can be thought of as a viewer for ASL log messages, but it only shows a subset of the information that can be sent along with an ASL message. Further, Console is not limited to ASL; it can also be used to follow the content of standard text log files.

Differences between the device and simulator

On iOS, the Apple System Log behaves differently depending on whether it is running on a device or in the iOS Simulator.

Behavior Simulator Device
Visibility By default, log entries are visible to root and to the UID of the process that recorded them By default, log entries are visible only to root
Searching Searches can return log entries recorded by any process Searches will only return log entries recorded by the calling process

On the device, in order for an ASL log entry to be visible to the process that recorded it, the .ReadUID attribute of the ASLMessageObject must be explicitly set to -1. Otherwise, the log entry will be visible only to root, and if the process is trying to search for its own log entries, they won't be returned.

To avoid this causing confusion, the CleanroomASL framework automatically sets a message's .ReadUID attribute to -1 if no value is explicitly specified.

If you do in fact want your messages visible only to root, you can ensure that your log entries are recorded as you intend by specifying a .ReadUID attribute value of 0. This will prevent CleanroomASL from automatically setting that attribute value to -1.

Learning more about ASL

Apple’s native API for ASL is written in C. The definitive documentation for ASL can be found in the manpage that can be accessed using the man 3 asl Terminal command.

Peter Hosey’s Idle Time blog also has a number of informative posts on ASL helpful to anyone who wants to understand how it works.

About

The Cleanroom Project began as an experiment to re-imagine Gilt’s iOS codebase in a legacy-free, Swift-based incarnation.

Since then, we’ve expanded the Cleanroom Project to include multi-platform support. Much of our codebase now supports tvOS in addition to iOS, and our lower-level code is usable on macOS and watchOS as well.

Cleanroom Project code serves as the foundation of Gilt on TV, our tvOS app featured by Apple during the launch of the new Apple TV. And as time goes on, we'll be replacing more and more of our existing Objective-C codebase with Cleanroom implementations.

In the meantime, we’ll be tracking the latest releases of Swift & Xcode, and open-sourcing major portions of our codebase along the way.

Contributing

CleanroomASL is in active development, and we welcome your contributions.

If you’d like to contribute to this or any other Cleanroom Project repo, please read the contribution guidelines.

Acknowledgements

API documentation is generated using Realm’s jazzy project, maintained by JP Simard and Samuel E. Giddins.

You might also like...
A logging backend for swift-log that sends logging messages to Logstash (eg. the ELK stack)
A logging backend for swift-log that sends logging messages to Logstash (eg. the ELK stack)

LoggingELK LoggingELK is a logging backend library for Apple's swift-log The LoggingELK library provides a logging backend for Apple's apple/swift-log

🍯 Awesome log aggregator for iOS
🍯 Awesome log aggregator for iOS

🍯 Awesome log aggregator for iOS

Puree is a log collector which provides some features like below
Puree is a log collector which provides some features like below

Puree Description Puree is a log collector which provides some features like below Filtering: Enable to interrupt process before sending log. You can

Log every incoming notification to view them again later, also includes attachments and advanced settings to configure

Vē Natively integrated notification logger Installation Add this repository to your package manager

Automate box any value! Print log without any format control symbol! Change debug habit thoroughly!

LxDBAnything Automate box any value! Print log without any format control symbol! Change debug habit thoroughly! Installation You only need drag LxD

A log should tell a story, not drown the reader in irrelevance.

StoryTeller A log should tell a story, not drown the reader in irrelevance Story Teller is an advanced logging framework that takes an entirely differ

Log messages to text files and share them by email or other way.
Log messages to text files and share them by email or other way.

LogToFiles How to log messages to text files and share them by email or share center. 1 - Add the Log.swift file to your App 2 - Just log the messages

Simply, Logify provides instant colorful logs to improve log tracking and bug tracing
Simply, Logify provides instant colorful logs to improve log tracking and bug tracing

Logify Simply, Logify provides instant colorful logs to improve log tracking and bug tracing. Why I need to use Logify? As discussed before in a lot o

100 Days of SwiftUI log

100DaysOfSwiftUI 100 Days of SwiftUI log Table of Contents Day 1: variables, constants, strings, and numbers Day 1 Variables store values and can be c

Releases(2.1.0)
Owner
Gilt Tech
Gilt Tech
Simple Design for Swift bridge with Javascript. Also can get javascript console.log.

SDBridgeOC is here. If your h5 partner confused about how to deal with iOS and Android. This Demo maybe help. YouTube video is here. bilibili Video is

null 20 Dec 28, 2022
📱💬🚦 TinyConsole is a micro-console that can help you log and display information inside an iOS application, where having a connection to a development computer is not possible.

TinyConsole TinyConsole is a tiny log console to display information while using your iOS app and written in Swift. Usage Wrap your Main ViewControlle

Devran Cosmo Uenal 2k Jan 3, 2023
Gedatsu provide readable format about AutoLayout error console log

Gedatsu Gedatsu provide readable format about AutoLayout error console log Abstract At runtime Gedatsu hooks console log and formats it to human reada

bannzai 520 Jan 6, 2023
Customizable Console UI overlay with debug log on top of your iOS App

AEConsole Customizable Console UI overlay with debug log on top of your iOS App AEConsole is built on top of AELog, so you should probably see that fi

Marko Tadić 142 Dec 21, 2022
Gedatsu provide readable format about AutoLayout error console log

Gedatsu Gedatsu provide readable format about AutoLayout error console log Abstract At runtime Gedatsu hooks console log and formats it to human reada

bannzai 475 Jun 24, 2021
Delightful console output for Swift developers.

Rainbow adds text color, background color and style for console and command line output in Swift. It is born for cross-platform software logging in te

Wei Wang 1.7k Dec 31, 2022
XCLog is a Swift extension that helps you print something in console when debugging your projects.

XCLog XCLog is a Swift extension that helps you print something in console when debugging your projects. Installation Open Xcode > File > Add Packages

null 1 Jan 9, 2022
Styling and coloring your XCTest logs on Xcode Console

XLTestLog Notes with Xcode 8 and XLTestLog Since Xcode 8 killed XcodeColors, the current way using XCTestLog on Xcode 8 is just plain texts with emoji

Xaree Lee 58 Feb 2, 2022
JustLog brings logging on iOS to the next level. It supports console, file and remote Logstash logging via TCP socket with no effort. Support for logz.io available.

JustLog JustLog takes logging on iOS to the next level. It supports console, file and remote Logstash logging via TCP socket with no effort. Support f

Just Eat 509 Dec 10, 2022
A custom logger implementation and Task Local helper for swift-log

LGNLog A custom logger implementation and TaskLocal helper for Swift-Log. Why and how This package provides two and a half things (and a small bonus):

17:11 Games 0 Oct 26, 2021