Skip to content

Commit 5dd8afd

Browse files
otamachankeszybz
authored andcommitted
journal: allow JournalHandler constructor to be called with args in a positional param
This change enables to add extra fields to JournalHandler in a configuration file loaded by `logging.config.fileConfig`, which only allows positional parameters: class=systemd.journal.JournalHandler args={'level': INFO, 'SYSLOG_IDENTIFIER': 'my-cool-app'} [zj: originally the patch added a new positional parameter to __init__(), but that is not backwards compatible. So I added a new classmethod to allow the positional parameters to be passed.]
1 parent 397dec7 commit 5dd8afd

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

systemd/journal.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,20 @@ def __init__(self, level=_logging.NOTSET, sender_function=send, **kwargs):
565565
self.send = sender_function
566566
self._extra = kwargs
567567

568+
@classmethod
569+
def with_args(cls, config=None):
570+
"""Create a JournalHandler with a configuration dictionary
571+
572+
This creates a JournalHandler instance, but accepts the parameters through
573+
a dictionary that can be specified as a positional argument. This is useful
574+
in contexts like logging.config.fileConfig, where the syntax does not allow
575+
for positional arguments.
576+
577+
>>> JournalHandler.with_args({'SYSLOG_IDENTIFIER':'my-cool-app'})
578+
<...JournalHandler ...>
579+
"""
580+
return cls(**(config or {}))
581+
568582
def emit(self, record):
569583
"""Write `record` as a journal event.
570584

systemd/test/test_journal.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,14 @@ def test_journalhandler_init_exception():
8282
kw = {' X ':3}
8383
with pytest.raises(ValueError):
8484
journal.JournalHandler(**kw)
85+
with pytest.raises(ValueError):
86+
journal.JournalHandler.with_args(kw)
8587

8688
def test_journalhandler_init():
8789
kw = {'X':3, 'X3':4}
8890
journal.JournalHandler(logging.INFO, **kw)
91+
kw['level'] = logging.INFO
92+
journal.JournalHandler.with_args(kw)
8993

9094
def test_journalhandler_info():
9195
record = logging.LogRecord('test-logger', logging.INFO, 'testpath', 1, 'test', None, None)
@@ -98,6 +102,16 @@ def test_journalhandler_info():
98102
assert 'X=3' in sender.buf[0]
99103
assert 'X3=4' in sender.buf[0]
100104

105+
sender = MockSender()
106+
handler = journal.JournalHandler.with_args({'level':logging.INFO, 'X':3, 'X3':4, 'sender_function':sender.send})
107+
handler.emit(record)
108+
assert len(sender.buf) == 1
109+
assert 'X=3' in sender.buf[0]
110+
assert 'X3=4' in sender.buf[0]
111+
112+
# just check that args==None doesn't cause an error
113+
journal.JournalHandler.with_args()
114+
101115
def test_journalhandler_no_message_id():
102116
record = logging.LogRecord('test-logger', logging.INFO, 'testpath', 1, 'test', None, None)
103117
sender = MockSender()

0 commit comments

Comments
 (0)