Skip to content

Commit 860dd3e

Browse files
committed
Add logger support
1 parent 268db65 commit 860dd3e

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

LibreNMS/__init__.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import json
22
import logging
33
import os
4+
import re
45
import signal
56
import subprocess
67
import sys
@@ -227,7 +228,33 @@ def call_script(script, args=(), log_dest=None):
227228
base_dir = os.path.realpath(os.path.dirname(__file__) + "/..")
228229
cmd = base + ("{}/{}".format(base_dir, script),) + tuple(map(str, args))
229230
logger.debug("Running {}".format(cmd))
230-
return command_runner(cmd, **kwargs)
231+
exit_code, output = command_runner(cmd, **kwargs)
232+
233+
if log_dest is LogOutput.LOGGER:
234+
if output:
235+
for line in output.splitlines():
236+
level = infer_log_level(line)
237+
logger.log(level, line)
238+
239+
return exit_code, output
240+
241+
242+
def infer_log_level(line):
243+
"""Infer log level from a log line."""
244+
upper_line = line.upper()
245+
246+
if re.match(r"^\s*(\[)?(CRITICAL|FATAL)(]|:|\s|-)", upper_line):
247+
return logging.CRITICAL
248+
elif re.match(r"^\s*(\[)?(ERROR|ERR)(]|:|\s|-)", upper_line):
249+
return logging.ERROR
250+
elif re.match(r"^\s*(\[)?(WARN|WARNING)(]|:|\s|-)", upper_line):
251+
return logging.WARNING
252+
elif re.match(r"^\s*(\[)?(INFO|INFORMATION)(]|:|\s|-)", upper_line):
253+
return logging.INFO
254+
elif re.match(r"^\s*(\[)?(DEBUG|TRACE)(]|:|\s|-)", upper_line):
255+
return logging.DEBUG
256+
257+
return logging.INFO
231258

232259

233260
class DB:

0 commit comments

Comments
 (0)