Repository: SuperDuperDB/superduperdb
File: superduper/base/logger.py (Python)

Code before:
class Logging:
    """Logging class to handle logging for the superduper.io # noqa."""

    if CFG.logging_type == LogType.LOKI:  # Send logs to Loki
        custom_handler = LokiLoggerHandler(
            url=os.environ["LOKI_URI"],
            labels={"application": "Test", "environment": "Develop"},
            labelKeys={},
            timeout=10,
            defaultFormatter=LoguruFormatter(),
        )

        logger.configure(handlers=[{"sink": custom_handler, "serialize": True}])
    else:
        # Replace default logger with a custom SuperDuper format.
        logger.remove()

        # Enrich logger with additional information.
        logger.configure(
            extra={
                "hostname": socket.gethostname(),
            }
        )

        # fmt = (
        #     "<green> {time:YYYY-MMM-DD HH:mm:ss.SS}</green>"
        #     "| <level>{level: <8}</level> "
        #     "| <cyan>{extra[hostname]: <8}</cyan>"
        #     "| <cyan>{name}</cyan>:<cyan>{line: <4}</cyan> "
        #     "| <level>{message}</level>"
        # )

        fmt = (
            "{time:YYYY-MMM-DD HH:mm:ss.SS}"
            "| {level: <8} "
            "| {extra[hostname]: <8}"
            "| {name}:{line: <4} "
            "| {message}"
        )

        # DEBUG until WARNING are sent to stdout.
        logger.add(
            lambda msg: tqdm.write(msg, end=""),
            format=fmt,
            level=CFG.log_level,
            filter=lambda record: record["level"].no < 40,
            # colorize=True,
            colorize=False,
        )

        # ERROR and above sent to stderr

Review comment (suggestion):
This should be configurable. We had this before, and it made life difficult with our `ray` deployments.

Code after:
class Logging:
    """Logging class to handle logging for the superduper.io # noqa."""

    if CFG.logging_type == LogType.LOKI:  # Send logs to Loki
        custom_handler = LokiLoggerHandler(
            url=os.environ["LOKI_URI"],
            labels={"application": "Test", "environment": "Develop"},
            labelKeys={},
            timeout=10,
            defaultFormatter=LoguruFormatter(),
        )

        logger.configure(handlers=[{"sink": custom_handler, "serialize": True}])
    else:
        # Replace default logger with a custom SuperDuper format.
        logger.remove()

        # Enrich logger with additional information.
        logger.configure(
            extra={
                "hostname": socket.gethostname(),
            }
        )

        colorize = CFG.log_colorize
        if colorize:
            fmt = (
                "<green>{time:YYYY-MMM-DD HH:mm:ss.SS}</green>"
                "| <level>{level: <8}</level> "
                "| <cyan>{extra[hostname]: <8}</cyan>"
                "| <cyan>{name}</cyan>:<cyan>{line: <4}</cyan> "
                "| <level>{message}</level>"
            )
        else:
            fmt = (
                "{time:YYYY-MMM-DD HH:mm:ss.SS}"
                "| {level: <8} "
                "| {name}:{line: <4} "
                "| {message}"
            )
        # DEBUG until WARNING are sent to stdout.
        logger.add(
            lambda msg: tqdm.write(msg, end=""),
            format=fmt,
            level=CFG.log_level,
            filter=lambda record: record["level"].no < 40,
            colorize=colorize,
        )

        # ERROR and above sent to stderr
        # https://loguru.readthedocs.io/en/stable/api/logger.html