Package: logwood
This is a simple, but fast logging library. We traded features for speed.Logwood is only tested with Python 3.5. There are no plans to support older versions.importlogwoodfromlogwood.handlers.stderrimportColoredStderrHandlerlogwood.basic_config(level=logwood.INFO,handlers=[ColoredStderrHandler()])logger=logwood.get_logger('LoggerName')logger.info('Just FYI')logger.warning('Six times seven is{:d}.',42)FeaturesWhile logwood is very lightweight (see the Compatibility section below) it still gives you a few nice features
out of the box:Chunked syslog handler. Splits long messages and writes them to syslog piece by piece. Useful e.g. to work
around maximum UDP packet size (logwood.handlers.chunked.ChunkedSysLogHandler).Alternative syslog handler that uses thestandard syslog moduleto emit logs to local syslog. Benchmarks show this to be faster than connecting and writing to asocketdirectly (logwood.handlers.syslog.SysLogLibHandler).Threaded handler: executes any underlying handler in a separate thread to avoid blocking the main thread
(logwood.handlers.threaded.ThreadedHandler).Colored logs on stderr (logwood.handlers.stderr.ColoredStderrHandler).Compatibility withloggingLogwood API is somewhat similar to the standardlogging, but we’ve made a few opinionated design decisions
that do not make this a drop-in replacement:Loggers do not form a tree hierarchy. There are no child loggers and no handler or configuration inheritance.
We found that such features make theloggingmodule very slow.Logging config cannot be changed after loggers are created, and conversely no loggers may be created until
logging is configured. Again, this is to make loggers as simple as possible.While %-style formatting is supported for historical reasons, logwood prefers the more powerfulstr.format.
It tries to guess which formatting style you’re using but defaults tostr.format(curly braces).
This feature will be removed in a future major release and onlystr.formatwill be supported going forward.logging->logwoodbridgeCalllogwood.compat.redirect_standard_logging()to configure the standardloggingmodule to send
all messages tologwoodfor handling. You can use this to run alogwood-based application that
nevertheless works with any 3rd-party libraries usinglogging.py.test fixturesFor testing convenience there are two fixtures prepared:configure_and_reset_logwoodandlogwood_handler_mock.configure_and_reset_logwoodis an autouse fixture, i.e. it is run before every test in projects that use logwood.
It callslogwood.basic_configto set a stderr handler so you don’t have to care about logwood configuration
in tests, and you can see logs when your test fails.
Thelogwood_handler_mockfixture is a mock handler. This is useful for testing how your code uses logwood.
It stores all messages in a dict of lists so you can easily find your messages. To identify the level you can use
either strings (upper/lowercase) or constants from logging or logwood:importlogwooddefsome_func():logger=logwood.get_logger('test_logger')logger.warning('Something is wrong with id{}',42)deftest_logwood(logwood_handler_mock):some_func()assert'Something is wrong with id 42'inlogwood_handler_mock['WARNING']assertnotlogwood_handler_mock['error']assertany(m.startswith('Something')forminlogwood_handler_mock[logwood.WARNING])