finesse.utilities.logging module

Loging utilities.

class finesse.utilities.logging.FinesseStreamHandler(*args, **kwargs)[source]

Bases: StreamHandler

Finesse stream handler.

This class provides a mechanism to exclude displayed log channels by wildcard. It is otherwise identical to logging.StreamHandler.

exclude(pattern)[source]
filter(record)[source]

Determine if a record is loggable by consulting all the filters.

The default is to allow the record to be logged; any filter can veto this by returning a false value. If a filter attached to a handler returns a log record instance, then that instance is used in place of the original log record in any further processing of the event by that handler. If a filter returns any other true value, the original log record is used in any further processing of the event by that handler.

If none of the filters return false values, this method returns a log record. If any of the filters return a false value, this method returns a false value.

Changed in version 3.2: Allow filters to be just callables.

Changed in version 3.12: Allow filters to return a LogRecord instead of modifying it in place.

reset_exclude_patterns()[source]

Empty the configured log channel exclude patterns, and return what was there.

finesse.utilities.logging.logs(logger, level=None, handler=None, close=True)[source]

Emit logs at or above level in the encapsulated context, optionally using the specified handler.

See the Python logging cookbook for more information.

Parameters

loggerlogging.Logger

The logger to use for the encapsulated context.

levelstr or int, optional

The minimum log levels to emit. The standard log levels “debug”, “info”, “warning”, “error” and “critical” are supported, as are their corresponding level numbers (see logging).

handlerlogging.Handler, optional

The handler to add to logger for the encapsulated context.

closebool, optional

Close handler once finished. Defaults to True.

Examples

Print debug logs during parsing, excluding compilation messages.

>>> import logging
>>> from finesse import Model
>>> from finesse.utilities import logs
>>> from finesse.utilities.logging import FinesseStreamHandler
>>> handler = FinesseStreamHandler()
>>> handler.exclude("finesse.script.compiler")
>>> model = Model()
>>> with logs(logging.getLogger(), level="debug", handler=handler):
>>>     model.parse("laser l1 P=1")
finesse.utilities.logging.tracebacks(tracebacks=True)[source]

Show or hide tracebacks in the encapsulated context.

Some environments, such as Jupyterlab, hide tracebacks by default. This context allows tracebacks to be forcefully shown or hidden temporarily.

Parameters

tracebacksbool, optional

Show tracebacks. Defaults to True.

Examples

Print tracebacks during a run.

>>> from finesse import Model
>>> from finesse.utilities import tracebacks
>>> model = Model()
>>> with tracebacks():
>>>     model.parse("laser l1 L=1")