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