"""
Logger module for TLS Tunnel Client
"""

import logging
import sys

from zscams.agent.src.support.configuration import get_config


loggers: dict[str, logging.Logger] = {}


def get_logger(name: str = "tls_tunnel") -> logging.Logger:
    """Create a singleton instance for that logger name"""

    if name in loggers:
        return loggers[name]

    logger = logging.getLogger(name)

    level = get_config().get("logging", {}).get("level", 10)

    logger.setLevel(level)
    logger.propagate = False

    if logger.hasHandlers():
        logger.handlers.clear()

    console_handler = logging.StreamHandler(sys.stdout)
    console_handler.setLevel(level)

    console_formatter = logging.Formatter(
        "%(asctime)s [%(levelname)s] %(name)s: %(message)s", "%Y-%m-%d %H:%M:%S"
    )
    console_handler.setFormatter(console_formatter)
    logger.addHandler(console_handler)

    loggers[name] = logger
    return logger
