Skip to main content

Send messages to the macOS unified logging system (os_log)

Project description

Pyoslog

Pyoslog allows you to send messages to the macOS unified logging system using Python.

from pyoslog import os_log, OS_LOG_DEFAULT
os_log(OS_LOG_DEFAULT, 'Hello from Python!')

Installation

Pyoslog requires macOS 10.12 or later and Python 3.6 or later. Install using pip:

python -m pip install pyoslog

The module will install and import without error on earlier macOS versions, or on unsupported Operating Systems or incompatible Python versions. Use pyoslog.is_supported() if you need to support incompatible environments and want to know at runtime whether to use pyoslog. Please note that if is_supported() returns False then none of the module's other methods or constants will exist.

Usage

import pyoslog
if pyoslog.is_supported():
    pyoslog.log('This is an OS_LOG_TYPE_DEFAULT message via pyoslog')

Available methods

Pyoslog provides the following methods from Apple's unified logging header:

All the pyoslog methods have the same signatures as their native versions, except for where a method requires a format parameter. The os_log system requires a constant (static) format specifier, and it is not possible to achieve this via Python. As a result, all instances of format strings use "%{public}s", and all messages are converted to a string before passing to the native methods.

Pyoslog also offers a helper method – log – that by default posts a message of type OS_LOG_TYPE_DEFAULT to OS_LOG_DEFAULT. For example, the shortcut log('message') is equivalent to os_log_with_type(OS_LOG_DEFAULT, OS_LOG_TYPE_DEFAULT, 'message').

The Handler class is designed for use with Python's inbuilt logging module. It works as a drop-in replacement for other Handler varieties.

See pyoslog's method documentation for a full reference.

Labelling subsystem and category

Create a log object using os_log_create and pass it to any of the log methods to add your own subsystem and category labels:

import pyoslog
log = pyoslog.os_log_create('ac.robinson.pyoslog', 'custom-category')
pyoslog.os_log_with_type(log, pyoslog.OS_LOG_TYPE_DEBUG, 'Message to log object', log, 'of type', pyoslog.OS_LOG_TYPE_DEBUG)

Enabling and disabling log output

Log output can be enabled or disabled globally by switching between the desired log object and pyoslog.OS_LOG_DISABLED:

import pyoslog
log = pyoslog.OS_LOG_DEFAULT
pyoslog.os_log(log, 'Log output enabled')
log = pyoslog.OS_LOG_DISABLED
pyoslog.os_log(log, 'Log output disabled')

It is also possible to check whether individual log types are enabled for a particular log object:

import pyoslog
pyoslog.os_log_type_enabled(pyoslog.OS_LOG_DEFAULT, pyoslog.OS_LOG_TYPE_DEBUG)

It is not possible to directly set a log object's mode from Python, but see the config section of man log for documentation about doing this in sudo mode.

Integration with the logging module

Use the pyoslog Handler to direct messages to pyoslog:

import logging, pyoslog
handler = pyoslog.Handler()
handler.setSubsystem('org.example.your-app', 'filter-category')
logger = logging.getLogger()
logger.addHandler(handler)
logger.error('message')

Logger levels are mapped internally to the OS_LOG_TYPE_* values – for example, logger.debug('message') will generate a message of type OS_LOG_TYPE_DEBUG.

Receiving log messages

Logs can be viewed using Console.app or the log command. For example, messages sent using the default configuration can be streamed using:

log stream --predicate 'processImagePath CONTAINS [c] "python"'

Messages sent using custom log objects can be filtered more precisely. For example, to receive messages from the labelled subsystem used in the example above:

log stream --predicate 'subsystem == "ac.robinson.pyoslog"' --level debug

See man log for further details about the available options and filters.

Handling cleanup

When labelling subsystem and category using the native C methods there is a requirement to free the log object after use (using os_release). The pyoslog module handles this for you – there is no need to del or release these objects.

Limitations

As noted above, while the macOS os_log API allows use of a format string with many methods, this name is required to be a C string literal. As a result, pyoslog hardcodes all format strings to "%{public}s".

Testing

