From 4c7fe7c690292a2878792a9237f9a0c3be4ef901 Mon Sep 17 00:00:00 2001 From: Bin Yang Date: Fri, 18 Nov 2022 12:11:16 +0800 Subject: [PATCH] Fix logging level setting issue Issue-ID: INF-366 Signed-off-by: Bin Yang Change-Id: I8d87d1418bb39748f79e40d54fbddb79f0480be4 --- configs/log.yaml | 30 +++++++++++++++--------------- o2common/helper/__init__.py | 7 +++++++ o2common/helper/o2logging.py | 24 ++++++++++++++++-------- 3 files changed, 38 insertions(+), 23 deletions(-) diff --git a/configs/log.yaml b/configs/log.yaml index d6fa592..24a9ba4 100644 --- a/configs/log.yaml +++ b/configs/log.yaml @@ -19,26 +19,26 @@ loggers: root: handlers: [console_handler, file_handler] level: "WARNING" - propagate: False - o2common: - handlers: [console_handler, file_handler] - level: "WARNING" - propagate: False - o2ims: - handlers: [console_handler, file_handler] - level: "DEBUG" - propagate: False - o2dms: - handlers: [console_handler, file_handler] - level: "DEBUG" - propagate: False + # propagate: False + # o2common: + # handlers: [console_handler, file_handler] + # level: "WARNING" + # propagate: True + # o2ims: + # handlers: [console_handler, file_handler] + # level: "WARNING" + # propagate: True + # o2dms: + # handlers: [console_handler, file_handler] + # level: "WARNING" + # propagate: True handlers: console_handler: - level: "DEBUG" + level: "NOTSET" class: "logging.StreamHandler" formatter: "standard" file_handler: - level: "DEBUG" + level: "NOTSET" class: "logging.handlers.RotatingFileHandler" filename: "/var/log/orano2/o2.log" formatter: "standard" diff --git a/o2common/helper/__init__.py b/o2common/helper/__init__.py index 813897e..c7f15c5 100644 --- a/o2common/helper/__init__.py +++ b/o2common/helper/__init__.py @@ -11,3 +11,10 @@ # 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. + + +from o2common.helper.o2logging import configure_logger + + +# make sure init logger at the first chance +configure_logger() diff --git a/o2common/helper/o2logging.py b/o2common/helper/o2logging.py index 31584f5..e1196d2 100644 --- a/o2common/helper/o2logging.py +++ b/o2common/helper/o2logging.py @@ -20,17 +20,25 @@ import yaml def get_logger(name=None): - CONFIG_FILE = os.environ.get( - "LOGGING_CONFIG_FILE", "/etc/o2/log.yaml") - if os.path.exists(CONFIG_FILE): - with open(file=CONFIG_FILE, mode='r', encoding="utf-8") as file: - config_yaml = yaml.load(stream=file, Loader=yaml.FullLoader) - logging.config.dictConfig(config=config_yaml) + # CONFIG_FILE = os.environ.get( + # "LOGGING_CONFIG_FILE", "/etc/o2/log.yaml") + # if os.path.exists(CONFIG_FILE): + # with open(file=CONFIG_FILE, mode='r', encoding="utf-8") as file: + # config_yaml = yaml.load(stream=file, Loader=yaml.FullLoader) + # logging.config.dictConfig(config=config_yaml) logger = logging.getLogger(name) - - # override logging level + # override root logger's logging level LOGGING_CONFIG_LEVEL = os.environ.get("LOGGING_CONFIG_LEVEL", None) if LOGGING_CONFIG_LEVEL: logger.setLevel(LOGGING_CONFIG_LEVEL) return logger + + +def configure_logger(): + CONFIG_FILE = os.environ.get( + "LOGGING_CONFIG_FILE", "/etc/o2/log.yaml") + if os.path.exists(CONFIG_FILE): + with open(file=CONFIG_FILE, mode='r', encoding="utf-8") as file: + config_yaml = yaml.load(stream=file, Loader=yaml.FullLoader) + logging.config.dictConfig(config=config_yaml) -- 2.16.6