import logging

FATAL = 50
ERROR = 40
INFO = 20
DEBUG = 10
TRACE = 5

DEFAULT_FILE = '/var/log/httpd/ssl_error_log'
DEFAULT_LEVEL = DEBUG

logging.addLevelName(TRACE, "TRACE")
logging.addLevelName(FATAL, "FATAL")
logging.addLevelName(ERROR, "ERROR")
logging.addLevelName(INFO, "INFO")
logging.addLevelName(DEBUG, "DEBUG")


def basic_config(log_file=DEFAULT_FILE, log_level=DEFAULT_LEVEL):
    logging.basicConfig(filename=log_file,
                        level=log_level,
                        format='%(asctime)s %(levelname)s: %(message)s',
                        datefmt='%m/%d/%Y %I:%M:%S %p')


def get_logger(log_file=DEFAULT_FILE, log_level=DEFAULT_LEVEL):
    logger = logging.getLogger('myapp')
    if not logger.handlers:
        hdlr = logging.FileHandler(log_file)
        formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
        hdlr.setFormatter(formatter)
        logger.addHandler(hdlr)
    logger.setLevel(log_level)
    return logger


def set_log_file(self, log_file):
    hdlr = logging.FileHandler(log_file)
    formatter = logging.Formatter('%(asctime)s %(levelname)s: %(message)s')
    hdlr.setFormatter(formatter)
    for hd in logging.handlers:  # remove all old handlers
        logging.removeHandler(hd)
    logging.addHandler(hdlr)


logging.Logger.set_log_file = set_log_file


def info(message):
    logging.log(INFO, message)


def debug(message):
    logging.log(DEBUG, message)


def fatal(message):
    logging.log(FATAL, message)


def error(message):
    logging.log(ERROR, message)


def trace(message):
    logging.log(TRACE, message)


__all__ = ["FATAL", "ERROR", "INFO", "DEBUG", "TRACE"]


