package cn.reghao.bnt.web.config.log;

import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.Layout;
import ch.qos.logback.core.UnsynchronizedAppenderBase;
import cn.reghao.bnt.web.admin.service.LogService;
import cn.reghao.jutil.web.ServletUtil;
import cn.reghao.jutil.jdk.web.log.AppLog;

/**
 * @author reghao
 * @date 2025-10-04 15:09:59
 */
public class MyLogbackAppender extends UnsynchronizedAppenderBase<ILoggingEvent> {
    // 自定义参数
    private String printAppenderName;
    private Layout<ILoggingEvent> layout;
    private String app;
    private String host;
    private String env;
    private LogService logService;

    public void setPrintAppenderName(String printAppenderName) {
        this.printAppenderName = printAppenderName;
    }

    public String getPrintAppenderName() {
        return printAppenderName;
    }

    public void setLayout(Layout<ILoggingEvent> layout) {
        this.layout = layout;
    }

    public Layout<ILoggingEvent> getLayout() {
        return layout;
    }

    public void setApp(String app) {
        this.app = app;
    }

    public String getApp() {
        return app;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public String getHost() {
        return host;
    }

    public void setEnv(String env) {
        this.env = env;
    }

    public String getEnv() {
        return env;
    }

    public void setLogService(LogService logService) {
        this.logService = logService;
    }

    @Override
    public void start() {
        System.out.println("调用 logback-start 方法");
        // 这里可以做些初始化判断(比如 layout 不能为 null), 或者写入数据库 或者redis时 初始化连接等等
        if(layout == null) {
            addWarn("Layout was not defined");
        }
        super.start();
    }

    @Override
    public void stop() {
        // 释放相关资源，如数据库连接，redis线程池等等
        System.out.println("调用 logback-stop 方法");
        if(!isStarted()) {
            return;
        }
        super.stop();
    }

    @Override
    public void append(ILoggingEvent event) {
        if (event == null || !isStarted()){
            return;
        }

        AppLog appLog = getAppLog(event);
        if (logService != null) {
            //logService.consumeAppLog(appLog);
        } else {
            // 此处自定义实现输出
            // 获取输出值：event.getFormattedMessage()
            // System.out.println(event.getFormattedMessage());
            // 使用 layout 格式化输出
            //System.out.print(printAppenderName + " " + layout.doLayout(event));
        }

        if ("dev".equals(env)) {
            System.out.print(printAppenderName + " " + layout.doLayout(event));
        }
    }

    private AppLog getAppLog(ILoggingEvent event) {
        String requestId = ServletUtil.getRequestId0();
        long timestamp = event.getTimeStamp();
        String level = event.getLevel().toString();
        String thread = event.getThreadName();
        String logger = event.getLoggerName();
        String message = event.getFormattedMessage();
        return new AppLog(requestId, "app", "host", timestamp, level, thread, logger, message);
    }
}
