Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift: OSLog/os_log not showing up in console app

Tags:

logging

swift

I am building a Swift 5 application with XCode 10.3. For this, I have a framework which contains a implementation for a logsystem (for debugging purposes). The default implementation for this logsystem is based on OSLog/os_log. When using the system in the consuming app, then none of the logs appear in the Console app. However, when placing breakpoints, I can see that the os_log statement (see code example below) is reached and that the correct parameters are passed on to the os_log function. However, when I use os_log or NSLog in the host application, then they do show up.

I have verified that it is not an issue with the LogSystem / DefaultLogImplementation types as all of the breakpoints that need to be hit, are hit, and all unit tests are green. In addition, the os_log statement is reached and executed, but the logs do not appear. I have verified that all messages are shown in the console app, I have verified that I have selected the correct device, I have tried multiple filters (and even dug through all the logs without filters enabled)...

Can anyone help / give a pointer at what the issue may be? I am currently suspecting that there is a bug in the implementation of OSLog/os_log.

Code sample

App-side code, which consumes code similar to the examples provided below

class SomeClass {
    private let logSystem = LogSystem()

    func doSomethingImportant() {
        // Do important stuff and log the result
        logSystem.debug("Finished process with result \(result)")
    }

}

Framework-side code, which is consumed by the app

public class LogSystem {

    private let logImplementation: LogImplementation

    init(_ logImplementation: LogImplementation = DefaultLogImplementation()) {
        self.logImplementation = logImplementation
    }

    public func debug(_ message: String) {
        logImplementation.log(message, type: .debug) // And some other parameters...
    }

    public func error(_ message: String) {
        // Similar to debug(:)...
    }

}

public protocol LogImplementation {
    func log(_ message: String, type: LogType, ...other parameters...)
}

public class DefaultLogImplementation: LogImplementation {
    func log(_ message: String, type: LogType, ...other parameters...) {
        let parsedType = parseLogType(type) // Function that parses LogType to OSLogType
        let log = OSLog(subsystem: "my.subsystem.domain", category: "myCategory")
        os_log("%{private}@", log: log, type: parsedType, message) // Breakpoint here is reached, but log does not appear in console app (even with debugger attached, which should remove the effect of "%{private}%". Either way, at the very least censored logs should still appear in console app.
    }
}

Additional info

Swift version: 5.0

XCode version: 10.3

Target device: iOS 12.2 iPhone X simulator

NSLog appears in console app: Yes

Selected correct device in console app: Yes

Correct filters: Yes

Update 2019-08-16 13:00 (Amsterdam time)

It appears that only .debug level messages are not appearing in the Console app. This bug occurs when using any simulator device in combination with OSLog. I have tried several commands to fix this:

sudo log config --mode level:debug,persist:debug

sudo log config --subsystem my.reverse.domain.name --mode level:debug,persist:debug

Neither of them fixed the issue. In fact, not a single debug-level message of the simulator is showing up in the console app (not even from iOS itself). Yes, the option to show .info and .debug level messages is enabled.

I tried setting the log-level for the simulator specifically through the following command:

xcrun simctl spawn booted log config --mode level:debug

But this result in an error:

log: Simulator unable to set system mode

like image 977
ImJustACowLol Avatar asked Feb 04 '23 17:02

ImJustACowLol


1 Answers

In the console app, there are two options to include / hide debug and info messages:

enter image description here

like image 198
JAHelia Avatar answered Feb 06 '23 14:02

JAHelia