我们从Python开源项目中,提取了以下6个代码示例,用于说明如何使用logging.BASIC_FORMAT。
def test_no_kwargs(self): logging.basicConfig() # handler defaults to a StreamHandler to sys.stderr self.assertEqual(len(logging.root.handlers), 1) handler = logging.root.handlers[0] self.assertIsInstance(handler, logging.StreamHandler) self.assertEqual(handler.stream, sys.stderr) formatter = handler.formatter # format defaults to logging.BASIC_FORMAT self.assertEqual(formatter._style._fmt, logging.BASIC_FORMAT) # datefmt defaults to None self.assertIsNone(formatter.datefmt) # style defaults to % self.assertIsInstance(formatter._style, logging.PercentStyle) # level is not explicitly set self.assertEqual(logging.root.level, self.original_logging_level)
def formatMessage(self, record): level_color = "0" text_color = "0" fmt = "" if record.levelno <= logging.DEBUG: fmt = "\033[0;37m" + logging.BASIC_FORMAT + "\033[0m" elif record.levelno <= logging.INFO: level_color = "1;36" lmsg = record.message.lower() if self.GREEN_RE.search(lmsg): text_color = "1;32" elif record.levelno <= logging.WARNING: level_color = "1;33" elif record.levelno <= logging.CRITICAL: level_color = "1;31" if not fmt: fmt = "\033[" + level_color + \ "m%(levelname)s\033[0m:%(name)s:\033[" + text_color + \ "m%(message)s\033[0m" return fmt % record.__dict__
def handlerAndBytesIO(): """ Construct a 2-tuple of C{(StreamHandler, BytesIO)} for testing interaction with the 'logging' module. @return: handler and io object @rtype: tuple of L{StreamHandler} and L{io.BytesIO} """ output = BytesIO() stream = output template = py_logging.BASIC_FORMAT if _PY3: stream = TextIOWrapper(output, encoding="utf-8", newline="\n") formatter = py_logging.Formatter(template) handler = py_logging.StreamHandler(stream) handler.setFormatter(formatter) return handler, output
def __init__(self, *args, **kwargs): self._file_handler = None self._file_formatter = kwargs.pop( 'file_formatter', logging.BASIC_FORMAT ) super(FileLogger, self).__init__(*args, **kwargs)