import datetime
import os
import logging

from custom_logger.custom_log import logger_init

main_logger = None

_now = datetime.datetime.now()

LOGGER_NAME = 'main'
LOGGER_FILE_NAME = f'{_now.strftime("%Y-%m-%d %H_%M_%S")}_{LOGGER_NAME}.log'

LOGGER_LEVEL = logging.INFO
LOGGER_DIR = "log/"
__LOGGER_FILE_PATH = LOGGER_DIR + LOGGER_FILE_NAME

def get_main_logger():
    """
    로거 객체 반환
    로거 객체가 없을 경우에는 로그 초기화를 진행하고 생성된 로그 객체를 반환함
    :return: 로그 객체
    """

    if not main_logger.handlers:
        logger_init(main_logger)

    return main_logger

if main_logger is None:
    
    main_logger = logging.getLogger(LOGGER_NAME)
    
    if not os.path.exists(LOGGER_DIR):
        os.makedirs(LOGGER_DIR)

    if not __LOGGER_FILE_PATH:
        logger_init(main_logger, level=LOGGER_LEVEL)
    else:
        if not os.path.exists(LOGGER_DIR):
            os.makedirs(LOGGER_DIR)
        logger_init(main_logger, level=LOGGER_LEVEL, file_log_path=__LOGGER_FILE_PATH)