Package: logging-extras
LOGGING EXTRASA collection of various python logging extensions.Documentationhttps://logging-extras.readthedocs.io/en/latest/InstallationInstalllogging-extrasusing pippip install logging-extrasAlternatively, download the latest binary or source package fromgithubInstall wheel package withpip:pip install logging_extras-{tags}.whlInstall source package as editable:tar -xf logging-extras-{tags}.tar.gz
cd logging-extras-{tags}
pip install -e .Please refer to documentation pages for available modules.Module Indexconfig.YAMLConfigYAMLConfig 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~usernamejust like shells do, either directly hardcoded in YAML file or passed through environment variables.Example configuration:File:logging.yamlversion: 1
formatters:
  simple:
    format: '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
handlers:
  console:
    class: logging.StreamHandler
    formatter: simple
    stream: ext://sys.stdout
  file_handler:
    class: logging.FileHandler
    filename: ${LOGGING_ROOT:.}/${LOG_FILENAME}
    formatter: simple
loggers:
  test_logger:
    level: DEBUG
    handlers:
      - file_handler
    propagate: no
root:
  level: NOTSET
  handlers:
    - consoleNote:Ignore the backslashes as markdown must display those escape characters.Example UsageFile:test_logger.pyimport logging
from logging_.config import YAMLConfig

with open("logging.yaml", "r") as config_file:
    YAMLConfig(config_file.read(), silent=True)

# alternatively, you can use
# YAMLConfig.from_file("logging.yaml", silent=True)

logger = logging.getLogger("test_logger")

logger.debug("This is a debug log")
logger.info("This is an info log")
logger.warning("This is an warning log")
logger.error("This is an error log")
logger.critical("This is a critical log")Note:An (optional) explicitsilent=Trueflag must be set to suppress any file or parsing related exceptions to be thrown.handlers.QueueListenerHandlerA simpleQueueHandlersubclass implementation utilizingQueueListenerfor 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.yamlversion: 1
objects:
  queue:
    class: queue.Queue
    maxsize: 1000
formatters:
  simple:
    format: '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
handlers:
  console:
    class: logging.StreamHandler
    formatter: simple
    stream: ext://sys.stdout
  file_handler:
    class: logging.FileHandler
    filename: test_logger.log
    formatter: simple
  queue_handler:
    class: logging_.handlers.QueueListenerHandler
    handlers:
      - cfg://handlers.console
      - cfg://handlers.file_handler
    queue: cfg://objects.queue
loggers:
  test_logger:
    level: DEBUG
    handlers:
      - queue_handler
    propagate: no
root:
  level: NOTSET
  handlers:
    - consoleNote:A queue object must be passed since the handler does not set a default queue implementation. Setmaxsize: -1to make the queue unlimited.Example UsageFile:test_logger.pyimport logging.config
import yaml

with open("logging.yaml", "r") as config_file:
    logging_config = yaml.safe_load(config_file.read())
    logging.config.dictConfig(logging_config)

logger = logging.getLogger("test_logger")

logger.debug("This is a debug log")
logger.info("This is an info log")
logger.warning("This is an warning log")
logger.error("This is an error log")
logger.critical("This is a critical log")DevelopmentAdditional development and documentation dependencies can be installed using extras. It is recommended to use a virtualenv.Use Pre-Commit HooksInstall pre-commit hooks and dependencies:pip install pre-commit
pre-commit install
pre-commit autoupdate
pre-commit run --all-filesRun TestsRun tests from the source with Pytest:pip install -e .[dev]
pytest -sGenerate DocumentationGenerate documentation from the source with Sphinx:pip install -e .[doc]
cd docs
mkdir -p _static _templates
make html
python -m http.server --directory build/htmlNorequirements.txtFileThis 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 traditionalrequirements.txtfile. Instead, this project utilizes modern python packaging paradigms withpyproject.tomlandsetup.cfgfiles. However, sometimes some IDEs (i.e. PyCharm) cannot resolve dependencies without arequirements.txtfile. To generate arequirements.txtfile, simply run the following command within your venv:pip freeze > requirements.txtCreate Distribution PackagesTo create a source and wheel distribution, run:git clone git@github.com:zobayer1/logging-extras.git
python -m pip install wheel
python setup.py clean sdist bdist_wheelNote:This project usessetuptools-scmto generate build versions from git tags. Build system will raise errors if you are trying to build packages outside a git repo.