import logging
from bingdog.util import equalsIgnoreCase
from bingdog.appconfig import Configurator

class Logger(object):
    __logger = None

    def __init__(self):
        loggingLevel = None
        configurator = Configurator.getConfigurator()
        if equalsIgnoreCase("info", configurator.getLoggingLevel()):
            loggingLevel = logging.INFO
        elif equalsIgnoreCase("debug", configurator.getLoggingLevel()):
            loggingLevel = logging.DEBUG
        logging.basicConfig(level=loggingLevel, filename=configurator.getLoggingFilePath(), format=configurator.getLoggingFormat())
        
    def info(self, message):
        logging.info(message)
    
    def warning(self, message):
        logging.warning(message)
    
    def error(self, message):
        logging.error(message)
    
    def exception(self, message):
        logging.exception(message)
    
    def getLogger(loggerClass, *args, **kargs):
        if issubclass(loggerClass, Logger) or loggerClass is Logger:
            if __logger is None:
                __logger = loggerClass(*args, **kargs)
        else:
            raise LoggerClassException(loggerClass)
        return __logger
        
class LoggerClassException(Exception):
    
    def __init__(self, loggerClass):
        super(HandlerException, self).__init__(loggerClass, " is not a class of Logger.")