Skip to content

Reference for SWMManywhere/logging.py

Logging module for SWMManywhere.

Example:

>>> import os
>>> os.environ["SWMMANYWHERE_VERBOSE"] = "true"
>>> # logging is now enabled in any swmmanywhere module
>>> from swmmanywhere.logging import logger # You can now log yourself
>>> logger.add("file.log") # Add a log file
>>> logger.info("This is an info message.") # Write to stdout and file.log
This is an info message.
>>> os.environ["SWMMANYWHERE_VERBOSE"] = "false" # Disable logging

dynamic_filter(record)

A dynamic filter.

Source code in swmmanywhere/logging.py
def dynamic_filter(record):
    """A dynamic filter."""
    return verbose()

get_logger()

Get a logger.

Source code in swmmanywhere/logging.py
def get_logger() -> loguru.logger:
    """Get a logger."""
    logger = loguru.logger
    logger.configure(
        handlers=[
            {
                "sink": sys.stdout,
                "filter": dynamic_filter,
                "colorize": True,
                "format": " | ".join(
                    [
                        "<cyan>{time:YYYY/MM/DD HH:mm:ss}</>",
                        "{message}",
                    ]
                ),
            }
        ]
    )
    return logger

new_add(sink, **kwargs)

A new add method to wrap existing one but with the filter.

Source code in swmmanywhere/logging.py
def new_add(sink, **kwargs):
    """A new add method to wrap existing one but with the filter."""
    # Include the dynamic filter in the kwargs if not already specified
    if "filter" not in kwargs:
        kwargs["filter"] = dynamic_filter
    # Call the original add method with the updated kwargs
    return original_add(sink, **kwargs)

set_verbose(verbose)

Set the verbosity.

Source code in swmmanywhere/logging.py
def set_verbose(verbose: bool):
    """Set the verbosity."""
    os.environ["SWMMANYWHERE_VERBOSE"] = str(verbose).lower()

verbose()

Get the verbosity.

Source code in swmmanywhere/logging.py
def verbose() -> bool:
    """Get the verbosity."""
    return os.getenv("SWMMANYWHERE_VERBOSE", "false").lower() == "true"