You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+26-7Lines changed: 26 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,5 @@
1
1
# Pyoslog
2
-
Pyoslog is a simple extension to send messages to the macOS [unified logging system](https://developer.apple.com/documentation/os/os_log) from Python.
2
+
Pyoslog is a simple Python module that allows you to send messages to the macOS [unified logging system](https://developer.apple.com/documentation/os/os_log).
3
3
4
4
```python
5
5
from pyoslog import os_log, OS_LOG_DEFAULT
@@ -8,19 +8,26 @@ os_log(OS_LOG_DEFAULT, 'Hello from Python!')
8
8
9
9
10
10
## Installation
11
+
Pyoslog requires macOS 10.12 or later.
12
+
Install using `pip`:
13
+
11
14
```shell
12
15
python -m pip install pyoslog
13
16
```
14
17
15
-
Pyoslog requires macOS 10.12 or later.
18
+
The module will install and import without error on earlier macOS versions.
19
+
Use `pyoslog.is_supported()` if you need to support old macOS versions and want to know at runtime whether to use pyoslog.
20
+
Please note that if `is_supported()` returns `False` then none of the module's other methods or constants will exist.
16
21
17
22
18
23
## Usage
19
24
Pyoslog currently provides the methods [`os_log_create`](https://developer.apple.com/documentation/os/1643744-os_log_create), [`os_log_with_type`](https://developer.apple.com/documentation/os/os_log_with_type) and [`os_log`](https://developer.apple.com/documentation/os/os_log), each with the same signatures as their native versions.
20
25
21
-
The module 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')`.
26
+
The module also offers a helper method – `log` – that by default posts a message of type `OS_LOG_TYPE_DEFAULT` to `OS_LOG_DEFAULT`.
27
+
For example, the shortcut `log('message')` is equivalent to `os_log_with_type(OS_LOG_DEFAULT, OS_LOG_TYPE_DEFAULT, 'message')`.
22
28
23
29
The `Handler` class is designed for use with Python's inbuilt [logging](https://docs.python.org/3/library/logging.html) module.
30
+
It works as a drop-in replacement for other Handler varieties.
24
31
25
32
### Labelling subsystem and category
26
33
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:
@@ -45,15 +52,20 @@ logger.addHandler(handler)
45
52
logger.debug('message')
46
53
```
47
54
55
+
To configure the Handler's output type, use `handler.setLevel` with a level from the logging module.
56
+
These are mapped internally to the `OS_LOG_TYPE` values – for example, `handler.setLevel(logging.DEBUG)` will configure the Handler to output messages of type `OS_LOG_TYPE_DEBUG`.
57
+
48
58
49
59
### Receiving log messages
50
-
Logs can be viewed using Console.app or the `log` command. For example, messages sent using the default configuration can be monitored using:
60
+
Logs can be viewed using Console.app or the `log` command.
61
+
For example, messages sent using the default configuration can be streamed using:
Messages sent using custom configurations can be filtered more precisely. For example, to receive messages from the labelled subsystem used in the example above:
67
+
Messages sent using custom configurations can be filtered more precisely.
68
+
For example, to receive messages from the labelled subsystem used in the example above:
See `man log` for further details about the available options and filters.
63
75
64
76
77
+
### Handling cleanup
78
+
When labelling subsystem and category using the native C methods there is a requirement to free the log object after use (using `os_release`).
79
+
The pyoslog module handles this for you – there is no need to `del` or release these objects.
80
+
81
+
65
82
## Alternatives
66
-
At the time this module was created there were no alternatives available on [PyPi](https://pypi.org/). There are, however, other options available if this is not seen as a constraint:
83
+
At the time this module was created there were no alternatives available on [PyPi](https://pypi.org/search/?q=macos+unified+logging&c=Operating+System+%3A%3A+MacOS).
84
+
There are, however, other options available if this is not seen as a constraint:
Note that the [pyobjc](https://pyobjc.readthedocs.io/) module [OSLog](https://pypi.org/project/pyobjc-framework-OSLog/) is for _reading_ from the unified logging system rather than writing to it. A `log.h` binding is on that project's [roadmap](https://github.com/ronaldoussoren/pyobjc/issues/377), but not yet implemented.
90
+
Note that the [pyobjc](https://pyobjc.readthedocs.io/) module [OSLog](https://pypi.org/project/pyobjc-framework-OSLog/) is for _reading_ from the unified logging system rather than writing to it.
91
+
A `log.h` binding is on that project's [roadmap](https://github.com/ronaldoussoren/pyobjc/issues/377), but not yet implemented.
0 commit comments