logUtils

Logging utilities, taken from Tensorflow.

logUtils is meant to be a drop-in replacement for the standard library logging module. If you’re feeling lazy, you can even from bpreveal import logUtils as logging.

bpreveal.logUtils.getLogger()

Return TF logger instance.

Returns:

An instance of the Python logging library Logger.

Return type:

Logger

See Python documentation (https://docs.python.org/3/library/logging.html) for detailed API. Below is only a summary.

The logger has 5 levels of logging from the most serious to the least:

  1. CRITICAL

  2. ERROR

  3. WARNING

  4. INFO

  5. DEBUG

The logger has the following methods, based on these logging levels:

  1. critical(msg, *args, **kwargs)

  2. error(msg, *args, **kwargs)

  3. warning(msg, *args, **kwargs)

  4. info(msg, *args, **kwargs)

  5. debug(msg, *args, **kwargs)

The msg can contain string formatting. An example of logging at the ERROR level using string formatting is:

>>> logUtils.getLogger().error("The value %d is invalid.", 3)

You can also specify the logging verbosity. In this case, the WARNING level log will not be emitted:

>>> logUtils.setVerbosity("ERROR")
>>> logUtils.getLogger().warning("This is a warning.")
bpreveal.logUtils.log(level, msg, *args, **kwargs)

Log message at the given level.

Parameters:
  • level (int | str) – The log level to use.

  • msg (str) – The string to log.

  • args – Further arguments to the logger.

  • kwargs – Keyword arguments to the logger.

Return type:

None

bpreveal.logUtils.debug(msg, *args, **kwargs)

For nitty-gritty details.

Parameters:
  • msg (str) – The string to log.

  • args – Further arguments to the logger.

  • kwargs – Keyword arguments to the logger.

Return type:

None

bpreveal.logUtils.error(msg, *args, **kwargs)

Something went horribly wrong.

Parameters:
  • msg (str) – The string to log.

  • args – Further arguments to the logger.

  • kwargs – Keyword arguments to the logger.

Return type:

None

bpreveal.logUtils.critical(msg, *args, **kwargs)

The world is ending. This is not used by BPReveal.

Parameters:
  • msg (str) – The string to log.

  • args – Further arguments to the logger.

  • kwargs – Keyword arguments to the logger.

Return type:

None

bpreveal.logUtils.info(msg, *args, **kwargs)

Normal progress messages.

Parameters:
  • msg (str) – The string to log.

  • args – Further arguments to the logger.

  • kwargs – Keyword arguments to the logger.

Return type:

None

bpreveal.logUtils.warning(msg, *args, **kwargs)

Something the user should pay attention to.

Parameters:
  • msg (str) – The string to log.

  • args – Further arguments to the logger.

  • kwargs – Keyword arguments to the logger.

Return type:

None

bpreveal.logUtils.logFirstN(level, msg, n, *args)

Log ‘msg % args’ at level ‘level’ only first ‘n’ times.

Not threadsafe.

Parameters:
  • level (int) – The level at which to log.

  • msg (str) – The message to be logged.

  • n (int) – The number of times this should be called before it is logged.

  • args (list) – The args to be substituted into the msg.

Return type:

None

bpreveal.logUtils.logIf(level, msg, condition, *args)

Log ‘msg % args’ at level ‘level’ only if condition is fulfilled.

Parameters:
  • level (str | int) – The log level.

  • msg (str) – The message to log.

  • condition (bool) – A boolean. If True, the message will be logged.

  • args (list) – Other arguments to pass to the logger.

Return type:

None

bpreveal.logUtils.getVerbosity()

Return how much logging output will be produced.

Returns:

The current verbosity setting.

Return type:

int

bpreveal.logUtils.setVerbosity(userLevel)

Set the verbosity for this BPReveal session.

BPReveal uses the python logging module for its printing, and less-important information is logged at lower levels. Level options are CRITICAL, ERROR, WARNING, INFO, and DEBUG.

Parameters:

userLevel (str | int) – The level of logging that you’d like to enable. It may be an actual logging level (like logUtils.ERROR), or a string naming one of the logging levels (like "ERROR").

Return type:

None

bpreveal.logUtils.setBooleanVerbosity(verbose, verboseLevel='INFO', quietLevel='WARNING')

Instead of passing in an int or a string, use a boolean to set verbosity.

Parameters:
  • verbose (bool) – Should the logging be verbose?

  • verboseLevel (str) – If verbose, how much chatter do you want? Options are the same as for setVerbosity.

  • quietLevel (str) – If not verbose, how much chatter do you want? Options are the same as for setVerbosity.

Return type:

None

bpreveal.logUtils.wrapTqdm(iterable, logLevel=20, **tqdmKwargs)

Create a tqdm logger or a dummy, based on current logging level.

Parameters:
  • iterable (Iterable | int) – The thing to be wrapped, or the number to be counted to.

  • logLevel (str | int) – The log level at which you’d like the tqdm to print progress.

  • tqdmKwargs (dict) – Additional keyword arguments passed to tqdm.

Raises:

TypeError – If you provide an invalid log level.

Returns:

Either a tqdm that will do logging, or an iterable that won’t log.

Return type:

A tqdm-like object supporting either iteration or .update().

Sometimes, you want to display a tqdm progress bar only if the logging level is high. Call this with something you want to iterate over OR an integer giving the total number of things that will be processed (corresponding to:

pbar = tqdm.tqdm(total=10000)
while condition:
    pbar.update()
)

If iterable is an integer, then this will return a tqdm that you need to call update() on, otherwise it’ll return something you can use as a loop iterable.

logLevel may either be a level from the logging module (like logging.INFO) or a string naming the log level (like “info”)