package org.nl.config.lucene;
/**
 * @author ldjun
 * @version 1.0
 * @date 2023年08月24日 13:00
 * @desc desc
 */

import ch.qos.logback.classic.spi.ILoggingEvent;
import com.yomahub.tlog.core.enhance.logback.async.AspectLogbackAsyncAppender;
import org.apache.commons.lang3.StringUtils;

import org.nl.config.lucene.remote.RemoteLogServer;
import org.slf4j.MDC;

import java.util.Map;

public class AsyncLuceneAppender extends AspectLogbackAsyncAppender {


    public AsyncLuceneAppender() {
        RemoteLogServer.asyncLuceneAppender = this;
    }

    @Override
    protected void append(ILoggingEvent event) {
        String traceId = LuceneAppender.traceIdTL.get();
        if (StringUtils.isNotEmpty(traceId)){
            MDC.put("traceId",traceId);
            Map<String, String> mdcPropertyMap = event.getMDCPropertyMap();
            if (mdcPropertyMap.getClass().getName().contains("SynchronizedMap")){
                mdcPropertyMap.put("traceId",traceId);
            }
            MDC.clear();
        }
        RemoteLogServer.writeLog(event);
    }
    public void appendSync(ILoggingEvent event){
        super.append(event);
    }

}
