mfaani
mfaani

Reputation: 36427

How can I disable a certain oslog log level?

For the following function:

let authenticationLogger = OSLog(subsystem: "com.Company.AppNameQA" ?? "Empty bundleIdentifier", category: "Authenticaiton)

What should I do if I want to disable/enable a certain log level?

Currently with the API the only thing that I'm able to access is the isEnabled fucntion:

authenticationLogger.isEnabled(.error) 

which just returns whether it's enabled or not.

Upvotes: 5

Views: 3106

Answers (2)

Helam
Helam

Reputation: 1505

You can use custom logic or environment variables to decide whether or not to disable specific logs by assigning OSLog.disabled.

This example from WWDC 2018 session Measuring Performance Using Logging uses an environment variable to determine whether or not to disable this log handle:

let refreshLog: OSLog
if ProcessInfo.processInfo.environment.keys.contains("SIGNPOSTS_FOR_REFRESH") {
    refreshLog = OSLog(subsystem: "com.example.your-app", category: "RefreshOperations")
} else {
    refreshLog = .disabled
}

When you assign .disabled to your OSLog variable it will automatically disable all logs that use that log handle.

When using the Logger class (Swift) the init(_ logObj: OSLog) initializer can be used to set OSLog.disabled:

static let loggerInstance = Logger(OSLog.disabled)

Upvotes: 4

mfaani
mfaani

Reputation: 36427

I haven't tried this yet. But I believe this is the solution

Reading from docs.

Under the section of:

Customizing Logging Behavior While Debugging

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.

Enabling debug-level logging for a subsystem

$ sudo log config --mode "level:debug" --subsystem com.your_company.your_subsystem_name

Use the log tool’s status argument to check the current logging level of a subsystem.

Checking the log level of a subsystem

$ sudo log config --status --subsystem com.your_company.your_subsystem_name
Mode for 'com.your_company.your_subsystem_name'  DEBUG

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. Name the file using an identifier string, in reverse DNS notation, representing the subsystem. For example, com.your_company.your_subsystem_name.plist. Next, add one or more settings dictionaries to the top level of the file. A DEFAULT-OPTIONS settings dictionary defines global behavior settings for the entire subsystem. Category settings dictionaries define behavior for specific categories of messages within the subsystem.

Top level structure of a logging profile

<dict>
    <key>DEFAULT-OPTIONS</key>
    <dict>
       <!-- GLOBAL SUBSYSTEM OR PROCESS SETTINGS -->
    </dict>
    <key>CategoryName</key>
    <dict>
       <!-- CATEGORY SETTINGS -->
    </dict>
</dict>

In a nutshell, you can't change the log level from you code in production. You can only change it during debugging. What use can it have? I'm not sure!

Upvotes: 3

Related Questions