项目作者: zobayer1

项目描述 :
Python logging extensions
高级语言: Python
项目地址: git://github.com/zobayer1/logging-extras.git
创建时间: 2021-05-28T22:56:18Z
项目社区:https://github.com/zobayer1/logging-extras

开源协议:MIT License

下载


LOGGING EXTRAS

A collection of various python logging extensions.

Python
PyPi Publish
Python Builds
codecov
pre-commit
Code style: black
Documentation Status
License: MIT

Documentation

https://logging-extras.readthedocs.io/en/latest/

Installation

Install logging-extras using pip

  1. pip install logging-extras

Alternatively, download the latest binary or source package from github

Install wheel package with pip:

  1. pip install logging_extras-{tags}.whl

Install source package as editable:

  1. tar -xf logging-extras-{tags}.tar.gz
  2. cd logging-extras-{tags}
  3. pip install -e .

Please refer to documentation pages for available modules.

Module Index

config.YAMLConfig

YAMLConfig class can be used for loading YAML files with custom tags. This class adds a custom envvar tag to native YAML parser which is used to evaluate environment variables. Supports one or more environment variables in the form of ${VARNAME} or ${VARNAME:DEFAULT} within a string. If no default value is specified, empty string is used. Default values can only be treated as plain strings. YAMLConfig can also expand ~ or ~username just like shells do, either directly hardcoded in YAML file or passed through environment variables.

Example configuration:

File: logging.yaml

  1. version: 1
  2. formatters:
  3. simple:
  4. format: '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
  5. handlers:
  6. console:
  7. class: logging.StreamHandler
  8. formatter: simple
  9. stream: ext://sys.stdout
  10. file_handler:
  11. class: logging.FileHandler
  12. filename: ${LOGGING_ROOT:.}/${LOG_FILENAME}
  13. formatter: simple
  14. loggers:
  15. test_logger:
  16. level: DEBUG
  17. handlers:
  18. - file_handler
  19. propagate: no
  20. root:
  21. level: NOTSET
  22. handlers:
  23. - console

Note: Ignore the backslashes as markdown must display those escape characters.

Example Usage

File: test_logger.py

  1. import logging
  2. from logging_.config import YAMLConfig
  3. with open("logging.yaml", "r") as config_file:
  4. YAMLConfig(config_file.read(), silent=True)
  5. # alternatively, you can use
  6. # YAMLConfig.from_file("logging.yaml", silent=True)
  7. logger = logging.getLogger("test_logger")
  8. logger.debug("This is a debug log")
  9. logger.info("This is an info log")
  10. logger.warning("This is an warning log")
  11. logger.error("This is an error log")
  12. logger.critical("This is a critical log")

Note: An (optional) explicit silent=True flag must be set to suppress any file or parsing related exceptions to be thrown.

handlers.QueueListenerHandler

A simple QueueHandler subclass implementation utilizing QueueListener for configured handlers. This is helpful for detaching the logger handlers from the main threads, which reduces the risk of getting blocked, for example, when using slower handlers such as smtp, file, or socket handlers.

Example configuration:

File: logging.yaml

  1. version: 1
  2. objects:
  3. queue:
  4. class: queue.Queue
  5. maxsize: 1000
  6. formatters:
  7. simple:
  8. format: '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
  9. handlers:
  10. console:
  11. class: logging.StreamHandler
  12. formatter: simple
  13. stream: ext://sys.stdout
  14. file_handler:
  15. class: logging.FileHandler
  16. filename: test_logger.log
  17. formatter: simple
  18. queue_handler:
  19. class: logging_.handlers.QueueListenerHandler
  20. handlers:
  21. - cfg://handlers.console
  22. - cfg://handlers.file_handler
  23. queue: cfg://objects.queue
  24. loggers:
  25. test_logger:
  26. level: DEBUG
  27. handlers:
  28. - queue_handler
  29. propagate: no
  30. root:
  31. level: NOTSET
  32. handlers:
  33. - console

Note: A queue object must be passed since the handler does not set a default queue implementation. Set maxsize: -1 to make the queue unlimited.

Example Usage

File: test_logger.py

  1. import logging.config
  2. import yaml
  3. with open("logging.yaml", "r") as config_file:
  4. logging_config = yaml.safe_load(config_file.read())
  5. logging.config.dictConfig(logging_config)
  6. logger = logging.getLogger("test_logger")
  7. logger.debug("This is a debug log")
  8. logger.info("This is an info log")
  9. logger.warning("This is an warning log")
  10. logger.error("This is an error log")
  11. logger.critical("This is a critical log")

Development

Additional development and documentation dependencies can be installed using extras. It is recommended to use a virtualenv.

Use Pre-Commit Hooks

Install pre-commit hooks and dependencies:

  1. pip install pre-commit
  2. pre-commit install
  3. pre-commit autoupdate
  4. pre-commit run --all-files

Run Tests

Run tests from the source with Pytest:

  1. pip install -e .[dev]
  2. pytest -s

Generate Documentation

Generate documentation from the source with Sphinx:

  1. pip install -e .[doc]
  2. cd docs
  3. mkdir -p _static _templates
  4. make html
  5. python -m http.server --directory build/html

No requirements.txt File

This is a python library package that is compatible with a wide range of Python versions. It does not make much sense to pin dependency versions in a traditional requirements.txt file. Instead, this project utilizes modern python packaging paradigms with pyproject.toml and setup.cfg files. However, sometimes some IDEs (i.e. PyCharm) cannot resolve dependencies without a requirements.txt file. To generate a requirements.txt file, simply run the following command within your venv:

  1. pip freeze > requirements.txt

Create Distribution Packages

To create a source and wheel distribution, run:

  1. git clone git@github.com:zobayer1/logging-extras.git
  2. python -m pip install wheel
  3. python setup.py clean sdist bdist_wheel

Note: This project uses setuptools-scm to generate build versions from git tags. Build system will raise errors if you are trying to build packages outside a git repo.