Source code for mymeco.logger

# coding: utf-8
"""
Logging utility module.

Module aims to configure logging (verbosity, format, etc…)
"""
import typing
import logging
import coloredlogs


[docs] def configure(verbosity_count: int = 0, quiet_count: int = 0, colored: bool = True) -> None: """ Configure main logger according to given verbosity configuration. :param verbosity_count: Set number of `-v` command line switch. :param quiet_count: Set number of `-q` command line switch. :param colored: Define if log output should be colored or not. Default is to set verbosity on INFO level """ log = logging.getLogger() # Compute verbosity: 10 is for DEBUG and 50 for CRITICAL # Default is 20 (for INFO). Each verbosity count (-v) will decrease # level by 10 and each quiet count (-q) will increase level by 10. level = 20 - (verbosity_count * 10) + (quiet_count * 10) level = max(0, min(60, level)) log.setLevel(level) log.handlers = [] formatter_class: typing.Type[logging.Formatter] if colored: formatter_class = coloredlogs.ColoredFormatter else: formatter_class = logging.Formatter handler = logging.StreamHandler() handler.setLevel(logging.DEBUG) handler.setFormatter( formatter_class('%(levelname)s\t%(message)s') ) log.addHandler(handler)