Refactor SphinxLoggerAdapter

This commit is contained in:
Takeshi KOMIYA 2016-12-25 00:39:54 +09:00
parent aa65a19466
commit 25a078655b
2 changed files with 21 additions and 13 deletions

View File

@ -124,25 +124,13 @@ class sphinx_domains(object):
class WarningStream(object):
level_mapping = {
'DEBUG': logger.debug,
'INFO': logger.info,
'WARNING': logger.warning,
'ERROR': logger.error,
'SEVERE': logger.critical,
}
def write(self, text):
matched = report_re.search(text)
if not matched:
logger.warning(text.rstrip("\r\n"))
else:
location, type, level, message = matched.groups()
if type in self.level_mapping:
logger_method = self.level_mapping.get(type)
logger_method(message, location=location)
else:
logger.warning(text.rstrip("\r\n"))
logger.log(type, message, location=location)
class LoggingReporter(Reporter):

View File

@ -31,6 +31,18 @@ if False:
VERBOSE = 15
DEBUG2 = 5
LEVEL_NAMES = defaultdict(lambda: logging.WARNING) # type: Dict[str, int]
LEVEL_NAMES.update({
'CRITICAL': logging.CRITICAL,
'SEVERE': logging.CRITICAL,
'ERROR': logging.ERROR,
'WARNING': logging.WARNING,
'INFO': logging.INFO,
'VERBOSE': VERBOSE,
'DEBUG': logging.DEBUG,
'DEBUG2': DEBUG2,
})
VERBOSITY_MAP = defaultdict(lambda: 0) # type: Dict[int, int]
VERBOSITY_MAP.update({
0: logging.INFO,
@ -97,6 +109,14 @@ class SphinxLoggerAdapter(logging.LoggerAdapter):
self.warning(message, **kwargs)
def log(self, level, msg, *args, **kwargs):
# type: (Union[int, str], unicode, Any, Any) -> None
if isinstance(level, int):
super(SphinxLoggerAdapter, self).log(level, msg, *args, **kwargs)
else:
levelno = LEVEL_NAMES.get(level)
super(SphinxLoggerAdapter, self).log(levelno, msg, *args, **kwargs)
def verbose(self, msg, *args, **kwargs):
# type: (unicode, Any, Any) -> None
self.log(VERBOSE, msg, *args, **kwargs)