|
1 | | -from ._journald import sendv |
2 | 1 | import traceback as _traceback |
| 2 | +import os as _os |
| 3 | +from syslog import (LOG_EMERG, LOG_ALERT, LOG_CRIT, LOG_ERR, |
| 4 | + LOG_WARNING, LOG_NOTICE, LOG_INFO, LOG_DEBUG) |
| 5 | +from ._journald import sendv, stream_fd |
3 | 6 |
|
4 | 7 | def _make_line(field, value): |
5 | 8 | if isinstance(value, bytes): |
@@ -52,3 +55,39 @@ def send(MESSAGE, MESSAGE_ID=None, |
52 | 55 |
|
53 | 56 | args.extend(_make_line(key, val) for key, val in kwargs.items()) |
54 | 57 | return sendv(*args) |
| 58 | + |
| 59 | +def stream(identifier, priority=LOG_DEBUG, level_prefix=False): |
| 60 | + r"""Return a file object wrapping a stream to journald. |
| 61 | +
|
| 62 | + Log messages written to this file as simple newline sepearted |
| 63 | + text strings are written to the journal. |
| 64 | +
|
| 65 | + The file will be line buffered, so messages are actually sent |
| 66 | + after a newline character is written. |
| 67 | +
|
| 68 | + >>> stream = journald.stream('myapp') |
| 69 | + >>> stream |
| 70 | + <open file '<fdopen>', mode 'w' at 0x...> |
| 71 | + >>> stream.write('message...\n') |
| 72 | +
|
| 73 | + will produce the following message in the journal: |
| 74 | +
|
| 75 | + PRIORITY=7 |
| 76 | + SYSLOG_IDENTIFIER=myapp |
| 77 | + MESSAGE=message... |
| 78 | +
|
| 79 | + Using the interface with print might be more convinient: |
| 80 | +
|
| 81 | + >>> from __future__ import print_function |
| 82 | + >>> print('message...', file=stream) |
| 83 | +
|
| 84 | + priority is the syslog priority, one of LOG_EMERG, LOG_ALERT, |
| 85 | + LOG_CRIT, LOG_ERR, LOG_WARNING, LOG_NOTICE, LOG_INFO, LOG_DEBUG. |
| 86 | +
|
| 87 | + level_prefix is a boolean. If true, kernel-style log priority |
| 88 | + level prefixes (such as '<1>') are interpreted. See sd-daemon(3) |
| 89 | + for more information. |
| 90 | + """ |
| 91 | + |
| 92 | + fd = stream_fd(identifier, priority, level_prefix) |
| 93 | + return _os.fdopen(fd, 'w', 1) |
0 commit comments