Display thread id instead thread name in log

后端 未结 10 831
南笙
南笙 2020-12-31 00:34

I have a Struts application with log4j to display information about application.

The pattern to format log\'s output is as follows:

log4j.appender.RA         


        
10条回答
  •  长情又很酷
    2020-12-31 01:20

    One possible solution is to create your own class which sits between your code and Log4J and appends the thread ID to every log message:

    public class ThreadLogger
    {
        // Constructor declared private to prevent instantiation.  Use static methods instead.
        private ThreadLogger() {}
    
        private static enum LogLevel
        {
            TRACE,
            DEBUG,
            INFO,
            WARN,
            ERROR
        }
    
        public static void trace(String message)
        {
            logMessage(message, LogLevel.ERROR);
        }
    
        public static void debug(String message)
        {
            logMessage(message, LogLevel.ERROR);
        }
    
        public static void info(String message)
        {
            logMessage(message, LogLevel.ERROR);
        }
    
        public static void warn(String message)
        {
            logMessage(message, LogLevel.WARN);
        }
    
        public static void error(String message)
        {
            logMessage(message, LogLevel.ERROR);
        }
    
        private static void logMessage(String message, LogLevel logLevel)
        {
            // Get the Log4J logger for the class that originally wanted to log the message
            String callingClassName = Thread.currentThread().getStackTrace()[3].getClassName();
            Class callingClass;
            try
            {
                callingClass = Class.forName(callingClassName);
            }
            catch(ClassNotFoundException e)
            {
                String errorMessage = String.format("Could not reference class [%s].  Unable to log call!", callingClassName);
                throw new RuntimeException(errorMessage);
            }
            Logger logger = Logger.getLogger(callingClass);
    
            // Get the thread ID and place it in front of the logged message
            long threadId = Thread.currentThread().getId();
            String formattedMessage = String.format("[%s] %s", threadId, message);
    
            // Log the message
            switch(logLevel)
            {
                case TRACE:
                    logger.trace(formattedMessage);
                    break;
                case DEBUG:
                    logger.debug(formattedMessage);
                    break;
                case INFO:
                    logger.info(formattedMessage);
                    break;
                case WARN:
                    logger.warn(formattedMessage);
                    break;
                case ERROR:
                    logger.error(formattedMessage);
                    break;
            }
        }
    }
    

    Downsides:

    • Performance? This adds a few extra steps to every log statement.
    • Stability? This adds a potential point of failure (the Class.forName call).
    • You have to replace all of your existing log statements with calls to the new class.
    • The thread ID won't appear until after the regular Log4J formatting. IE:

    1234 [main] INFO com.foo.bar.Baz - [1] Hello world on thread #1!
    1234 [main] INFO com.foo.bar.Baz - [2] Hello world on thread #2!

提交回复
热议问题