diff options
-rw-r--r-- | README.md | 4 | ||||
-rw-r--r-- | systemd/journal.py | 30 | ||||
-rw-r--r-- | systemd/test/test_daemon.py | 5 | ||||
-rw-r--r-- | systemd/test/test_journal.py | 36 |
4 files changed, 60 insertions, 15 deletions
@@ -25,6 +25,10 @@ On Debian/Ubuntu/Mint apt-get install python-systemd python3-systemd +On openSUSE and SLE + + zypper in python-systemd + To build from source On Fedora 21+ with Python 2: diff --git a/systemd/journal.py b/systemd/journal.py index 41ffbdc..83e3004 100644 --- a/systemd/journal.py +++ b/systemd/journal.py @@ -236,7 +236,7 @@ class Reader(_Reader): super(Reader, self).add_match(arg) def get_next(self, skip=1): - """Return the next log entry as a dictionary. + r"""Return the next log entry as a dictionary. Entries will be processed with converters specified during Reader creation. @@ -257,7 +257,7 @@ class Reader(_Reader): return dict() def get_previous(self, skip=1): - """Return the previous log entry. + r"""Return the previous log entry. Equivalent to get_next(-skip). @@ -546,6 +546,8 @@ class JournalHandler(_logging.Handler): raise ValueError('Invalid field name: ' + name) if 'SYSLOG_IDENTIFIER' not in kwargs: kwargs['SYSLOG_IDENTIFIER'] = _sys.argv[0] + + self.send = kwargs.pop('SENDER_FUNCTION', send) self._extra = kwargs def emit(self, record): @@ -559,9 +561,9 @@ class JournalHandler(_logging.Handler): msg = self.format(record) pri = self.mapPriority(record.levelno) mid = getattr(record, 'MESSAGE_ID', None) - extras = { k:str(v) for k,v in self._extra.iteritems() } + extras = { k:str(v) for k,v in self._extra.items() } extras.update({ - k:str(v) for k,v in record.__dict__.iteritems() + k:str(v) for k,v in record.__dict__.items() }) if record.exc_text: @@ -573,16 +575,16 @@ class JournalHandler(_logging.Handler): if record.args: extras['CODE_ARGS'] = str(record.args) - send(msg, - MESSAGE_ID=mid, - PRIORITY=format(pri), - LOGGER=record.name, - THREAD_NAME=record.threadName, - PROCESS_NAME=record.processName, - CODE_FILE=record.pathname, - CODE_LINE=record.lineno, - CODE_FUNC=record.funcName, - **extras) + self.send(msg, + MESSAGE_ID=mid, + PRIORITY=format(pri), + LOGGER=record.name, + THREAD_NAME=record.threadName, + PROCESS_NAME=record.processName, + CODE_FILE=record.pathname, + CODE_LINE=record.lineno, + CODE_FUNC=record.funcName, + **extras) except Exception: self.handleError(record) diff --git a/systemd/test/test_daemon.py b/systemd/test/test_daemon.py index 5e9f5b8..c1e08c7 100644 --- a/systemd/test/test_daemon.py +++ b/systemd/test/test_daemon.py @@ -273,7 +273,10 @@ def test_notify_bad_socket(): def test_notify_with_socket(tmpdir): path = tmpdir.join('socket').strpath sock = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM) - sock.bind(path) + try: + sock.bind(path) + except socket.error as e: + pytest.xfail('failed to bind socket (%s)' % e) # SO_PASSCRED is not defined in python2.7 SO_PASSCRED = getattr(socket, 'SO_PASSCRED', 16) sock.setsockopt(socket.SOL_SOCKET, SO_PASSCRED, 1) diff --git a/systemd/test/test_journal.py b/systemd/test/test_journal.py index 2ecfc16..fd38036 100644 --- a/systemd/test/test_journal.py +++ b/systemd/test/test_journal.py @@ -6,13 +6,40 @@ import logging import os import time import uuid +import traceback as _traceback from systemd import journal, id128 +from systemd.journal import _make_line import pytest TEST_MID = uuid.UUID('8441372f8dca4ca98694a6091fd8519f') +class MockSender: + def __init__(self): + self.buf = [] + + def send(self, MESSAGE, MESSAGE_ID=None, + CODE_FILE=None, CODE_LINE=None, CODE_FUNC=None, + **kwargs): + args = ['MESSAGE=' + MESSAGE] + + if MESSAGE_ID is not None: + id = getattr(MESSAGE_ID, 'hex', MESSAGE_ID) + args.append('MESSAGE_ID=' + id) + + if CODE_LINE == CODE_FILE == CODE_FUNC == None: + CODE_FILE, CODE_LINE, CODE_FUNC = _traceback.extract_stack(limit=2)[0][:3] + if CODE_FILE is not None: + args.append('CODE_FILE=' + CODE_FILE) + if CODE_LINE is not None: + args.append('CODE_LINE={:d}'.format(CODE_LINE)) + if CODE_FUNC is not None: + args.append('CODE_FUNC=' + CODE_FUNC) + + args.extend(_make_line(key, val) for key, val in kwargs.items()) + self.buf.append(args) + @contextlib.contextmanager def skip_oserror(code): try: @@ -59,6 +86,15 @@ def test_journalhandler_init(): kw = {'X':3, 'X3':4} journal.JournalHandler(logging.INFO, **kw) +def test_journalhandler_info(): + record = logging.LogRecord('test-logger', logging.INFO, 'testpath', 1, 'test', None, None) + + sender = MockSender() + kw = {'X':3, 'X3':4, 'SENDER_FUNCTION': sender.send} + handler = journal.JournalHandler(logging.INFO, **kw) + handler.emit(record) + assert len(sender.buf) == 1 + def test_reader_init_flags(): j1 = journal.Reader() j2 = journal.Reader(journal.LOCAL_ONLY) |