from logzero import logger
logger.debug("hello")
logger.info("info")
logger.warning("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)
# JSON logging
import logzero
logzero.json()
logger.info("JSON test")
# Start writing into a logfile
logzero.logfile("/tmp/logzero-demo.log")
# Set a minimum loglevel
logzero.loglevel(logzero.WARNING)
Exceptions logged with logger.exception(e) have these additional JSON fields:
{
"levelname": "ERROR",
"levelno": 40,
"message": "this is a demo exception",
"exc_info": "Traceback (most recent call last):\n File \"_tests/test.py\", line 15, in test_this\n raise Exception(\"this is a demo exception\")\nException: this is a demo exception"
}
Take a look at the documentation for more information and examples:
$ git clone https://github.com/metachris/logzero.git
$ cd logzero
# Activate virtualenv
$ python3 -m venv venv
$ . venv/bin/activate
# Install main and dev dependencies
$ pip install -e .
$ pip install -r requirements_dev.txt
# Run the tests
$ make test
# Run the linter
$ make lint
# Generate the docs (will auto-open in Chrome)
$ make docs
# You can enable watching mode to automatically rebuild on changes:
$ make servedocs
To test with Python 2.7, you can use Docker:
docker run --rm -it -v /Users/chris/stream/logzero:/mnt python:2.7 /bin/bash
Now you have a shell with the current directory mounted into /mnt/ inside the container.
Color output now works in Windows (supported by colorama)
1.2.1 (2017-07-09)
Logfiles with custom loglevels (eg. stream handler with DEBUG and
file handler with ERROR).
1.2.0 (2017-07-05)
Way better API for configuring the default logger with logzero.loglevel(..), logzero.logfile(..), etc.
Built-in rotating logfile support.
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")
# 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)
1.1.2 (2017-07-04)
Better reconfiguration of handlers, doesn't remove custom handlers
anymore
1.1.0 (2017-07-03)
Bugfix: Disabled color logging to logfile
1.1.0 (2017-07-02)
Global default logger instance (logzero.logger)
Ability to reconfigure the default logger with (logzero.setup_default_logger(..))
More tests
More documentation
1.0.0 (2017-06-27)
Cleanup and documentation
0.2.0 (2017-06-12)
Working logzero package with code and tests
0.1.0 (2017-06-12)
First release on PyPI.
Alternatives
Use instead of logzero: tools that do the same job, closest first.
Use together with logzero: extensions, drivers, servers and companion tools.
concurrent-log-handlerPythie’s Index: 50RotatingFileHandler replacement with concurrency, gzip and Windows support. Size and time based rotation.3M downloads/mo