Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KP-8688 Add a syslog handler #11

Merged
merged 6 commits into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 19 additions & 10 deletions korpplugins/logger/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
# See config.py.template for more information on the configuration variables

pluginconf = korppluginlib.get_plugin_config(
# Which facilities to use
LOG_USING_NATIVE_PYTHON = True,
LOG_USING_SYSLOG = False,
# Base directory for log files
LOG_BASEDIR = "/v/korp/log/korp-py",
# Log filename format string (for str.format())
Expand Down Expand Up @@ -178,16 +181,22 @@ def __init__(self):
self._logger = logging.getLogger(__name__)
self._logger.setLevel(pluginconf.LOG_LEVEL)
tm = time.localtime()
logfile = (os.path.join(pluginconf.LOG_BASEDIR,
pluginconf.LOG_FILENAME_FORMAT)
.format(year=tm.tm_year, mon=tm.tm_mon, mday=tm.tm_mday,
hour=tm.tm_hour, min=tm.tm_min, sec=tm.tm_sec,
pid=os.getpid()))
logdir = os.path.split(logfile)[0]
os.makedirs(logdir, exist_ok=True)
handler = logging.FileHandler(logfile)
handler.setFormatter(TruncatingLogFormatter(pluginconf.LOG_FORMAT))
self._logger.addHandler(handler)
if pluginconf.LOG_USING_NATIVE_PYTHON:
logfile = (os.path.join(pluginconf.LOG_BASEDIR,
pluginconf.LOG_FILENAME_FORMAT)
.format(year=tm.tm_year, mon=tm.tm_mon, mday=tm.tm_mday,
hour=tm.tm_hour, min=tm.tm_min, sec=tm.tm_sec,
pid=os.getpid()))
logdir = os.path.split(logfile)[0]
os.makedirs(logdir, exist_ok=True)
logfile_handler = logging.FileHandler(logfile)
logfile_handler.setFormatter(TruncatingLogFormatter(pluginconf.LOG_FORMAT))
self._logger.addHandler(logfile_handler)
if pluginconf.LOG_USING_SYSLOG:
syslog_handler = logging.handlers.SysLogHandler(address='/dev/log',
facility=logging.handlers.SysLogHandler.LOG_LOCAL0)
syslog_handler.setFormatter(TruncatingLogFormatter(pluginconf.LOG_FORMAT))
self._logger.addHandler(syslog_handler)
# Storage for request-specific data, such as start times
self._logdata = dict()

Expand Down
5 changes: 5 additions & 0 deletions korpplugins/logger/config.py.template
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ Configuration module for korpplugins.logger.

import logging

# Do we write to logfiles from Python?
LOG_USING_NATIVE_PYTHON = True

# Do we log using syslog?
LOG_USING_SYSLOG = False

# Base directory for log files
LOG_BASEDIR = "/v/korp/log/korp-py"
Expand Down