os_log configuration iOS

Afternoon Apple Fourms,


I'm exploring the features of os_log for my team, and from reading the documentation and playing around an example project it seems to me that there isn't a support way to enable/disable log levels at all on iOS devices for subsystems or their categories The documentation suggest that there is a way to do this macOS using both a command line tool or by installing a plist:


https://developer.apple.com/documentation/os/logging?language=objc


"Logging behavior is normally governed by the system. However, while debugging in macOS, you can enable different logging levels for a subsystem using the log command-line tool’s config argument while logged in as root. See Listing 5, which shows how to enable debug-level logging for a subsystem."



"You can also override the logging behavior of a specific subsystem by creating and installing a logging configuration profile property list file in the /Library/Preferences/Logging/Subsystems/ directory."



To my knowledge, neither of these will work for iOS as you can't shell into the device, or install to the Library. Am I correct that there is no way to alert the configuration on the device? For example If I want debug logs for one category, but default on another?


Ex

Default-Options: Level:Info

Cat1: Level:Debug

Cat2: Level:Default



Thanks.

Post not yet marked as solved Up vote post of p8burns Down vote post of p8burns
3.9k views

Replies

I found an older post that got no reply:


https://forums.developer.apple.com/message/178405#178405


It's interesting that Apple has profiles for controlloing the log levels. These are used to turn up logging for programs like messages to revel more info for use in radars:

https://developer.apple.com/bug-reporting/profiles-and-logs/


Any chance I can get word if this is offically supported for 3rd parties? I suppose I'll file a coulpe of feature request radars. 🙂

You can configure os_log with different log levels like:

os_log("%{public}@", log: OSLog(subsystem: "com.example", category: "app"), type: .default, "default log level")
os_log("%{public}@", log: OSLog(subsystem: "com.example", category: "app"), type: .debug, "debug log level")


I have written a logging library JSLogger to simplify the logging using

os_log
. You can check that out.

Thanks for the response Kadaj,


But I'm looking for an answer about how to configure the behavior of the logs vs the actual labeling of the logs.


For example, I would be nice to be able to disable debugging for specific categories.

Here's the real answer:

You need to use add an OSLogPreferences dictionary to your app's "Info.plist" file.

For details, open Terminal and type man 5 os_log. The general structure will be something like this:

    <key>OSLogPreferences</key>
    <dict>
        <key>put.your.subsystem.name.here</key>
        <dict>
            <key>put.your.category.name.here</key>
            <dict>
                <key>Level</key>
                <dict>
                    <key>Enable</key>
                    <string>Debug</string>
                    <key>Persist</key>
                    <string>Debug</string>
                </dict>
                <key>Enable-Private-Data</key>
                <true/>
            </dict>
        </dict>
    </dict>

In Xcode, this looks like this:

You enter a subsystem and category when you instantiate your Logger in Swift, something like this:

import OSLog

let myLogger = Logger(subsystem: "com.example.mycompany.mysubsystem", category: "kittens")
logger.log("Hello there!")

The dictionary under OSLogPreferences can have multiple subsystems, and each subsystem can list one or more categories. You can also use the special category key, DEFAULT-OPTIONS, to define common settings for all categories in a subsystem. So, if you used the single subsystem, com.example.mycompany.mysubsystem, you could do something like this:

    <key>OSLogPreferences</key>
    <dict>
        <key>com.example.mycompany.mysubsystem</key>
        <dict>
            <key>DEFAULT-OPTIONS</key>
            <dict>
                <key>Level</key>
                <dict>
                    <key>Enable</key>
                    <string>Debug</string>
                    <key>Persist</key>
                    <string>Debug</string>
                </dict>
                <key>Enable-Private-Data</key>
                <true/>
            </dict>
        </dict>
    </dict>

In both of my examples, the Enable-Private-Data key isn't required, but I include it so that everything doesn't say when you try to look at your logs. Thanks to @eskimo for pointing me to this man page.

  • This is great write up, but I just can't get it to work for boilerplate iOS app. Import -> create logger -> log at each level. Add OSLogPreferences to info.plist with sub-system and category (which can be shown in the console log with the metadata). Set Level to Error and my Debug log messages still appear still appear. I tried using the DEFAULT-OPTIONS instead of the category and the same time.

Add a Comment

Here's the real answer

Indeed. And for more system log hints and tips, see Your Friend the System Log.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"