コード例 #1
0
ファイル: tests.py プロジェクト: xolox/python-coloredlogs
 def test_plain_text_output_format(self):
     """Inspect the plain text output of coloredlogs."""
     logger = VerboseLogger(random_string(25))
     stream = StringIO()
     install(level=logging.NOTSET, logger=logger, stream=stream)
     # Test that filtering on severity works.
     logger.setLevel(logging.INFO)
     logger.debug("No one should see this message.")
     assert len(stream.getvalue().strip()) == 0
     # Test that the default output format looks okay in plain text.
     logger.setLevel(logging.NOTSET)
     for method, severity in ((logger.debug, 'DEBUG'),
                              (logger.info, 'INFO'),
                              (logger.verbose, 'VERBOSE'),
                              (logger.warning, 'WARNING'),
                              (logger.error, 'ERROR'),
                              (logger.critical, 'CRITICAL')):
         # Prepare the text.
         text = "This is a message with severity %r." % severity.lower()
         # Log the message with the given severity.
         method(text)
         # Get the line of output generated by the handler.
         output = stream.getvalue()
         lines = output.splitlines()
         last_line = lines[-1]
         assert text in last_line
         assert severity in last_line
         assert PLAIN_TEXT_PATTERN.match(last_line)
コード例 #2
0
ファイル: tests.py プロジェクト: ddboline/python-coloredlogs
 def test_plain_text_output_format(self):
     """Inspect the plain text output of coloredlogs."""
     logger = VerboseLogger(random_string(25))
     stream = StringIO()
     install(level=logging.NOTSET, logger=logger, stream=stream)
     # Test that filtering on severity works.
     logger.setLevel(logging.INFO)
     logger.debug("No one should see this message.")
     assert len(stream.getvalue().strip()) == 0
     # Test that the default output format looks okay in plain text.
     logger.setLevel(logging.NOTSET)
     for method, severity in ((logger.debug, 'DEBUG'), (logger.info,
                                                        'INFO'),
                              (logger.verbose, 'VERBOSE'), (logger.warning,
                                                            'WARN'),
                              (logger.error, 'ERROR'), (logger.critical,
                                                        'CRITICAL')):
         # Prepare the text.
         text = "This is a message with severity %r." % severity.lower()
         # Log the message with the given severity.
         method(text)
         # Get the line of output generated by the handler.
         output = stream.getvalue()
         lines = output.splitlines()
         last_line = lines[-1]
         assert text in last_line
         assert severity in last_line
         assert PLAIN_TEXT_PATTERN.match(last_line)
コード例 #3
0
ファイル: log.py プロジェクト: hofbi/dev-sync
def init_logging(logfile: Path) -> VerboseLogger:
    dev_sync_logger = VerboseLogger(NAME)

    logfile.parent.mkdir(exist_ok=True, parents=True)

    coloredlogs.install(level="DEBUG",
                        milliseconds=True,
                        logger=dev_sync_logger)
    dev_sync_logger.setLevel(logging.DEBUG)

    fh = TimedRotatingFileHandler(logfile, when="MIDNIGHT")
    fh.setFormatter(logging.Formatter(coloredlogs.DEFAULT_LOG_FORMAT))
    dev_sync_logger.addHandler(fh)

    return dev_sync_logger
コード例 #4
0
 def test_plain_text_output_format(self):
     """Inspect the plain text output of coloredlogs."""
     logger = VerboseLogger(random_string(25))
     stream = StringIO()
     install(level=logging.NOTSET, logger=logger, stream=stream)
     # Test that filtering on severity works.
     logger.setLevel(logging.INFO)
     logger.debug("No one should see this message.")
     assert len(stream.getvalue().strip()) == 0
     # Test that the default output format looks okay in plain text.
     logger.setLevel(logging.NOTSET)
     for method, severity in ((logger.debug, 'DEBUG'),
                              (logger.info, 'INFO'),
                              (logger.verbose, 'VERBOSE'),
                              (logger.warning, 'WARNING'),
                              (logger.error, 'ERROR'),
                              (logger.critical, 'CRITICAL')):
         # XXX Workaround for a regression in Python 3.7 caused by the
         # Logger.isEnabledFor() method using stale cache entries. If we
         # don't clear the cache then logger.isEnabledFor(logging.DEBUG)
         # returns False and no DEBUG message is emitted.
         try:
             logger._cache.clear()
         except AttributeError:
             pass
         # Prepare the text.
         text = "This is a message with severity %r." % severity.lower()
         # Log the message with the given severity.
         method(text)
         # Get the line of output generated by the handler.
         output = stream.getvalue()
         lines = output.splitlines()
         last_line = lines[-1]
         assert text in last_line
         assert severity in last_line
         assert PLAIN_TEXT_PATTERN.match(last_line)
コード例 #5
0
            return ansi_text(message, color=colorname, bold=bold)
        else:
            return message

if __name__ == '__main__':

    # If my verbose logger is installed, we'll use that for the demo.
    try:
        from verboselogs import VerboseLogger as DemoLogger
    except ImportError:
        from logging import getLogger as DemoLogger

    # Initialize the logger.
    logger = DemoLogger('coloredlogs-demo')
    logger.addHandler(ColoredStreamHandler())
    logger.setLevel(logging.DEBUG)

    # Print some examples with different timestamps.
    for level in ['debug', 'verbose', 'info', 'warn', 'error', 'critical']:
        if hasattr(logger, level):
            getattr(logger, level)("message with level %r", level)
            time.sleep(1)

    # Show how exceptions are logged.
    try:
        class RandomException(Exception):
            pass
        raise RandomException, "Something went horribly wrong!"
    except Exception, e:
        logger.exception(e)
    logger.info("Done, exiting ..")
コード例 #6
0
        else:
            return message


if __name__ == '__main__':

    # If my verbose logger is installed, we'll use that for the demo.
    try:
        from verboselogs import VerboseLogger as DemoLogger
    except ImportError:
        from logging import getLogger as DemoLogger

    # Initialize the logger.
    logger = DemoLogger('coloredlogs-demo')
    logger.addHandler(ColoredStreamHandler())
    logger.setLevel(logging.DEBUG)

    # Print some examples with different timestamps.
    for level in ['debug', 'verbose', 'info', 'warn', 'error', 'critical']:
        if hasattr(logger, level):
            getattr(logger, level)("message with level %r", level)
            time.sleep(1)

    # Show how exceptions are logged.
    try:

        class RandomException(Exception):
            pass

        raise RandomException, "Something went horribly wrong!"
    except Exception, e: