#!/usr/bin/env python

"""SHINS Logger"""

import logging
# from cysystemd import journal as cyjournal


class Color:
    """ANSI Colors and formatting codes"""
    def __init__(self):
        """Costructor"""

        self.bold = '\033[1m'
        self.end = '\033[0m'
        self.red = '\033[31m'
        self.yellow = '\033[33m'
        self.green = '\033[32m'
        self.purple = '\033[35m'
        self.cyan = '\033[36m'
        self.blue = '\033[34m'
        self.underline = '\033[4m'

    def test(self):
        """Test colors"""
        attr = vars(self)
        for item in attr.items():
            print(item[1]+item[0]+self.end)


class ColoredFormatter(logging.Formatter):
    """Formatter class for custom logging"""

    def __init__(self, msg, color=True):
        """Constructor"""
        logging.Formatter.__init__(self, msg,
                                   datefmt='%Y-%m-%d %H:%M:%S.%s'
                                   #'%Y-%m-%d %H:%M:%S.%sss'
                                   )
        self.color = color

    def format(self, record):
        """Format method"""
        col = Color()

        colors = {
            'DEBUG': col.purple,
            'INFO': col.green,
            'WARNING': col.yellow,
            'ERROR': col.red,
            'CRITICAL': col.underline+col.red,
        }

        if self.color and record.levelname in colors:
            record.levelname = col.bold + colors[record.levelname] + record.levelname + col.end + col.end
            record.module = col.bold + record.module + col.end
            record.funcName = col.bold + record.funcName + col.end

        return logging.Formatter.format(self, record)


class CustomLogger(logging.Logger):
    """Custom Logger"""

    def __init__(self, name):
        """Costructor"""

        self.fmt = "%(asctime)s %(levelname)-21s: %(message)s (%(module)s %(funcName)s)"
        logging.Logger.__init__(self, name)
#        logging.Logger.__init__(self, name, logging.DEBUG)
        
        color_formatter = ColoredFormatter(self.fmt)

        # CONSOLE
        console = logging.StreamHandler()
        console.setFormatter(color_formatter)


        # LOGFILE
        logfile = logging.FileHandler("/data/log/log_of_python_test.log")
        logfile.setFormatter(color_formatter)


        # WEBSOCKET? 
        #console = logging.HTTPHandler("snoopy:5001", "/", "POST")
       
        
        self.addHandler(console)
        self.addHandler(logfile)


logging.setLoggerClass(CustomLogger)

scheduler_logger = logging.getLogger('apscheduler.executors.default')
scheduler_logger.setLevel(logging.ERROR)
scheduler_logger.disabled = True
#scheduler_logger.propagate = False


logger = logging.getLogger("SHINS")
logger.setLevel(logging.DEBUG)

# logger = logging.getLogger("SHINS")
# fmt = logging.Formatter('%(levelname)s:%(asctime)s:%(name)s:%(lineno)d:%(message)s')
# cyjournal_handler = cyjournal.JournaldLogHandler()
# cyjournal_handler.setFormatter(fmt)
# logger.addHandler(cyjournal_handler)
