setup_logging

  • full name: tenpy.tools.misc.setup_logging

  • parent module: tenpy.tools.misc

  • type: function

tenpy.tools.misc.setup_logging(options={}, output_filename=None)[source]

Configure the logging module.

The default logging setup is given by the following equivalent dict_config (here in [yaml] format for better readability).

version: 1  # mandatory for logging config
disable_existing_loggers: False  # keep module-based loggers already defined!
formatters:
    custom:
        format: "%(levelname)-8s: %(message)s"   # options['format']
handlers:
    to_stdout:
        class: logging.StreamHandler
        level: INFO         # options['to_stdout']
        formatter: custom
        stream: ext://sys.stdout
    to_file:
        class: logging.FileHandler
        level: INFO         # options['to_file']
        formatter: custom
        filename: output_filename.log   # options['filename']
        mode: a
root:
    handlers: [to_stdout, to_file]
    level: DEBUG

Note

We remove any previously configured logging handlers. This is to handle the case when this function is called multiple times, e.g., because you run multiple Simulation classes sequentially.

Parameters
  • options (dict) – Parameters as described below.

  • output_filename (None | str) – The filename where results are saved. The filename for the log-file defaults to this, but replecing the extension with .log.

Options

config logging
option summary

capture_warnings

Whether to call :func:`logging.captureWarnings` to include the warnings int [...]

dict_config

Alternatively, a full configuration dictionary for :mod:`logging.config.dic [...]

filename

Filename for the logfile. [...]

format

Formatting string, `fmt` argument of :class:`logging.config.Formatter`.

logger_levels

Set levels for certain loggers, e.g. ``{'tenpy.tools.params': 'WARNING'}`` [...]

skip_setup

If True, don't change anything in the logging setup; just return. [...]

to_file

If not None, save log with (at least) the given level to a file. [...]

to_stdout

If not None, print log with (at least) the given level to stdout.

option skip_setup: bool

If True, don’t change anything in the logging setup; just return. This is usefull for testing purposes, where pytest handles the logging setup. All other options are ignored in this case.

option to_stdout: None | "DEBUG" | "INFO" | "WARNING" | "ERROR" | "CRITICAL"

If not None, print log with (at least) the given level to stdout.

option to_file: None | "DEBUG" | "INFO" | "WARNING" | "ERROR" | "CRITICAL"

If not None, save log with (at least) the given level to a file. The filename is given by filename.

option filename: str

Filename for the logfile. It defaults to output_filename with the extension replaced to “.log”. If None, no log-file will be created, even with to_file set.

option logger_levels: dict(str, str)

Set levels for certain loggers, e.g. {'tenpy.tools.params': 'WARNING'} to suppress the parameter readouts logs. The keys of this dictionary are logger names, which follow the module structure in tenpy. For example, setting the level for tenpy.simulations will change the level for all loggers in any of those submodules, including the one provided as Simluation.logger class attribute. Hence, all messages from Simulation class methods calling self.logger.info(...) will be affected by that.

option format: str

Formatting string, fmt argument of logging.config.Formatter.

option dict_config: dict

Alternatively, a full configuration dictionary for logging.config.dictConfig. If used, all other options except skip_setup and capture_warnings are ignored.

option capture_warnings: bool

Whether to call logging.captureWarnings() to include the warnings into the log.