The pyoslog module's tests require the pyobjc OSLog framework wrappers and the storeWithScope initialiser in order to verify output so, as a result, can only be run on macOS 12 or later.

After installing the OSLog wrappers (via python -m pip install pyobjc-framework-OSLog), navigate to the tests directory and run:

python -m unittest

All of pyoslog's code is covered by tests, but please note that if Console.app is live-streaming messages, some tests may fail. See test_logging.py for discussion about why this is the case.

Alternatives

At the time this module was created there were no alternatives available on PyPi. Since then, the macos-oslog module has been released, with broadly equivalent functionality to pyoslog, except for the need to manually release the log object. There is also os-signpost, which uses cython to provide the OSSignposter API, and could easily be extended to provide os_log functionality.

In addition, there are other options available if PyPi access is not seen as a constraint:

Note that the pyobjc module OSLog is for reading from the unified logging system rather than writing to it (and as a result is used for testing pyoslog). A log.h binding is on that project's roadmap, but not yet implemented.

License

Apache 2.0

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyoslog-1.1.0.tar.gz (15.5 kB view details)

Uploaded Source

Built Distributions

pyoslog-1.1.0-cp310-cp310-macosx_12_0_arm64.whl (17.5 kB view details)

Uploaded CPython 3.10 macOS 12.0+ ARM64

pyoslog-1.1.0-cp310-cp310-macosx_11_0_arm64.whl (17.7 kB view details)

Uploaded CPython 3.10 macOS 11.0+ ARM64

pyoslog-1.1.0-cp310-cp310-macosx_10_12_x86_64.whl (17.3 kB view details)

Uploaded CPython 3.10 macOS 10.12+ x86-64

pyoslog-1.1.0-cp39-cp39-macosx_12_0_arm64.whl (17.5 kB view details)

Uploaded CPython 3.9 macOS 12.0+ ARM64

pyoslog-1.1.0-cp39-cp39-macosx_11_0_arm64.whl (17.7 kB view details)

Uploaded CPython 3.9 macOS 11.0+ ARM64

pyoslog-1.1.0-cp39-cp39-macosx_10_12_x86_64.whl (17.2 kB view details)

Uploaded CPython 3.9 macOS 10.12+ x86-64

pyoslog-1.1.0-cp38-cp38-macosx_12_0_arm64.whl (17.5 kB view details)

Uploaded CPython 3.8 macOS 12.0+ ARM64

pyoslog-1.1.0-cp38-cp38-macosx_11_0_arm64.whl (17.7 kB view details)

Uploaded CPython 3.8 macOS 11.0+ ARM64

pyoslog-1.1.0-cp38-cp38-macosx_10_12_x86_64.whl (17.2 kB view details)

Uploaded CPython 3.8 macOS 10.12+ x86-64

pyoslog-1.1.0-cp37-cp37m-macosx_12_0_arm64.whl (17.5 kB view details)

Uploaded CPython 3.7m macOS 12.0+ ARM64

pyoslog-1.1.0-cp37-cp37m-macosx_11_0_arm64.whl (17.7 kB view details)

Uploaded CPython 3.7m macOS 11.0+ ARM64

pyoslog-1.1.0-cp37-cp37m-macosx_10_12_x86_64.whl (17.2 kB view details)

Uploaded CPython 3.7m macOS 10.12+ x86-64

pyoslog-1.1.0-cp36-cp36m-macosx_12_0_arm64.whl (17.5 kB view details)

Uploaded CPython 3.6m macOS 12.0+ ARM64

pyoslog-1.1.0-cp36-cp36m-macosx_11_0_arm64.whl (17.7 kB view details)

Uploaded CPython 3.6m macOS 11.0+ ARM64

pyoslog-1.1.0-cp36-cp36m-macosx_10_12_x86_64.whl (17.2 kB view details)

Uploaded CPython 3.6m macOS 10.12+ x86-64

File details

Details for the file pyoslog-1.1.0.tar.gz.

File metadata

  • Download URL: pyoslog-1.1.0.tar.gz
  • Upload date:
  • Size: 15.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.9.0

