qcodes.logger.instrument_logger

This module defines a logging.LoggerAdapter and logging.Filter. They are used to enable the capturing of output from specific instruments.

class qcodes.logger.instrument_logger.InstrumentFilter(instruments: Union[InstrumentBase, Sequence[InstrumentBase]])[source]

Bases: logging.Filter

Filter to filter out records that originate from the given instruments. Records created through the InstrumentLoggerAdapter have additional properties as specified in the extra dictionary which is a property of the adapter.

Here the instrument property gets used to reject records that don’t originate from the list of instruments that has been passed to the __init__ method.

Initialize a filter.

Initialize with the name of the logger which, together with its children, will have its events allowed through the filter. If no name is specified, allow every event.

filter(record: logging.LogRecord) bool[source]

Determine if the specified record is to be logged.

Is the specified record to be logged? Returns 0 for no, nonzero for yes. If deemed appropriate, the record may be modified in-place.

class qcodes.logger.instrument_logger.InstrumentLoggerAdapter(logger, extra)[source]

Bases: logging.LoggerAdapter

In the Python logging module adapters are used to add context information to logging. The logging.LoggerAdapter has the same methods as the logging.Logger and can thus be used as such.

Here it is used to add the instruments full name to the log records so that they can be filtered (using the InstrumentFilter) by instrument instance.

The context data gets stored in the extra dictionary as a property of the Adapter. It is filled by the __init__ method:

>>> LoggerAdapter(log, {'instrument': self.full_name})

Initialize the adapter with a logger and a dict-like object which provides contextual information. This constructor signature allows easy stacking of LoggerAdapters, if so desired.

You can effectively pass keyword arguments as shown in the following example:

adapter = LoggerAdapter(someLogger, dict(p1=v1, p2=”v2”))

process(msg: str, kwargs: MutableMapping[str, Any]) Tuple[str, MutableMapping[str, Any]][source]

Returns the message and the kwargs for the handlers.

critical(msg, *args, **kwargs)

Delegate a critical call to the underlying logger.

debug(msg, *args, **kwargs)

Delegate a debug call to the underlying logger.

error(msg, *args, **kwargs)

Delegate an error call to the underlying logger.

exception(msg, *args, exc_info=True, **kwargs)

Delegate an exception call to the underlying logger.

getEffectiveLevel()

Get the effective level for the underlying logger.

hasHandlers()

See if the underlying logger has any handlers.

info(msg, *args, **kwargs)

Delegate an info call to the underlying logger.

isEnabledFor(level)

Is this logger enabled for level ‘level’?

log(level, msg, *args, **kwargs)

Delegate a log call to the underlying logger, after adding contextual information from this adapter instance.

property manager
property name
setLevel(level)

Set the specified level on the underlying logger.

warn(msg, *args, **kwargs)
warning(msg, *args, **kwargs)

Delegate a warning call to the underlying logger.

qcodes.logger.instrument_logger.filter_instrument(instrument: Union[InstrumentBase, Sequence[InstrumentBase]], handler: Optional[Union[logging.Handler, Sequence[logging.Handler]]] = None, level: Optional[Union[int, str]] = None) Iterator[None][source]

Context manager that adds a filter that only enables the log messages of the supplied instruments to pass.

Example

>>> h1, h2 = logger.get_console_handler(), logger.get_file_handler()
>>> with logger.filter_instruments((qdac, dmm2), handler=[h1, h2]):
>>>     qdac.ch01(1)  # logged
>>>     v1 = dmm2.v() # logged
>>>     v2 = keithley.v()  # not logged
Parameters
  • instrument – The instrument or sequence of instruments to enable messages from.

  • level – Level to set the handlers to.

  • handler – Single or sequence of handlers to change.

qcodes.logger.instrument_logger.get_instrument_logger(instrument_instance: InstrumentBase, logger_name: Optional[str] = None) qcodes.logger.instrument_logger.InstrumentLoggerAdapter[source]

Returns an InstrumentLoggerAdapter that can be used to log messages including instrument_instance as an additional context.

The logging.LoggerAdapter object can be used as any logger.

Parameters
  • instrument_instance – The instrument instance to be added to the context of the log record.

  • logger_name – Name of the logger to which the records will be passed. If None, defaults to the root logger.

Returns

logging.LoggerAdapter instance, that can be used for instrument specific logging.