Skip to content

Commit fe9c7d1

Browse files
authored
Merge pull request #100 from linuxmuster/perf/syslog-instead-of-logger
Perf: use syslog instead of forking logger for every log line
2 parents 3ebf7b6 + 32e7acd commit fe9c7d1

2 files changed

Lines changed: 12 additions & 16 deletions

File tree

‎usr/lib/python3/dist-packages/linuxmusterLinuxclient7/logging.py‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
import traceback, re, sys, subprocess, inspect
1+
import traceback, re, sys, inspect, syslog
22
from enum import Enum
33
from linuxmusterLinuxclient7 import config
44

5+
syslog.openlog("linuxmuster-linuxclient7")
6+
57
class Level(Enum):
68
DEBUG = 0
79
INFO = 1
@@ -140,4 +142,4 @@ def _log(level, message):
140142
logMessage = f"[{level.name}] {moduleName} {message}"
141143

142144
print(logMessage)
143-
subprocess.call(["logger", "-t", "linuxmuster-linuxclient7", logMessage])
145+
syslog.syslog(logMessage)

‎usr/lib/python3/dist-packages/linuxmusterLinuxclient7/tests/test_logging.py‎

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22
from .. import logging
33
import os
44

5-
@mock.patch("subprocess.call")
6-
def test_forAllLevels(mockSubprocessCall):
5+
@mock.patch("syslog.syslog")
6+
def test_forAllLevels(mockSyslog):
77
logging.debug("debug")
88
logging.info("info")
99
logging.warning("warning")
1010
logging.error("error")
1111
logging.fatal("fatal")
1212
logging.exception(Exception("exception"))
1313

14-
logs = _getLoggedLogs(mockSubprocessCall)
14+
logs = _getLoggedLogs(mockSyslog)
1515
assert logs == [
1616
"[DEBUG] (tests.test_logging) debug",
1717
"[INFO] (tests.test_logging) info",
@@ -24,12 +24,12 @@ def test_forAllLevels(mockSubprocessCall):
2424
]
2525

2626
@mock.patch("inspect.stack")
27-
@mock.patch("subprocess.call")
28-
def test_inspectStackEmpty(mockSubprocessCall, mockInspectStack):
27+
@mock.patch("syslog.syslog")
28+
def test_inspectStackEmpty(mockSyslog, mockInspectStack):
2929
mockInspectStack.return_value = []
3030
logging.info("info without module name")
3131

32-
logs = _getLoggedLogs(mockSubprocessCall)
32+
logs = _getLoggedLogs(mockSyslog)
3333
assert logs == [
3434
"[INFO] info without module name"
3535
]
@@ -56,14 +56,8 @@ def test_printLogs(mockConfigNetwork, mockOpen, mockPrint):
5656
# - Helper functions -
5757
# --------------------
5858

59-
def _getLoggedLogs(mockSubprocessCall):
60-
logs = []
61-
for call_args in mockSubprocessCall.call_args_list:
62-
args = call_args.args[0]
63-
print(args)
64-
if type(args) == list and args[0] == "logger":
65-
logs.append(args[3])
66-
return logs
59+
def _getLoggedLogs(mockSyslog):
60+
return [call_args.args[0] for call_args in mockSyslog.call_args_list]
6761

6862
def _getPrintedLines(mockPrint):
6963
lines = []

0 commit comments

Comments
 (0)