File hashes

Hashes for pyoslog-1.1.0.tar.gz
Algorithm Hash digest
SHA256 7fce8aabdf096dfcb5f2fcea3d2906854b62fd4a1b8849111c387ccab9e06a6a
MD5 3f2cf484b3bebddfa9aa7e4a55e83913
BLAKE2b-256 f24ce92284bbed082cd1748b7624a3604578243eed6d8ff8509b6da18b51a916

See more details on using hashes here.

File details

Details for the file pyoslog-1.1.0-cp310-cp310-macosx_12_0_arm64.whl.

File metadata

File hashes

Hashes for pyoslog-1.1.0-cp310-cp310-macosx_12_0_arm64.whl
Algorithm Hash digest
SHA256 91498916aa55912118969700a031c42f9201004559ef79c6bf5f9f9e2787f2c5
MD5 80ab5191a4e8fbd908d16c73457f1b35
BLAKE2b-256 b82a9b81a4434cc79592ea05e544191698e1df4baed2ebcdc968141dbccccb0d

See more details on using hashes here.

File details

Details for the file pyoslog-1.1.0-cp310-cp310-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for pyoslog-1.1.0-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 aa0b3d0d4f0aa91bbc9b72b7ab94ab5c4bcaa3b27a4f52dad68b2170fee3fb41
MD5 89c3662eeabb1cbe870ea64420eb8636
BLAKE2b-256 4a2fc5c71cde068874eceaa99902bab40af1a7605036863490fc83069e107b84

See more details on using hashes here.

File details

Details for the file pyoslog-1.1.0-cp310-cp310-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for pyoslog-1.1.0-cp310-cp310-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 871c67f5367e0152873f334bb2fe5bd97ab089c7433acd182a49d738fc5c7d61
MD5 9e4c7e594875f8f1c4867ceb33b2566a
BLAKE2b-256 b9ef18467d903093bf7c1bcc2f60985f790c930843571218bec9f047c4d23e5e

See more details on using hashes here.

File details

Details for the file pyoslog-1.1.0-cp39-cp39-macosx_12_0_arm64.whl.

File metadata

File hashes

Hashes for pyoslog-1.1.0-cp39-cp39-macosx_12_0_arm64.whl
Algorithm Hash digest
SHA256 f29ec296b1745c0229c9d23e2013dbb06147d60ebf575f6b232aeba32815c4a2
MD5 ee8ce910da36689ddc8172638eb78204
BLAKE2b-256 bde1d4022a1c26ad2f5e33c8e215d20c103f899ca8efcc2d775e87ea22f9c8fe

See more details on using hashes here.

File details

Details for the file pyoslog-1.1.0-cp39-cp39-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for pyoslog-1.1.0-cp39-cp39-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 4f38cfa564ef2430531b8a391d6e89653f51dbec33ea983d3d541321227c94fb
MD5 a6c5d106476104ea6f57654ae91eaa48
BLAKE2b-256 eece5f6814d7114bd32a4a41a2b48a5999d3fa6c168aa1448b8559bb0911871e

See more details on using hashes here.

File details

Details for the file pyoslog-1.1.0-cp39-cp39-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for pyoslog-1.1.0-cp39-cp39-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 c9e540a27be35996bd840eb2a47bb60692cdd4664121328d76e19f11828a03f3
MD5 76bcc785f19ce39d2f528d473e5b1e2e
BLAKE2b-256 1959c03e900ebd1ee36db98bf9a07512ae9e665bf5d522ba5a5db16cba4a5f3e

See more details on using hashes here.

File details

Details for the file pyoslog-1.1.0-cp38-cp38-macosx_12_0_arm64.whl.

File metadata

File hashes

Hashes for pyoslog-1.1.0-cp38-cp38-macosx_12_0_arm64.whl
Algorithm Hash digest
SHA256 855db6af76b68fcbd493074f71c05e007a95e15184bae3df92e0dda5ac3a51dc
MD5 bb865184bd90fb88b6e49926fb334b87
BLAKE2b-256 950dd9843d8231cc4430ab0423822bb28b8d40dc362858ffcdcbf52e215d4617

