From f79b9a974895f794c802ec07d0dd6ffccf593be3 Mon Sep 17 00:00:00 2001 From: Heitor Matsui Date: Thu, 18 Nov 2021 16:22:29 -0300 Subject: [PATCH] Use alternate log handler if running in container Currently the log is written to syslog, which in turn uses a socket /dev/log from the platform, but when running in a container such as in the stx-openstack application this log handler is not available. This commit uses an environment variable to be set on the fm-rest-api deployment definition to determine if it must the syslog handler, which is the case when running on the platform, or write the log to standard output, which is the case when running inside a container. Closes-Bug: 1951579 Signed-off-by: Heitor Matsui Change-Id: I3f2fe2953fb26ad1759f3bc10a358cb9d093192f --- fm-common/sources/fm_log.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/fm-common/sources/fm_log.py b/fm-common/sources/fm_log.py index 1918dc7e..d49ae9b3 100644 --- a/fm-common/sources/fm_log.py +++ b/fm-common/sources/fm_log.py @@ -11,6 +11,8 @@ Logging import logging import logging.handlers +import os +import sys _loggers = {} @@ -34,8 +36,12 @@ def setup_logger(logger): "%(pathname)s:%(lineno)s " + "%(levelname)8s [%(name)s] %(message)s") - handler = logging.handlers.SysLogHandler(address='/dev/log', - facility=syslog_facility) + running_in_container = os.getenv("RUNNING_IN_CONTAINER", "False").strip().lower() + if running_in_container == "true": + handler = logging.StreamHandler(stream=sys.stdout) + else: + handler = logging.handlers.SysLogHandler(address='/dev/log', + facility=syslog_facility) handler.setLevel(logging.INFO) handler.setFormatter(formatter)