Skip to content

Commit 5a5063b

Browse files
committed
journal: allow stream() to be used without any arguments
It's fairly easy to provide a reasonable default for the first argument. Let's do that. Also, the documentation was misleading, suggesting that the function itself can be passed as file parameter to print(). Use a different name for the temporary variable to clarify that.
1 parent 52d9f51 commit 5a5063b

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

systemd/journal.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ def send(MESSAGE, MESSAGE_ID=None,
446446
args.extend(_make_line(key, val) for key, val in kwargs.items())
447447
return sendv(*args)
448448

449-
def stream(identifier, priority=LOG_INFO, level_prefix=False):
449+
def stream(identifier=None, priority=LOG_INFO, level_prefix=False):
450450
r"""Return a file object wrapping a stream to journal.
451451
452452
Log messages written to this file as simple newline sepearted text strings
@@ -465,10 +465,13 @@ def stream(identifier, priority=LOG_INFO, level_prefix=False):
465465
SYSLOG_IDENTIFIER=myapp
466466
MESSAGE=message...
467467
468-
Using the interface with print might be more convenient:
468+
If identifier is None, a suitable default based on sys.argv[0] will be used.
469+
470+
This interface can be used conveniently with the print function:
469471
470472
>>> from __future__ import print_function
471-
>>> print('message...', file=stream) # doctest: +SKIP
473+
>>> fileobj = journal.stream()
474+
>>> print('message...', file=fileobj) # doctest: +SKIP
472475
473476
priority is the syslog priority, one of `LOG_EMERG`, `LOG_ALERT`,
474477
`LOG_CRIT`, `LOG_ERR`, `LOG_WARNING`, `LOG_NOTICE`, `LOG_INFO`, `LOG_DEBUG`.
@@ -477,6 +480,12 @@ def stream(identifier, priority=LOG_INFO, level_prefix=False):
477480
(such as '<1>') are interpreted. See sd-daemon(3) for more information.
478481
"""
479482

483+
if identifier is None:
484+
if not _sys.argv or not _sys.argv[0] or _sys.argv[0] == '-c':
485+
identifier = 'python'
486+
else:
487+
identifier = _sys.argv[0]
488+
480489
fd = stream_fd(identifier, priority, level_prefix)
481490
return _os.fdopen(fd, 'w', 1)
482491

0 commit comments

Comments
 (0)