31584f58a5f22c9312345973e5ad11ccb843395b
[pti/o2.git] / o2common / helper / o2logging.py
1 # Copyright (C) 2021 Wind River Systems, Inc.
2 #
3 #  Licensed under the Apache License, Version 2.0 (the "License");
4 #  you may not use this file except in compliance with the License.
5 #  You may obtain a copy of the License at
6 #
7 #      http://www.apache.org/licenses/LICENSE-2.0
8 #
9 #  Unless required by applicable law or agreed to in writing, software
10 #  distributed under the License is distributed on an "AS IS" BASIS,
11 #  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 #  See the License for the specific language governing permissions and
13 #  limitations under the License.
14
15 import logging
16 import logging.config
17 import logging.handlers
18 import os
19 import yaml
20
21
22 def get_logger(name=None):
23     CONFIG_FILE = os.environ.get(
24         "LOGGING_CONFIG_FILE", "/etc/o2/log.yaml")
25     if os.path.exists(CONFIG_FILE):
26         with open(file=CONFIG_FILE, mode='r', encoding="utf-8") as file:
27             config_yaml = yaml.load(stream=file, Loader=yaml.FullLoader)
28         logging.config.dictConfig(config=config_yaml)
29
30     logger = logging.getLogger(name)
31
32     # override logging level
33     LOGGING_CONFIG_LEVEL = os.environ.get("LOGGING_CONFIG_LEVEL", None)
34     if LOGGING_CONFIG_LEVEL:
35         logger.setLevel(LOGGING_CONFIG_LEVEL)
36     return logger