logzero
Robust and effective logging for Python 2 and 3.
- Documentation: https://logzero.readthedocs.io
- GitHub: https://github.com/metachris/logzero
Features
- Easy logging to console and/or (rotating) file.
- Provides a fully configured standard Python logger object.
- Pretty formatting, including level-specific colors in the console.
- Windows color output supported by colorama
- Robust against str/bytes encoding problems, works with all kinds of character encodings and special characters.
- Multiple loggers can write to the same logfile (also across multiple Python files).
- Global default logger with logzero.logger and custom loggers with logzero.setup_logger(..).
- Compatible with Python 2 and 3.
- All contained in a single file.
- Licensed under the MIT license.
- Heavily inspired by the Tornado web framework.
Example Usage
from logzero import logger
logger.debug("hello")
logger.info("info")
logger.warn("warn")
logger.error("error")
# This is how you'd log an exception
try:
raise Exception("this is a demo exception")
except Exception as e:
logger.exception(e)
Here are more examples which show how to use logfiles, custom formatters and setting a minimum loglevel:
import logging
import logzero
from logzero import logger
# This log message goes to the console
logger.debug("hello")
# Set a minimum log level
logzero.loglevel(logging.INFO)
# Set a logfile (all future log messages are also saved there)
logzero.logfile("/tmp/logfile.log")
# You can also set a different loglevel for the file handler
logzero.logfile("/tmp/logfile.log", loglevel=logging.ERROR)
# Set a rotating logfile (replaces the previous logfile handler)
logzero.logfile("/tmp/rotating-logfile.log", maxBytes=1000000, backupCount=3)
# Disable logging to a file
logzero.logfile(None)
# Set a custom formatter
formatter = logging.Formatter('%(name)s - %(asctime)-15s - %(levelname)s: %(message)s');
logzero.formatter(formatter)
# Log some variables
logger.info("var1: %s, var2: %s", var1, var2)
Take a look at the documentation for more information and examples:
- Documentation: https://logzero.readthedocs.io.
Installation
Install logzero with pip:
$ pip install -U logzero
On openSUSE you can install the current version from repos: python2-logzero, python3-logzero.
Changelog
See the changelog here: https://github.com/metachris/logzero/blob/master/HISTORY.rst
Future Features & Ideas
- Decorator for logging function calls
- Easier usage of custom log handlers (currently works like this)
- JSON output (a la 12 factor app)
- Send logs to remote log collector (maybe)
- Structured logging a la https://structlog.readthedocs.io/en/stable/index.html (maybe)
TODO
- Travis CI: pypy3 gives error RuntimeError: Python 3.3 or later is required (see job 255217329)
- Tests
- Custom handlers and reconfiguration
- Strange behaviour: py.test with default logger - capturing err does not work if the logger is setup initially in logzero. Only works when setup from the py script.
Related Projects
- https://12factor.net/logs
- Log collectors: fluentd, logstash, etc.
- https://structlog.readthedocs.io/en/stable/why.html
Notes: How to release a new version
via https://cookiecutter-pypackage.readthedocs.io/en/latest/pypi_release_checklist.html
# Run the tests
py.test
make lint
tox
# Update history
vi HISTORY.rst
git add HISTORY.rst
git commit -m "Changelog for upcoming release 0.1.1."
# Update version
bumpversion minor
# Push
git push && git push --tags
Credits
This package was created with Cookiecutter and the audreyr/cookiecutter-pypackage project template.