Skip to content

logging

DEFAULT_RUNTIME_CONFIGURATION_PATH: Path = Path('runtime.yaml') module-attribute

The default path to the runtime configuration file.

initialize_logging(log_level=None, runtime_configuration=None, *, parse_arguments=True)

Initialize an experiment for commandline usage.

This function configures commandline arguments and initializes the logging framework. Three sources of configuration are checked in the following priority:

  1. Commandline arguments
  2. Runtime configuration file
  3. Function arguments

That is, commandline arguments take precedence over the configuration file, and the configuration file takes precedence over the function arguments.

Example
>>> import flowcean.cli
>>> flowcean.cli.initialize(log_level="DEBUG")
Example
$ python my_experiment.py --log-level DEBUG

Parameters:

Name Type Description Default
log_level str | int | None

The logging level to use.

None
runtime_configuration Path | None

The path to the runtime configuration file.

None
parse_arguments bool

Whether to parse commandline arguments.

True
Source code in src/flowcean/cli/logging.py
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
def initialize_logging(
    log_level: str | int | None = None,
    runtime_configuration: Path | None = None,
    *,
    parse_arguments: bool = True,
) -> None:
    """Initialize an experiment for commandline usage.

    This function configures commandline arguments and initializes the logging
    framework. Three sources of configuration are checked in the following
    priority:

    1. Commandline arguments
    2. Runtime configuration file
    3. Function arguments

    That is, commandline arguments take precedence over the configuration
    file, and the configuration file takes precedence over the function
    arguments.

    Example:
        ```python
        >>> import flowcean.cli
        >>> flowcean.cli.initialize(log_level="DEBUG")
        ```

    Example:
        ```sh
        $ python my_experiment.py --log-level DEBUG
        ```

    Args:
        log_level: The logging level to use.
        runtime_configuration: The path to the runtime configuration file.
        parse_arguments: Whether to parse commandline arguments.
    """
    if parse_arguments:
        arguments = _parse_arguments()
        if arguments.log_level is not None:
            log_level = arguments.log_level
        if arguments.runtime_configuration is not None:
            runtime_configuration = arguments.runtime_configuration
        elif DEFAULT_RUNTIME_CONFIGURATION_PATH.exists():
            runtime_configuration = DEFAULT_RUNTIME_CONFIGURATION_PATH

    if runtime_configuration is not None:
        with Path(runtime_configuration).open() as file:
            configuration = ruamel.yaml.YAML(typ="safe").load(file)
        if "logging" in configuration:
            logging.config.dictConfig(configuration["logging"])
            if log_level is not None:
                logging.getLogger().setLevel(log_level)
            return

    logging.basicConfig(
        level=log_level or logging.INFO,
        format="%(asctime)s [%(name)s][%(levelname)s] %(message)s",
    )