import os
import logging
from logging.handlers import RotatingFileHandler

# logger_name = 'default-logger'

# Default Logger Setting
format_string = '%(asctime)s - %(name)s - %(levelname)s : %(threadName)s(%(thread)s) ' \
                '%(funcName)s@%(pathname)s:%(lineno)d %(message)s'
logging.basicConfig(format=format_string)
logger = None


def init_logger(name, log_level, log_dir=None, log_file=None):
    """
    init logger
    :param log_dir: directory for logging
    :param log_file: log file name
    :param log_level: log level (logging.DEBUG, logging.INFO, logging.WARNING, logging.ERROR, logging.CRITICAL)
    :return: logger object
    """
    global logger
    logger = logging.getLogger(name)
    logger.setLevel(log_level)

    if log_dir and log_file:

        # global log_handler
        log_handler = RotatingFileHandler(os.path.join(log_dir, log_file), maxBytes=1024*1024*20, backupCount=5)
        log_handler.setLevel(log_level)

        # create formatter
        formatter = logging.Formatter(format_string)
        log_handler.setFormatter(formatter)

        logger.addHandler(log_handler)
