PyObjC OSLog Framework Wrappers
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
- breaking PyObjC 12.x (including pyobjc-framework-oslog 12.1) officially drops support for Python 3.9. PyObjC 11.x dropped support for Python 3.8.
- breaking PyObjC 11.1 introduced significant changes to align the core bridge's Automatic Reference Counting (ARC) behavior with `clang`'s documentation for initializer methods. Methods in the 'init' family now correctly steal and return new references. Code that accidentally worked in previous versions due to hidden reference counting bugs may now crash.
- gotcha There was a breaking change in PyObjC 10.3 regarding the interaction of `__init__` and `__new__` in Python subclasses of Objective-C classes. While 10.3.1 partially reverted this, user-defined `__init__` methods on classes with a user-defined `__new__` will work, but `__init__` cannot be used with the `__new__` provided by PyObjC.
- deprecated PyObjC frameworks are direct wrappers of Apple's Objective-C frameworks. If Apple deprecates or removes an underlying framework (e.g., IMServicePlugIn removed in macOS 14), its PyObjC bindings will also be removed in future PyObjC versions without specific deprecation cycles within PyObjC itself.
Install
-
pip install pyobjc-framework-oslog
Imports
- OSLog
import OSLog
- OSLog.os_log_create
from OSLog import os_log_create
Quickstart
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()