# coding=utf-8
"""
 @Author: fu-zhe
 @FileName: ext_logging.py
 @DateTime: 2024/5/16 14:27
 @Description: 
"""
import logging
import os
from mlogging import TimedRotatingFileHandler_MP
from appsUtils import env

APPFOLDERNAME = 'uebaMetricsAnalysis'

APP_CRON_FOLDERNAME = 'uebaMetricsAnalysis_cron'

def get_clean_file_path():
    fileroot = env.get_isop_root() + "/apps/" + APPFOLDERNAME + "/files"
    if not os.path.exists(fileroot):
        os.mkdir(fileroot)
    return fileroot

def get_logger(logfile):
    """
    获取日志句柄
    """
    logger = logging.getLogger(logfile)
    logger.setLevel(logging.DEBUG)
    logroot = env.get_isop_root() + "/apps/" + APPFOLDERNAME + "/logs"
    if not os.path.exists(logroot):
        os.mkdir(logroot)
    filehandle = TimedRotatingFileHandler_MP(os.path.normpath(logroot + "/" + \
                                                              logfile + ".log"), 'midnight')
    filehandle.suffix = "%Y-%m-%d"
    filehandle.setLevel(logging.DEBUG)
    consolehandle = logging.StreamHandler()
    consolehandle.setLevel(logging.DEBUG)
    formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    filehandle.setFormatter(formatter)
    consolehandle.setFormatter(formatter)
    logger.addHandler(filehandle)
    return logger


logger = get_logger(APPFOLDERNAME)
logger_cron = get_logger(APP_CRON_FOLDERNAME)