fault/fm-common/sources/fm_log.py
Heitor Matsui f79b9a9748 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 <HeitorVieira.Matsui@windriver.com>
Change-Id: I3f2fe2953fb26ad1759f3bc10a358cb9d093192f
2021-11-19 10:07:36 -03:00

50 lines
1.2 KiB
Python

#
# Copyright (c) 2021 Wind River Systems, Inc.
#
# SPDX-License-Identifier: Apache-2.0
#
"""
Logging
"""
import logging
import logging.handlers
import os
import sys
_loggers = {}
def get_logger(name):
""" Get a logger or create one """
if name not in _loggers:
_loggers[name] = logging.getLogger(name)
setup_logger(_loggers[name])
return _loggers[name]
def setup_logger(logger):
""" Setup a logger """
# Send logs to /var/log/platform.log
syslog_facility = logging.handlers.SysLogHandler.LOG_LOCAL1
formatter = logging.Formatter("configassistant[%(process)d] " +
"%(pathname)s:%(lineno)s " +
"%(levelname)8s [%(name)s] %(message)s")
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)
logger.addHandler(handler)
logger.setLevel(logging.INFO)