Display thread id instead thread name in log

后端 未结 10 839
南笙
南笙 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:02

    I create my own appender and set Thread.currentThread().getId() to the MDC property. %X{threadId} should give me the thread id. This solution is working since 1.2.15. You can then attach AsyncAppender to this.

    public class CurrentThreadIdAppender extends AppenderSkeleton implements AppenderAttachable {
    
        private final AppenderAttachableImpl appenders = new AppenderAttachableImpl();
    
    ...
    
        @Override
        protected void append(LoggingEvent event) {   
            synchronized (appenders) {
                event.setProperty("threadId", String.valueOf(Thread.currentThread().getId()));
                appenders.appendLoopOnAppenders(event);
            }
        }
    
    ...
    
    }
    

提交回复
热议问题