在日志中显示线程ID而不是线程名称 [英] Display thread id instead thread name in log

查看:1712
本文介绍了在日志中显示线程ID而不是线程名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有log4j的Struts应用程序来显示有关应用程序的信息。

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

格式化日志输出的模式如下:

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

log4j.appender.RALL.layout.ConversionPattern=[%p] %d{dd/MM/yyyy HH:mm:ss} [THREAD ID=%t] [CLASS=(%C{1}:%L)] %m%n 

我需要在日志中显示主题ID 而不是主题名称。显示线程名称的转换字符是%t。我没有在log4j文档中看到获取它的方法。

I need to show the thread id instead the thread name in log. The conversion character that display the thread name is %t. I don't see in log4j documentation the way to get it.

任何人都可以帮助我吗?

Can anyone help me??

推荐答案

这可能但不是那么简单,只需使用一些预先配置的模式。

It is possible but not so easy as just using some preconfigured patterns.

Log4j 1.X和Log4j 2.x don' t有任何预先配置的模式用于打印线程ID,但你总是可以使用一些魔术。

Log4j 1.X and Log4j 2.x don't have any preconfigured patterns for printing Thread ID but you can always use some "magic trick".

PatternLayout 是使用 PatternParser 类标记为 final 类,并将patterns的静态映射作为键和转换器类作为值。每当Parses发现模式使用以开头的日志模式格式时,它就会使用与地图中此模式键匹配的转换器。

PatternLayout is using PatternParser class which is mark as final class and has static map of "patterns" as keys and Converters classes as values. Everytime when Parses finds pattern using for logging pattern format starting with % it uses converter matched with this pattern key in map.

您无法将自己的规则添加到该地图,但您仍然可以编写自己的MyOwnPatternLayout:

You cannot add your own rule to that map, but you can still write your own MyOwnPatternLayout:

public class MyOwnPatternLayout extends PatternLayout

这将以格式方法执行此操作技巧:

which will in it's format method do such trick:

public String format(LoggingEvent event) {
   String log = super.format(event);
   /*
   Now you just have to replace with regex all occurences of %i or 
   any mark you would like to use as mark to represent Thread ID 
   with Thread ID value.
   Only thing you have to be sure to not use any mark as your Thread ID
   that already is defined by PatterParser class
   */
   return log.replaceAll("%i", someThreadID);
}

唯一的问题是你必须以某种方式获得该线程ID。有时您需要做的就是解析线程名称,您可以轻松收集:

The only problem is that you have to get that thread ID in some way. Sometimes all you have to do is to parse Thread name witch can you easly collect:

String threadName = event.getThreadName();

例如Apache-Tomcat将线程ID放在线程名称的末尾 http-nio- /127.0.0.1-8084\"-exec-41

For example Apache-Tomcat put thread ID at the end of thread name http-nio-/127.0.0.1-8084"-exec-41.

为了确保线程ID正确,您还可以创建自己的LogginEvent和Logger子类(MyLoggingEvent和MyLogger)和MyLogger内部的创建MyLoggingEvent女巫也将参数线程ID不仅仅是线程名称。然后你可以在上面的代码中轻松收集它。

To be sure that thread ID is correct you can also make your own subclass of LogginEvent and Logger (MyLoggingEvent and MyLogger) and inside MyLogger create MyLoggingEvent witch will also take as argument Thread ID not only Thread Name. Then you can easly collect it in code above.

对不起答案很长,我希望这至少会给你一些帮助。

Sorry for long answer and I hope this will at least give you some help.

这篇关于在日志中显示线程ID而不是线程名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