setup_logging

  • full name: tenpy.tools.misc.setup_logging

  • parent module: tenpy.tools.misc

  • type: function

tenpy.tools.misc.setup_logging(options=None, output_filename=None, *, filename=<object object>, to_stdout='INFO', to_file='INFO', format='%(levelname)-8s: %(message)s', datefmt=None, logger_levels={}, dict_config=None, capture_warnings=None, skip_setup=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 (e.g., run_seq_simulations()).

Deprecated since version 0.9.0: The arguments were previously collected in a dictionary options. Now they should be given directly as keyword arguments.

Parameters:
  • **kwargs – Keyword arguments as described in the options below.

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

Options

config log
option summary

capture_warnings

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

datefmt

Formatting string for the `asctime` key in the `format`, e.g. ``"%Y-%m-%d % [...]

dict_config

Alternatively, a full configuration dictionary for :func:`logging.config.di [...]

filename

Filename for the logfile. [...]

format

Formatting string, `fmt` argument of :class:`logging.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 useful 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. If not set, 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 Simulation.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.Formatter. You can for example use "{loglevel:.4s} {asctime} {message}" to include the time stamp of each message into the log - this is useful to get an idea where code hangs. Find allowed keys here. The style of the formatter is chosen depending on whether the format string contains '%' '{' '$', respectively.

option datefmt: str

Formatting string for the asctime key in the format, e.g. "%Y-%m-%d %H:%M:%S", see logging.Formatter.formatTime().

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.