项目作者: metachris

项目描述 :
Robust and effective logging for Python 2 and 3.
高级语言: Python
项目地址: git://github.com/metachris/logzero.git
创建时间: 2017-06-12T19:32:26Z
项目社区:https://github.com/metachris/logzero

开源协议:MIT License

下载


logzero

Build status for master branch
Documentation Status
Latest version on PyPi
Anaconda-Server Badge
Downloads

Robust and effective logging for Python 2 and 3.

Logo

Features

  • Easy logging to console and/or (rotating) file.
  • Provides a fully configured standard Python logger object.
  • JSON logging (with integrated python-json-logger)
  • Pretty formatting, including level-specific colors in the console.
  • No dependencies
  • 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 and processes).
  • 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.

Installation:

  1. python -m pip install logzero

Example Usage

  1. from logzero import logger
  2. logger.debug("hello")
  3. logger.info("info")
  4. logger.warning("warn")
  5. logger.error("error")
  6. # This is how you'd log an exception
  7. try:
  8. raise Exception("this is a demo exception")
  9. except Exception as e:
  10. logger.exception(e)
  11. # JSON logging
  12. import logzero
  13. logzero.json()
  14. logger.info("JSON test")
  15. # Start writing into a logfile
  16. logzero.logfile("/tmp/logzero-demo.log")
  17. # Set a minimum loglevel
  18. logzero.loglevel(logzero.WARNING)

This is the output:

demo-output

Note: You can find more examples in the documentation: https://logzero.readthedocs.io

JSON logging

JSON logging can be enabled for the default logger with logzero.json(), or with setup_logger(json=True) for custom loggers:

  1. >>> logzero.json()
  2. >>> logger.info("test")
  3. {"asctime": "2020-10-21 10:42:45,808", "filename": "<stdin>", "funcName": "<module>", "levelname": "INFO", "levelno": 20, "lineno": 1, "module": "<stdin>", "message": "test", "name": "logzero_default", "pathname": "<stdin>", "process": 76179, "processName": "MainProcess", "threadName": "MainThread"}
  4. >>> my_logger = setup_logger(json=True)
  5. >>> my_logger.info("test")
  6. {"asctime": "2020-10-21 10:42:45,808", "filename": "<stdin>", "funcName": "<module>", "levelname": "INFO", "levelno": 20, "lineno": 1, "module": "<stdin>", "message": "test", "name": "logzero_default", "pathname": "<stdin>", "process": 76179, "processName": "MainProcess", "threadName": "MainThread"}

The logged JSON object has these fields:

  1. {
  2. "asctime": "2020-10-21 10:43:40,765",
  3. "filename": "test.py",
  4. "funcName": "test_this",
  5. "levelname": "INFO",
  6. "levelno": 20,
  7. "lineno": 9,
  8. "module": "test",
  9. "message": "info",
  10. "name": "logzero",
  11. "pathname": "_tests/test.py",
  12. "process": 76204,
  13. "processName": "MainProcess",
  14. "threadName": "MainThread"
  15. }

Exceptions logged with logger.exception(e) have these additional JSON fields:

  1. {
  2. "levelname": "ERROR",
  3. "levelno": 40,
  4. "message": "this is a demo exception",
  5. "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"
  6. }

Take a look at the documentation for more information and examples:

Installation

Install logzero with pip:

  1. python -m pip install logzero

Here’s how you setup a virtualenv and download and run the demo:

  1. # Create and activate a virtualenv in ./venv/
  2. python3 -m venv venv
  3. . venv/bin/activate
  4. # Install logzero
  5. python -m pip install logzero
  6. # Download and run demo.py
  7. wget https://raw.githubusercontent.com/metachris/logzero/master/examples/demo.py
  8. python demo.py

If you don’t have pip installed, this Python installation guide can guide
you through the process.

Alternatively, if you use the Anaconda distribution:

  1. $ conda config --add channels conda-forge
  2. $ conda install logzero

You can also install logzero from the public Github repo:

  1. $ git clone https://github.com/metachris/logzero.git
  2. $ cd logzero
  3. $ python setup.py install

Contributors


Development

Getting started

  1. $ git clone https://github.com/metachris/logzero.git
  2. $ cd logzero
  3. # Activate virtualenv
  4. $ python3 -m venv venv
  5. $ . venv/bin/activate
  6. # Install main and dev dependencies
  7. $ pip install -e .
  8. $ pip install -r requirements_dev.txt
  9. # Run the tests
  10. $ make test
  11. # Run the linter
  12. $ make lint
  13. # Generate the docs (will auto-open in Chrome)
  14. $ make docs
  15. # You can enable watching mode to automatically rebuild on changes:
  16. $ make servedocs

To test with Python 2.7, you can use Docker:

  1. 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.

Notes


Changelog

See the changelog here: https://github.com/metachris/logzero/blob/master/HISTORY.md

Feedback

All kinds of feedback and contributions are welcome.

logo