hevslib.log module

Hevslib logger module is based on the standart library logging as well as colorama The hevslib.log work in a python terminal as well as jupyter and jupyterlab environment.

Currently it only supports console log without the creation of external log files. Hijackes the logging module and adds colors and simplistic functionality to it.

Logging levels are:

  • logging.DEBUG == 10 same as self.DEBUG

  • logging.INFO == 20 same as self.INFO

  • logging.WARNING == 30 same as self.WARNING

  • logging.ERROR == 40 same as self.ERROR

  • logging.CRITICAL == 50 same as self.CRITICAL

If a logging level logging.WARNING is selected only message with a equal or higher logging level (WARNING, ERROR, CRITICAL) are printed.

_images/log_example.png

Import

import sys
sys.path.append("hevslib")

from hevslib.log import *

Example

Simple

Simple example with console:

self.log = Log.console()
self.log.debug("A quirky message only developers care about")
self.log.info("Curious users might want to know this")
self.log.warning("Something is wrong and any user should be informed")
self.log.error("Serious stuff, this is red for a reason")
self.log.critical("OH NO everything is on fire")

Simple example with logfile, same as console with a logfile saved under /logs in append mode:

self.log = Log.file()
self.log.debug("A quirky message only developers care about")
self.log.info("Curious users might want to know this")
self.log.warning("Something is wrong and any user should be informed")
self.log.error("Serious stuff, this is red for a reason")
self.log.critical("OH NO everything is on fire")

Acronyms

Acronyms for either console or log mode.

self.log = Log.console()

self.log.debug("A quirky message only developers care about")
self.log.dbg("A quirky message only developers care about")
self.log.d("A quirky message only developers care about")

self.log.info("Curious users might want to know this")
self.log.inf("Curious users might want to know this")
self.log.nfo("Curious users might want to know this")
self.log.i("Curious users might want to know this")

self.log.warning("Something is wrong and any user should be informed")
self.log.warn("Something is wrong and any user should be informed")
self.log.w("Something is wrong and any user should be informed")

self.log.error("Serious stuff, this is red for a reason")
self.log.err("Serious stuff, this is red for a reason")
self.log.e("Serious stuff, this is red for a reason")

self.log.critical("OH NO everything is on fire")
self.log.crit("OH NO everything is on fire")
self.log.c("OH NO everything is on fire")
self.log.fatal("OH NO everything is on fire")

Special Characters

There are special constant such as self.LINE and self.INDENT

self.log = Log.console()

