pybend.utils package
This package provides:
project global parameter definition including the number of processors
logging tools
pybend.utils.globalParameters
Global configuration parameters.
Defines global parameters such as the number of processors to use and functions to get and set these parameters.
- pybend.utils.globalParameters.get_nb_procs() int
Get the number of processors.
Number of processors is the minimum between expected values and the number of available processors.
- Returns:
Number of processors.
- Return type:
int
- pybend.utils.globalParameters.set_nb_procs(nb: int) None
Set the number of desired processors.
- Parameters:
nb (int) – Number of desired processors.
pybend.utils.logging
Logging utilities.
This module manages logging tools.
Code was modified from: https://stackoverflow.com/questions/384076/how-can-i-color-python-logging-output
- class pybend.utils.logging.CustomLoggerFormatter(fmt=None, datefmt=None, style='%', validate=True, *, defaults=None)
Bases:
FormatterCustom formatter for the logger.
To use it:
logger = logging.getLogger("Logger name") ch = logging.StreamHandler() ch.setFormatter(CustomLoggerFormatter()) logger.addHandler(ch)
Initialize the formatter with specified format strings.
Initialize the formatter either with the specified format string, or a default as described above. Allow for specialized date formatting with the optional datefmt argument. If datefmt is omitted, you get an ISO8601-like (or RFC 3339-like) format.
Use a style parameter of ‘%’, ‘{’ or ‘$’ to specify that you want to use one of %-formatting,
str.format()({}) formatting orstring.Templateformatting in your format string.Changed in version 3.2: Added the
styleparameter.- format(record: LogRecord) str
Return the format according to input record.
- Parameters:
record (logging.LogRecord) – record
- Returns:
format as a string
- Return type:
str
- FORMATS: dict[int, str] = {10: '\x1b[38;20m%(asctime)s - %(name)s - %(levelname)s - %(message)s (%(filename)s:%(lineno)d)\x1b[0m', 20: '\x1b[38;20m%(asctime)s - %(name)s - %(levelname)s - %(message)s\x1b[0m', 30: '\x1b[33;20m%(asctime)s - %(name)s - %(levelname)s - %(message)s\x1b[0m', 40: '\x1b[31;20m%(asctime)s - %(name)s - %(levelname)s - %(message)s\x1b[0m', 50: '\x1b[31;1m%(asctime)s - %(name)s - %(levelname)s - %(message)s (%(filename)s:%(lineno)d)\x1b[0m'}
format for each logger output type
- bold_red: str = '\x1b[31;1m'
- format1: str = '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
- format2: str = '%(asctime)s - %(name)s - %(levelname)s - %(message)s (%(filename)s:%(lineno)d)'
- grey: str = '\x1b[38;20m'
- red: str = '\x1b[31;20m'
- reset: str = '\x1b[0m'
- yellow: str = '\x1b[33;20m'
- class pybend.utils.logging.Logger(name, level=0)
Bases:
Filtererlogger type
- _log(level, msg, args, exc_info=None, extra=None, stack_info=False, stacklevel=1)
Low-level logging routine which creates a LogRecord and then calls all the handlers of this logger to handle the record.
- addHandler(hdlr)
Add the specified handler to this logger.
- callHandlers(record)
Pass a record to all relevant handlers.
Loop through all handlers for this logger and its parents in the logger hierarchy. If no handler was found, output a one-off error message to sys.stderr. Stop searching up the hierarchy whenever a logger with the “propagate” attribute set to zero is found - that will be the last logger whose handlers are called.
- critical(msg, *args, **kwargs)
Log ‘msg % args’ with severity ‘CRITICAL’.
To pass exception information, use the keyword argument exc_info with a true value, e.g.
logger.critical(“Houston, we have a %s”, “major disaster”, exc_info=True)
- debug(msg, *args, **kwargs)
Log ‘msg % args’ with severity ‘DEBUG’.
To pass exception information, use the keyword argument exc_info with a true value, e.g.
logger.debug(“Houston, we have a %s”, “thorny problem”, exc_info=True)
- error(msg, *args, **kwargs)
Log ‘msg % args’ with severity ‘ERROR’.
To pass exception information, use the keyword argument exc_info with a true value, e.g.
logger.error(“Houston, we have a %s”, “major problem”, exc_info=True)
- exception(msg, *args, exc_info=True, **kwargs)
Convenience method for logging an ERROR with exception information.
- fatal(msg, *args, **kwargs)
Don’t use this method, use critical() instead.
- findCaller(stack_info=False, stacklevel=1)
Find the stack frame of the caller so that we can note the source file name, line number and function name.
- getChild(suffix)
Get a logger which is a descendant to this one.
This is a convenience method, such that
logging.getLogger(‘abc’).getChild(‘def.ghi’)
is the same as
logging.getLogger(‘abc.def.ghi’)
It’s useful, for example, when the parent logger is named using __name__ rather than a literal string.
- getChildren()
- getEffectiveLevel()
Get the effective level for this logger.
Loop through this logger and its parents in the logger hierarchy, looking for a non-zero logging level. Return the first one found.
- handle(record)
Call the handlers for the specified record.
This method is used for unpickled records received from a socket, as well as those created locally. Logger-level filtering is applied.
- hasHandlers()
See if this logger has any handlers configured.
Loop through all handlers for this logger and its parents in the logger hierarchy. Return True if a handler was found, else False. Stop searching up the hierarchy whenever a logger with the “propagate” attribute set to zero is found - that will be the last logger which is checked for the existence of handlers.
- info(msg, *args, **kwargs)
Log ‘msg % args’ with severity ‘INFO’.
To pass exception information, use the keyword argument exc_info with a true value, e.g.
logger.info(“Houston, we have a %s”, “notable problem”, exc_info=True)
- isEnabledFor(level)
Is this logger enabled for level ‘level’?
- log(level, msg, *args, **kwargs)
Log ‘msg % args’ with the integer severity ‘level’.
To pass exception information, use the keyword argument exc_info with a true value, e.g.
logger.log(level, “We have a %s”, “mysterious problem”, exc_info=True)
- makeRecord(name, level, fn, lno, msg, args, exc_info, func=None, extra=None, sinfo=None)
A factory method which can be overridden in subclasses to create specialized LogRecords.
- removeHandler(hdlr)
Remove the specified handler from this logger.
- setLevel(level)
Set the logging level of this logger. level must be an int or a str.
- warn(msg, *args, **kwargs)
- warning(msg, *args, **kwargs)
Log ‘msg % args’ with severity ‘WARNING’.
To pass exception information, use the keyword argument exc_info with a true value, e.g.
logger.warning(“Houston, we have a %s”, “bit of a problem”, exc_info=True)
- manager = <logging.Manager object>
- root = <RootLogger root (WARNING)>
- pybend.utils.logging.getLogger(title: str) Logger
Return the Logger with pre-defined configuration.
Example:
# module import import Logger # logger instanciation logger :Logger.Logger = Logger.getLogger("My application") # logger use logger.debug("debug message") logger.info("info message") logger.warning("warning message") logger.error("error message") logger.critical("critical message")
- Parameters:
title (str) – Name of the logger.
- Returns:
logger
- Return type: