2 Copyright (c) 2019 Intel Corporation
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
8 http://www.apache.org/licenses/LICENSE-2.0
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
23 _DEFAULT_LOGGER_NAME = 'accuracy_checker'
24 _DEFAULT_LOG_FILE = 'accuracy_checker.log'
26 PRINT_INFO = logging.INFO + 5
27 logging.addLevelName(PRINT_INFO, "PRINT_INFO")
29 _LOG_LEVEL_ENVIRON = "ACCURACY_CHECKER_LOG_LEVEL"
30 _LOGGING_LEVEL = logging.getLevelName(os.environ.get(_LOG_LEVEL_ENVIRON, PRINT_INFO))
33 class LoggingFormatter(logging.Formatter):
34 def format(self, record: logging.LogRecord):
35 if record.levelno == PRINT_INFO:
37 return super().format(record)
40 class ConsoleHandler(logging.StreamHandler):
41 def __init__(self, default_stream=sys.stdout):
42 super().__init__(default_stream)
43 self.default_stream = default_stream
44 self.err_stream = sys.stderr
46 def emit(self, record):
47 if record.levelno >= logging.WARNING:
48 self.stream = self.err_stream
50 self.stream = self.default_stream
54 _LOGGING_CONFIGURATION = {
56 _DEFAULT_LOGGER_NAME: {
57 'handlers': ['console'],
58 'level': _LOGGING_LEVEL,
63 'disable_existing_loggers': False,
66 '()': LoggingFormatter,
67 'format': '%(asctime)s %(name)s %(levelname)s: %(message)s',
71 'format': '%(asctime)s %(name)s %(levelname)s: %(message)s'
78 'formatter': 'default',
83 logging.config.dictConfig(_LOGGING_CONFIGURATION)
85 _default_logger = logging.getLogger(_DEFAULT_LOGGER_NAME)
88 def _warning_handler(message, category, filename, line_number):
89 s = warnings.formatwarning(message, category, filename, line_number)
90 _default_logger.warning(s)
93 warnings.showwarning = _warning_handler
96 def get_logger(logger_name: str):
97 if logger_name.startswith(_DEFAULT_LOGGER_NAME):
98 return _default_logger.getChild(logger_name)
99 return logging.getLogger(logger_name)
102 def error(msg, *args, **kwargs):
103 _default_logger.error(msg, *args, **kwargs)
106 def warning(msg, *args, raise_warning=True, **kwargs):
110 _default_logger.warning(msg, *args, **kwargs)
113 def info(msg, *args, **kwargs):
114 _default_logger.info(msg, *args, **kwargs)
117 def debug(msg, *args, **kwargs):
118 _default_logger.debug(msg, *args, **kwargs)
121 def print_info(msg, *args, **kwargs):
122 _default_logger.log(PRINT_INFO, msg, *args, **kwargs)
125 def add_file_handler(file_name):
126 file_info_handler_config = {
127 'level': 'PRINT_INFO',
128 'class': 'logging.handlers.WatchedFileHandler',
129 'formatter': 'default',
130 'filename': file_name
132 _LOGGING_CONFIGURATION['handlers']['file_info'] = file_info_handler_config
133 _LOGGING_CONFIGURATION['loggers'][_DEFAULT_LOGGER_NAME]['handlers'].append('file_info')
134 logging.config.dictConfig(_LOGGING_CONFIGURATION)