log4j2 源代码片段 - 如何获取日志记录的行号、类名、方法名

import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.Marker;
import org.apache.logging.log4j.message.Message;
import org.apache.logging.log4j.message.ReusableParameterizedMessage;
import org.apache.logging.log4j.util.StackLocatorUtil;

/**
 * @date 2020-10-10 10:40
 */
public class StackLocatorUtilTest {

    public static void main(String[] args) {
        Logger logger = new Logger();
        logger.info("hello, this is log4j2.");
    }

    /**
     * 日志
     * org.apache.logging.log4j.core.Logger
     */
    static class Logger extends AbstractLogger {

        public void logMessage(final String fqcn, final Level level, final Marker marker, final Message message,
                               final Throwable t) {
            System.out.println("this is Logger logMessage.");
            StackTraceElement element = StackLocatorUtil.calcLocation(fqcn);

            StringBuilder sb = new StringBuilder();

            // 行号
            int lineNumber = element.getLineNumber();
            sb.append("lineNumber = ").append(lineNumber);

            // 类名
            final String className = element.getClassName();
            sb.append(", className = ").append(className);

            // 方法名
            final String methodName = element.getMethodName();
            sb.append(", methodName = ").append(methodName);

            System.out.println(sb);
        }
    }

    /**
     * 日志抽象
     */
    static abstract class AbstractLogger implements ExtendedLogger {
        private static final String FQCN = AbstractLogger.class.getName();

        public void info(final String message, final Object... params) {
            logIfEnabled(FQCN, Level.INFO, null, message, params);
        }

        public void logIfEnabled(final String fqcn, final Level level, final Marker marker, final String message,
                                 final Object... params) {
            logMessage(fqcn, level, marker, message, params);
        }

        protected void logMessage(final String fqcn, final Level level, final Marker marker, final String message,
                                  final Object... params) {
            Message msg = new ReusableParameterizedMessage();
            logMessageSafely(fqcn, level, marker, msg, msg.getThrowable());
        }

        private void logMessageSafely(final String fqcn, final Level level, final Marker marker, final Message msg,
                                      final Throwable throwable) {
            logMessageTrackRecursion(fqcn, level, marker, msg, throwable);
        }

        private void logMessageTrackRecursion(final String fqcn,
                                              final Level level,
                                              final Marker marker,
                                              final Message msg,
                                              final Throwable throwable) {
            try {
                // incrementRecursionDepth(); // LOG4J2-1518, LOG4J2-2031
                tryLogMessage(fqcn, level, marker, msg, throwable);
            } finally {
                // decrementRecursionDepth();
            }
        }

        private void tryLogMessage(final String fqcn,
                                   final Level level,
                                   final Marker marker,
                                   final Message msg,
                                   final Throwable throwable) {
            try {
                logMessage(fqcn, level, marker, msg, throwable);
            } catch (final Exception e) {
                // LOG4J2-1990 Log4j2 suppresses all exceptions that occur once application called the logger
                // handleLogMessageException(e, fqcn, msg);
            }
        }
    }

    static interface ExtendedLogger {
        void logMessage(String fqcn, Level level, Marker marker, Message message, Throwable t);
    }
}

 

你可能感兴趣的:(Java,log4j2,log4j2,java,log4j2.11.0)