self.log.î(self.log.LINE)
self.log.î(self.log.INDENT + "Indented message)

Adaptation

Exemple with console custom implementation:

self.log = Log.console(lvl=logging.CRITICAL
                       name="loggername"
                       color={'DEBUG': Fore.MAGENTA,
                              'INFO': Fore.BLUE,
                              'WARNING': Fore.WHITE + Back.BLACK,
                              'ERROR': Fore.RED + Back.GREEN,
                              'CRITICAL': Fore.BLUE + Back.BLACK + Style.BRIGHT},
                       datefmt="%H:%M:%S"
                       print_date=False,
                       print_level=False,
                       print_name=False,
                       delimiter="#")
self.log.d("A quirky message only developers care about")

Exemple with logfile custom implementation:

self.log = Log.file(lvl_console=logging.CRITICAL
                    lvl_logfile=logging.WARNING
                    name="loggername"
                    color={'DEBUG': Fore.MAGENTA,
                           'INFO': Fore.BLUE,
                           'WARNING': Fore.WHITE + Back.BLACK,
                           'ERROR': Fore.RED + Back.GREEN,
                           'CRITICAL': Fore.BLUE + Back.BLACK + Style.BRIGHT},
                    datefmt="%H:%M:%S"
                    print_date=False,
                    print_level_console=True,
                    print_level_logfile=False,
                    print_name_console=True,
                    print_name_logfile=False,
                    delimiter="#",
                    logfile_name="another_name",
                    logfile_path="another_path/",
                    logfile_mode="w")
self.log.d("A quirky message only developers care about")

TODO

  • Themes

  • Default root logger

  • logger instance color

Documentation

class hevslib.log.ColoredFormatter(*args, colors: Optional[Dict[str, str]] = None, **kwargs)

Bases: logging.Formatter

Colored log formatter class inherits logging.Formatter class

Note

Helper class for the hevslib log module

Initialize the formatter with specified format strings.

format(record) → str

Format the specified record as text.

class hevslib.log.Log(lvl_console=None, lvl_logfile=None, name=None, colors=None, datefmt=None, print_date=None, print_level_console=None, print_level_logfile=None, print_name_console=None, print_name_logfile=None, delimiter=None, logfile_name=None, logfile_path=None, logfile_mode=None)

Bases: object

Main Log class holds all the logic; see the end of the script to see how it’s instantiated in order to have the line “from log import log” work

Parameters
  • lvl_console (logging.level) – human readable string describing the exception (default:None)

  • lvl_logfile (logging.level) – human readable string describing the exception (default:None)

  • name (str) – Logger name (default:None)

  • (dict(str (colors) – Fore.color)) : dict with colors default:( {‘DEBUG’: Fore.CYAN, ‘INFO’: Fore.GREEN, ‘WARNING’: Fore.YELLOW, ‘ERROR’: Fore.RED, ‘CRITICAL’: Fore.RED + Back.WHITE + Style.BRIGHT })

  • datefmt (str) – string with datetime format (default: ‘%Y-%m-%d %H:%M:%S,%f’)

  • print_date (bool) – print date string on output (default:None)

  • print_level_console (bool) – print verbositylevel string on console output (default:None)

  • print_level_logfile (bool) – print verbositylevel string on logfile output (default:None)

  • print_name_console (bool) – print loggername on console output (default:None)

  • print_name_logfile (bool) – print loggername on logfile output (default:None)

  • delimiter (str) – delimiter between message parts (default:None)

  • logfile_name (str) – name of the log file, if not specified takes the logger name. No needs to specify the extention, automatic setting to *.log (default:None)

  • logfile_path (str) – path of the log file, if not specified set to ‘logs/’ folder (default:None)

  • logfile_mode (str) – mode of logfile handler, see mode for detail (default:None)

c(msg, *args, **kwargs)

critical log message

Parameters
  • msg (str) – log message

  • *args – Variable length argument list.

  • **kwargs – Arbitrary keyword arguments.

classmethod console(lvl=10, name=None, colors=None, datefmt=None, print_date=True, print_level=False, print_name=False, delimiter='|')

Similar to a C/C++ overload, this function instanciate a logger in console only. :param lvl: Human readable string describing the exception. :type lvl: logging.level :param name: Logger name :type name: str :param colors (dict(str: Fore.color)): dict with colors default:( {‘DEBUG’: Fore.CYAN, ‘INFO’: Fore.GREEN,

‘WARNING’: Fore.YELLOW, ‘ERROR’: Fore.RED, ‘CRITICAL’: Fore.RED + Back.WHITE + Style.BRIGHT })

Parameters
  • datefmt (str) – string with datetime format (default: ‘%Y-%m-%d %H:%M:%S,%f’)

  • print_date (bool) – print date string on output (default:True)

  • print_level (bool) – print verbositylevel string on output (default:False)

  • print_name (bool) – print loggername on output (default:False)

  • delimiter (str) – delimiter between message parts (default:”|”)

crit(msg, *args, **kwargs)

critical log message

Parameters
  • msg (str) – log message

  • *args – Variable length argument list.

  • **kwargs – Arbitrary keyword arguments.

critical(msg, *args, **kwargs)

critical log message

Parameters
  • msg (str) – log message

  • *args – Variable length argument list.

  • **kwargs – Arbitrary keyword arguments.

d(msg, *args, **kwargs)

debug log message

Parameters
  • msg (str) – log message

  • *args – Variable length argument list.

  • **kwargs – Arbitrary keyword arguments.

dbg(msg, *args, **kwargs)

debug log message

Parameters
  • msg (str) – log message

  • *args – Variable length argument list.

  • **kwargs – Arbitrary keyword arguments.

debug(msg, *args, **kwargs)

debug log message

Parameters
  • msg (str) – log message

  • *args – Variable length argument list.

  • **kwargs – Arbitrary keyword arguments.

e(msg, *args, **kwargs)

error log message

Parameters
  • msg (str) – log message

  • *args – Variable length argument list.

  • **kwargs – Arbitrary keyword arguments.

err(msg, *args, **kwargs)

error log message

Parameters
  • msg (str) – log message

  • *args – Variable length argument list.

  • **kwargs – Arbitrary keyword arguments.

error(msg, *args, **kwargs)

error log message

Parameters
  • msg (str) – log message

  • *args – Variable length argument list.

  • **kwargs – Arbitrary keyword arguments.

fatal(msg, *args, **kwargs)

critical log message

Parameters
  • msg (str) – log message

  • *args – Variable length argument list.

  • **kwargs – Arbitrary keyword arguments.

classmethod file(lvl_console=30, lvl_logfile=10, name=None, colors=None, datefmt=None, print_date=True, print_level_console=False, print_level_logfile=True, print_name_console=False, print_name_logfile=False, delimiter='|', logfile_name=None, logfile_path=None, logfile_mode='a')

Similar to a C/C++ overload, this function instanciate a logger with file history. :param lvl_console: human readable string describing the exception (default:WARNING) :type lvl_console: logging.level :param lvl_logfile: human readable string describing the exception (default:DEBUG) :type lvl_logfile: logging.level :param name: Logger name :type name: str :param colors (dict(str: Fore.color)): dict with colors default:( {‘DEBUG’: Fore.CYAN, ‘INFO’: Fore.GREEN,

‘WARNING’: Fore.YELLOW, ‘ERROR’: Fore.RED, ‘CRITICAL’: Fore.RED + Back.WHITE + Style.BRIGHT })

Parameters
  • datefmt (str) – string with datetime format (default: ‘%Y-%m-%d %H:%M:%S,%f’)

  • print_date (bool) – print date string on output (default:True)

  • print_level_console (bool) – print verbositylevel string on console output (default:False)

  • print_level_logfile (bool) – print verbositylevel string on logfile output (default:True)

  • print_name_console (bool) – print loggername on console output (default:False)

  • print_name_logfile (bool) – print loggername on logfile output (default:False)

  • delimiter (str) – delimiter between message parts (default:”|”)

  • logfile_name (str) – name of the log file, if not specified takes the logger name (default:None)

  • logfile_path (str) – path of the log file, if not specified set to ‘logs/’ folder (default:None)

  • logfile_mode (str) –

    mode of logfile handler, default mode is append. See mode for detail (default:”a”)

i(msg, *args, **kwargs)

info log message

Parameters
  • msg (str) – log message

  • *args – Variable length argument list.

  • **kwargs – Arbitrary keyword arguments.

inf(msg, *args, **kwargs)

info log message

Parameters
  • msg (str) – log message

  • *args – Variable length argument list.

  • **kwargs – Arbitrary keyword arguments.

info(msg, *args, **kwargs)

info log message

Parameters
  • msg (str) – log message

  • *args – Variable length argument list.

  • **kwargs – Arbitrary keyword arguments.

nfo(msg, *args, **kwargs)

info log message

Parameters
  • msg (str) – log message

  • *args – Variable length argument list.

  • **kwargs – Arbitrary keyword arguments.

setLevel(lvl=None)

Set the logging level

Parameters

lvl (logging.level) – set new logging level

w(msg, *args, **kwargs)

warning log message

Parameters
  • msg (str) – log message

  • *args – Variable length argument list.

  • **kwargs – Arbitrary keyword arguments.

warn(msg, *args, **kwargs)

warning log message

Parameters
  • msg (str) – log message

  • *args – Variable length argument list.

  • **kwargs – Arbitrary keyword arguments.

warning(msg, *args, **kwargs)

warning log message

Parameters
  • msg (str) – log message

  • *args – Variable length argument list.

  • **kwargs – Arbitrary keyword arguments.