See more details on using hashes here.

File details

Details for the file pyoslog-1.1.0-cp38-cp38-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for pyoslog-1.1.0-cp38-cp38-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 855ed3a12b61e74585386ee565d31f260c0a973666827a3db24fa1a474d42cf6
MD5 47cfa3e3192ecb3bc2cbe54aac64b6e0
BLAKE2b-256 778b664071a9ab38decb36de1d7cf3b489444300f5ca61617be891ff51fabf56

See more details on using hashes here.

File details

Details for the file pyoslog-1.1.0-cp38-cp38-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for pyoslog-1.1.0-cp38-cp38-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 ae6f7848eed49e46258f338cd867fc5bc3fb2f1e9577f869cabcba9bf7842d80
MD5 e33a21b38da538abb1ba128464f64e3f
BLAKE2b-256 3349ad29bc27f493151ca52baede481266aa0697b1ee2fb4508a069bff21e241

See more details on using hashes here.

File details

Details for the file pyoslog-1.1.0-cp37-cp37m-macosx_12_0_arm64.whl.

File metadata

File hashes

Hashes for pyoslog-1.1.0-cp37-cp37m-macosx_12_0_arm64.whl
Algorithm Hash digest
SHA256 31d6400990fdd15e86a44aea70fa7a4e883fe7e33c609cfc324504751ef5bcdb
MD5 269b6ba01edb36e4149281338039a515
BLAKE2b-256 6ad5ba2e9fdf81d0573ae1a408b0688205e828e519d7573544589bbfc538f892

See more details on using hashes here.

File details

Details for the file pyoslog-1.1.0-cp37-cp37m-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for pyoslog-1.1.0-cp37-cp37m-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 7691f6fd83f52aec637e1b9c47f857e236f76f1b27983114b67ebff21d845487
MD5 2f48a91e54dbf52ae57e4f695b1e11a6
BLAKE2b-256 8ef84781dda5f779a51d1064307f48898fd8b0f8393d8187193d49d837c22329

See more details on using hashes here.

File details

Details for the file pyoslog-1.1.0-cp37-cp37m-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for pyoslog-1.1.0-cp37-cp37m-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 e2ea01363d33af35a057b5a621350eb6e06af1e9ce084c69b501e1daf559b202
MD5 95320f12e56dadee16ee0fd16139acba
BLAKE2b-256 dfcacd6bd076f42f351d3a07169d73e0a6429b078847480e0d7c06982cea423f

See more details on using hashes here.

File details

Details for the file pyoslog-1.1.0-cp36-cp36m-macosx_12_0_arm64.whl.

File metadata

File hashes

Hashes for pyoslog-1.1.0-cp36-cp36m-macosx_12_0_arm64.whl
Algorithm Hash digest
SHA256 1a6e30a7442e1094df0cda675981f0cca08f0bba35033d3411ca672c70fb0968
MD5 15e50e7253ac795731f71ed2ee86135f
BLAKE2b-256 4735efa40c75bfe392d146fe66018b99cb26048e507645d1853f90ac481969d2

See more details on using hashes here.

File details

Details for the file pyoslog-1.1.0-cp36-cp36m-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for pyoslog-1.1.0-cp36-cp36m-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 3e7d6204b53b1ae8c1806885b5d127e4a085c823d303a95f51767b36b1334eb7
MD5 e4396b703592983146d73ac1f080fa22
BLAKE2b-256 7ed0429fa6c1ddae35633b16cb88bf89e4c69d2fa147e611382cc5a3293c4d6a

See more details on using hashes here.

File details

Details for the file pyoslog-1.1.0-cp36-cp36m-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for pyoslog-1.1.0-cp36-cp36m-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 5bec250074264a950e5e6fd59b69eb0e8ecd77f2e48871711a49a54e1694020e
MD5 4de80a8261962e4beb5533b593d35ec3
BLAKE2b-256 193cdcf9e555f29f589c4734a8321cecc0d71417ba745e38095d10aece83c695

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page