# Copyright 2025 21X AG
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Logging Module"""

import logging


class CustomLogRecord(logging.LogRecord):
    "add custom keywords, like origin, that can be used as within log_format"

    # pylint: disable=too-few-public-methods

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.origin = f"{self.name}.{self.funcName}"


class CustomFormatter(logging.Formatter):
    """Enables color coded logging"""

    grey = "\x1b[38;20m"
    yellow = "\x1b[33;20m"
    red = "\x1b[31;20m"
    bold_red = "\x1b[31;1m"
    reset = "\x1b[0m"
    log_format = "%(levelname)s %(name)s - %(message)s"
    date_format = "%Y-%m-%d %H:%M:%S"

    FORMATS = {
        logging.DEBUG: grey + log_format + reset,
        logging.INFO: grey + log_format + reset,
        logging.WARNING: yellow + log_format + reset,
        logging.ERROR: red + log_format + reset,
        logging.CRITICAL: bold_red + log_format + reset,
    }

    def format(self, record):
        log_fmt = self.FORMATS.get(record.levelno)
        formatter = logging.Formatter(log_fmt, datefmt=self.date_format)
        return formatter.format(record)


# pylint: disable=invalid-name
def getLogger(name):
    """Returns logging.getLogger"""

    logging.setLogRecordFactory(CustomLogRecord)

    # create logger
    logger = logging.getLogger(name)
    logger.setLevel(logging.INFO)

    # create console handler with a higher log level
    ch = logging.StreamHandler()
    ch.setLevel(logging.INFO)

    # set formatter and add handler
    ch.setFormatter(CustomFormatter())
    logger.addHandler(ch)

    return logger


def enable_log_to_file():
    """Enables logging to file"""

    fh = logging.FileHandler("logfile.log")
    fh.setFormatter(logging.Formatter("%(levelname)s %(name)s - %(message)s"))
    logging.getLogger().addHandler(fh)
