All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.lx.boot.log.LXLogAppender Maven / Gradle / Ivy

Go to download

使用文档: https://a7fi97h1rc.feishu.cn/docx/X3LRdtLhkoXQ8hxgXDQc2CLOnEg?from=from_copylink

There is a newer version: 1.1
Show newest version
package com.lx.boot.log;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.classic.spi.ThrowableProxy;
import ch.qos.logback.core.AppenderBase;
import com.lx.boot.OS;
import com.lx.boot.log.entity.EsInfo;
import com.lx.boot.log.entity.RunLogMessage;
import com.lx.constant.DefaultBaseConstant;
import com.lx.util.LX;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.MDC;
import org.slf4j.helpers.MessageFormatter;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;

//说明:分布式日志上传
/**{ ylx } 2021/1/21 13:51
 * 可以通过 lxlog.enable=false 进行关闭
 *
 **/
@Slf4j
public class LXLogAppender extends AppenderBase {

    public final static String CURRENT_IP = getIp();

    private ESUtil esUtil = new ESUtil(()->{
        String esHost = OS.getProperty(DefaultBaseConstant.ES_HOSTS);
        if (LX.isNotEmpty(esHost) && OS.getPropertyIsTrue(DefaultBaseConstant.LXLOG_ENABLE,"true")){
            return new EsInfo(esHost,OS.getProperty(DefaultBaseConstant.ES_USERNAME,""),OS.getProperty(DefaultBaseConstant.ES_PASSWORD,""), "plume_log_run_"+LX.getDate("yyyyMMdd"));
        }
        return null;
    });

    @Override
    protected void append(ILoggingEvent event) {
        String traceId = MDC.get("requestId");
        if (LX.isNotEmpty(traceId)){
            RunLogMessage logMessage = new RunLogMessage();
            logMessage.setServerName(CURRENT_IP);
            logMessage.setAppName(OS.getApplicationName());
            logMessage.setContent(getMessage(event));
            logMessage.setDtTime(event.getTimeStamp());
            logMessage.setMicroTime(getmicTime());
            logMessage.setTraceId(traceId);
            logMessage.setLogLevel(event.getLevel().toString());
            logMessage.setFirmId(OS.getFirmId());
            if (event.getCallerData().length>0){
                logMessage.setClassName(event.getCallerData()[0].getClassName());
                logMessage.setMethod(event.getCallerData()[0].getMethodName());
                logMessage.setLineNumber(event.getCallerData()[0].getLineNumber());
            }
            esUtil.offer(logMessage);
        }
    }

    //说明: 获取微秒
    /**{ ylx } 2021/1/20 16:23 */
    public static Long getmicTime() {
        long nanoTime = System.nanoTime();
        return System.currentTimeMillis() * 1000 + (nanoTime - nanoTime / 1000000 * 1000000) / 1000;
    }

    private static String getMessage(ILoggingEvent logEvent) {
        if (logEvent.getLevel().equals(Level.ERROR)) {
            if (logEvent.getThrowableProxy() != null) {
                ThrowableProxy throwableProxy = (ThrowableProxy) logEvent.getThrowableProxy();
                String[] args = new String[]{erroStackTrace(throwableProxy.getThrowable()).toString()};
                return packageMessage(logEvent.getMessage(), args);
            } else {
                Object[] args = logEvent.getArgumentArray();
                if (args != null) {
                    for (int i = 0; i < args.length; i++) {
                        if (args[i] instanceof Throwable) {
                            args[i] = erroStackTrace(args[i]);
                        }
                    }
                    return packageMessage(logEvent.getMessage(), args);
                }
            }
        }
        return logEvent.getFormattedMessage();
    }
    private static String packageMessage(String message, Object[] args) {
        if (message.indexOf("{}") > 0) {
            return MessageFormatter.arrayFormat(message, args).getMessage();
        }
        StringBuilder builder = new StringBuilder(128);
        builder.append(message);
        for (int i = 0; i < args.length; i++) {
            builder.append("\n").append(args[i]);
        }
        return builder.toString();
    }



    public static String getIp(){
        String localHostAddress = "127.0.0.1";
        try {
            Enumeration allNetInterfaces = NetworkInterface.getNetworkInterfaces();

            while(allNetInterfaces.hasMoreElements()){
                NetworkInterface networkInterface = allNetInterfaces.nextElement();
                Enumeration address = networkInterface.getInetAddresses();
                while(address.hasMoreElements()){
                    InetAddress inetAddress = address.nextElement();
                    if(inetAddress != null
                            && inetAddress instanceof Inet4Address
                            && !"127.0.0.1".equals(inetAddress.getHostAddress())){
                        localHostAddress = inetAddress.getHostAddress();
                    }
                }
            }
        }catch (Exception e){

        }
        return localHostAddress;
    }

    public static Object erroStackTrace(Object obj) {
        if (obj instanceof Exception) {
            Exception eObj = (Exception) obj;
            StringWriter sw = null;
            PrintWriter pw = null;
            try {
                sw = new StringWriter();
                pw = new PrintWriter(sw);
                String exceptionStack = "\r\n";
                eObj.printStackTrace(pw);
                exceptionStack = sw.toString();
                return exceptionStack;
            } catch (Exception e) {
                e.printStackTrace();
                return obj;
            } finally {
                try {
                    pw.close();
                    sw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        } else {
            return obj;
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy