5

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.

TT--
  • 2,956
  • 1
  • 27
  • 46
mfaani
  • 33,269
  • 19
  • 164
  • 293

2 Answers2

3

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!

mfaani
  • 33,269
  • 19
  • 164
  • 293
  • I read this as well, but It does not say how to do it on iOS, only on MacOS. It indicates that you can use 'log profile', but I don't what it is or how to do it – gutte Jun 18 '19 at 12:16
  • I think in macOS just means keep the iPhone connected to a macOS – mfaani Jun 18 '19 at 13:01
  • This doesn't answer the question: This explains how to **enable** rather than **disable** log levels. – TT-- Nov 17 '21 at 17:06
2

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)
TT--
  • 2,956
  • 1
  • 27
  • 46
Helam
  • 1,385
  • 13
  • 17
  • 1
    that would disable logging of **all** levels. But docs says: _Returns a Boolean value indicating whether a **specific** type of logging, such as default, info, debug, error, or fault, is enabled for a specified log object._ – mfaani May 28 '19 at 14:49
  • I see, I misunderstood your question. Did you find a good way to do this? I have viewed the docs mentioned in the other answer and have not found a way to apply them on iOS. – Helam May 28 '19 at 17:02
  • Did you see my answer? I never tried it though. I haven't found anything else – mfaani May 28 '19 at 17:03
  • 1
    Oh I didn't realize it was your own answer, but I have tried doing what is mentioned in your answer and have not been successful. I have opened a support case with apple regarding this as they have no documentation for how to do this on iOS, only on macOS. The sudo log config command they mention does not exist on iOS. I'll let you know if I learn anything useful. If you could do the same :) – Helam May 28 '19 at 19:50
  • Another thing I don't understand, is that the docs say that DEBUG level is turned off by default, but I do see them in Console App when I install my app from TestFlight – gutte Jun 18 '19 at 12:19
  • @gutte I _think_ for TestFlight it’s expected. You want those debug info. It should be there on a release build... – mfaani Jun 18 '19 at 13:00