package com.ssyf.plugs.logger;


import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * 日志监听器
 * @author huwei
 * @date 2024/8/23 15:54
 */
@Slf4j
@Component
public class LogEventListener {

    /**
     * 日志记录器
     */
    @Autowired(required = false)
    private LoggerRecorder loggerRecorder;

    @Async
    @EventListener(LogEvent.class)
    public void onApplicationEvent(LogEvent event) {
        LogInfo logInfo = event.getLogInfo();
        //记录日志
        if (loggerRecorder != null){
            loggerRecorder.record(logInfo);
        }else {
            //默认自行打印
            if(StrUtil.isBlank(logInfo.getErrorStackTrace())){
                log.info(logInfo.toString());
            }else{
                log.error(logInfo.toString());
            }
        }
    }
}
