PyObjC OSLog Framework Wrappers

12.1 · active · verified Tue Apr 14

PyObjC-framework-OSLog provides Python wrappers for Apple's OSLog framework on macOS, allowing Python applications to interact with the unified logging system. It is part of the larger PyObjC project, currently at version 12.1, and maintains an active release cadence with frequent updates to align with macOS SDK changes and Python versions.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to import the OSLog framework and log a simple message to the macOS unified logging system. It shows how to create a log handle and then use `os_log_with_type` to send a message. You can view these logs using macOS's Console.app.

import OSLog
from Foundation import NSObject

def main():
    # Instantiate an OSLog object (equivalent to Objective-C: [[OSLog alloc] initWithSubsystem:category:])
    # os_log_create is an alternative to OSLog.alloc().init...
    log_handle = OSLog.os_log_create('com.example.myapp', 'general')

    # Log a message at the default level (OS_LOG_TYPE_DEFAULT)
    # Equivalent to Objective-C: os_log(log_handle, "Hello from PyObjC OSLog!");
    # Note: For simplicity, string formatting and privacy modifiers (%{public}s, %{private}s) are not shown here
    # as they typically apply to C-level os_log calls. PyObjC handles basic strings directly.
    OSLog.os_log_with_type(log_handle, OSLog.OS_LOG_TYPE_DEFAULT, 'Hello from PyObjC OSLog!')
    print('Logged a message to system console. Check Console.app for "Hello from PyObjC OSLog!".')

if __name__ == '__main__':
    main()

view raw JSON →