Python Quick Logging | QLogging

Beautifully colored, quick and simple Python logging. This logger is based on Python logging package

Screenshots:

Terminal/CMD

terminal

Notebooks:

notebook

Windows:

windows

Features

  • Color logging in Terminal and CMD
  • Color logging in Jupyter Notebook and Jupyter Lab
  • Color logging in Kaggle Notebook
  • Color logging in Google Colab Notebook
  • Know which function the logger was called from
  • Know while line number the logger was called from
  • Support logging to a file
  • Simple and clean one-liner
  • Customizable

Installation

$ pip install qlogging

Examples

Logging only to console/notebook:

import qlogging
logger = qlogging.get_logger(level='debug')

logger.debug("This is debug") 
logger.info("This is info")
logger.warning("This is warning")
logger.error("This is an error")
logger.critical("This is a critical")

Output (output format: <time> <function_name>,<line#>| <log_message>):

12:21:37 foo(),3| This is debug 
12:21:37 foo(),4| This is info 
12:21:37 foo(),5| This is warning 
12:21:37 foo(),6| This is an error 
12:21:37 foo(),7| This is a critical 

Logging to console/terminal and a log file (append if log file exists):

import qlogging
logger = qlogging.get_logger(level='debug', logfile='my_log.log')

Logging to console/terminal and a log file (overwrite if log file exists):

import qlogging
logger = qlogging.get_logger(level='debug', logfile='my_log.log', logfilemode='w')

Logging with loggingmode='long' (default is loggingmode='short'):

import qlogging
logger = qlogging.get_logger(level='debug', loggingmode='long')

logger.debug("This is debug") 

Output (output format: <date> <time> | <file_name> | <function_name>,<line#>| <log_message>):

2021-05-18 12:38:22 | <main.py> | <foo()>,4 | This is debug

Easy Customization

Customize your logger based on the following get_logger() function parameters

def get_logger(
    level="info",
    logfile=None,
    logfilemode="a",
    loggingmode="short",
    format_str=None,
    file_format_str=None,
    format_date=None,
    colors=None,
    logger_config=None,
):
    """
    returns Python logging based logger formatted with colors

    :param level: (DEFAULT='info') str of logging level, each str option is mapped to Python logging levels, str options:
                        'info': logging.INFO,
                        'debug': logging.DEBUG,
                        'warning': logging.WARNING,
                        'error': logging.ERROR,
                        'critical': logging.CRITICAL,
                        'notset': logging.NOTSET
    :param logfile: (DEFAULT=None) str path where to save log file, example: '/tmp/my_log.log'
    :param logfilemode: (DEFAULT='a') str of log file writing mode, same as the ones documented at Python logging package. options:
                        'a': appends to logfile
                        'w': overwrites logfile
    :param loggingmode: (DEFAULT='short') str logging mode to be selected. options:
                        'short': will use short str format ('%(asctime)s %(funcName)s,%(lineno)s| %(message)s') and short date format ('%H:%M:%S')
                        'medium': will use long str format ('%(asctime)s | %(filename)s | %(funcName)s,%(lineno)s | %(message)s') and long date format ('%Y-%m-%d %H:%M:%S')
                        'manual': you need to set :param format_str: and :param format_date: yourself
    :param format_str: (DEFAULT=None) str of format logging string for console, only set this if you selected :param loggingmode: as 'manual'. example (the style is always '%', see python logging module for more info):
                        '%(asctime)s | %(filename)s | %(funcName)s,%(lineno)s | %(message)s'
    :param file_format_str: (DEFAULT=None) str of format logging string for logfile (if you keep it None, we will use what you passed in :param format_str:), only set this if you selected :param loggingmode: as 'manual'. example (the style is always '%', see python logging module for more info):
                        '%(asctime)s | %(filename)s | %(funcName)s,%(lineno)s | %(message)s'
    :param date_str: (DEFAULT=None) str of date logging string, only set this if you selected :param loggingmode: as 'manual'. example:
                        '%Y-%m-%d %H:%M:%S'
    :param colors: (DEFAULT=None) dict of color settings, only set this if you selected :param loggingmode: as 'manual'. example:
                        {
                            '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,
                        }
    :param logger_config: (DEFAULT=None) dict python logger config if you want to fully overwrite configs. example:
                        {
                            "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,
                                },
                            },
                        }
    :return: formated Python logging instance
    """

GitHub

https://github.com/sinkingtitanic/qlogging