Skip to content

Instantly share code, notes, and snippets.

@a-recknagel
Last active May 18, 2021 12:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save a-recknagel/238cd768ccbac31c98d2118834788f06 to your computer and use it in GitHub Desktop.
Save a-recknagel/238cd768ccbac31c98d2118834788f06 to your computer and use it in GitHub Desktop.
qlog test
from logging import getLogger, config
from colorama import Fore, Back, Style
log = getLogger(__name__)
config.dictConfig(
{
"version": 1,
"disable_existing_loggers": False,
"formatters": {
"qlog": {
"()": "qlogging.qlogging.ColoredFormatter",
"colors": {
'DEBUG': Fore.CYAN + Style.BRIGHT,
'INFO': Fore.GREEN + Style.BRIGHT,
'WARNING': Fore.YELLOW + Style.BRIGHT,
'ERROR': Fore.RED + Style.BRIGHT,
'CRITICAL': Fore.RED + Back.WHITE + Style.BRIGHT,
},
"format": "%(color)s%(asctime)s [%(levelname)s] %(name)s: %(message)s"
},
},
"handlers": {
"console": {
"level": "DEBUG",
"formatter": "qlog",
"class": "logging.StreamHandler",
"stream": "ext://sys.stdout",
},
},
"loggers": {
"": {
"handlers": ["console"],
"level": "DEBUG",
"propagate": True,
},
},
}
)
# colored output, since I adapted the format string
log.debug("test 1")
log.info("test 2")
log.warning("test 3")
log.error("test 4")
@ELHoussineT
Copy link

now it will work if you do:

from qlogging import get_logger
from colorama import Fore, Back, Style

my_logger_config = {
                        "version": 1,
                        "disable_existing_loggers": False,
                        "formatters": {
                            "qlog": {
                                "()": "qlogging.qlogging.ColoredFormatter",
                                "colors":  {
                                    'DEBUG': Fore.CYAN + Style.BRIGHT,
                                    'INFO': Fore.GREEN + Style.BRIGHT,
                                    'WARNING': Fore.YELLOW + Style.BRIGHT,
                                    'ERROR': Fore.RED + Style.BRIGHT,
                                    'CRITICAL': Fore.RED + Back.WHITE + Style.BRIGHT,
                                },
                                "format": "%(asctime)s [%(levelname)s] %(name)s: %(message)s",
                                "datefmt":'%H:%M:%S'
                            },
                        },
                        "handlers": {
                            "console": {
                                "level": "DEBUG",
                                "formatter": "qlog",
                                "class": "logging.StreamHandler",
                                "stream": "ext://sys.stdout",
                            },
                        },
                        "loggers": {
                            "": {
                                "handlers": ["console"],
                                "level": "DEBUG",
                                "propagate": True,
                            },
                        },
                    }


logger = get_logger(logger_config=my_logger_config)

# currently, output on my terminal is not colored
logger.debug("test 1")
logger.info("test 2")
logger.warning("test 3")
logger.error("test 4")

@ELHoussineT
Copy link

just make sure you update to latest qlogger by: pip3 install qlogging>=1.0.5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment