import logging
import os
from logging import config as cf

from framework.base import get_project_path
from framework.env_config import get_config


def build_logger(config):
    if 'handlers' in config and 'file' in config['handlers']:
        file = config['handlers']['file']
        path = os.path.join(get_project_path(), file["filename"])
        os.makedirs(os.path.split(path)[0], exist_ok=True)
        file["filename"] = os.path.abspath(path)
    cf.dictConfig(config)


config = get_config("framework.logger")
if config:
    build_logger(config)


def get_logger(name=None):
    return logging.getLogger(config['use'] if 'use' in config and config['use'] is not None else name)