bb98dea067
The logging statements in our modules, including CLI, are now using a dedicated VF logger with Syslog and stdout handlers. This will ensure, that all messages will be properly journaled and that only messages >warn will be displayed to user by default. Patch only changes behavior of the logging statements in the VF code itself. Log statements in imported modules, such as ansible-runner, will behave as before. In the absence of available '/dev/log' device the user will be notified, during logger init, by a warning level message. Tests for the new code are included. Signed-off-by: Jiri Podivin <jpodivin@redhat.com> Change-Id: If8ed6510dad16dc8495717789bb132b957828e0d
48 lines
1.5 KiB
Python
48 lines
1.5 KiB
Python
# Copyright 2022 Red Hat, Inc.
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
# not use this file except in compliance with the License. You may obtain
|
|
# a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
# License for the specific language governing permissions and limitations
|
|
# under the License.
|
|
#
|
|
import logging
|
|
import os
|
|
from logging.handlers import SysLogHandler
|
|
|
|
|
|
def getLogger(loggerName, stream_lvl=logging.WARN):
|
|
"""Create logger instance.
|
|
|
|
:param loggerName: name of the new Logger instance
|
|
:type loggerName: `str`
|
|
:param stream_lvl: minimum level at which the messages will be printed to stream
|
|
:type stream_lvl: `int`
|
|
:rtype: `Logger`
|
|
"""
|
|
new_logger = logging.getLogger(loggerName)
|
|
|
|
formatter = logging.Formatter("%(asctime)s %(module)s %(message)s")
|
|
|
|
s_handler = logging.StreamHandler()
|
|
s_handler.setFormatter(formatter)
|
|
s_handler.setLevel(stream_lvl)
|
|
|
|
new_logger.addHandler(s_handler)
|
|
|
|
if os.path.exists('/dev/log'):
|
|
sys_handler = SysLogHandler(address='/dev/log')
|
|
sys_handler.setFormatter(formatter)
|
|
|
|
new_logger.addHandler(sys_handler)
|
|
else:
|
|
new_logger.warning("Journal socket does not exist. Logs will not be processed by syslog.")
|
|
|
|
return new_logger
|