简体   繁体   English

如何设置log4j2 Mbean的contextName

[英]How to set contextName of log4j2 Mbean

While accessing log4j2 Mbeans using jvisualvm, I see that the type (contextName) inside log4j2 package is a number (for me it is 1482868390). 当使用jvisualvm访问log4j2 Mbeans时,我看到log4j2包中的类型(contextName)是一个数字(对我来说是1482868390)。 Since this goes in as 'type' while specifying Object in query, I would like to specify it to a more recognizable name that I prefer. 由于这是在查询中指定对象时作为“类型”输入的,因此我希望将其指定为我更喜欢的名称。 Is there a way to set the contextName in xml configuration. 有没有一种方法可以在xml配置中设置contextName。 I think there is a way to do it for web applications, but I would like to set this for a standalone java application. 我认为有一种针对Web应用程序的方法,但是我想为独立的Java应用程序设置此方法。

Yes this is possible but not in configuration. 是的,这是可能的,但无法进行配置。 You can achieve this by subclassing one of Log4j's ContextSelectors and overriding its defaultContextName() method. 您可以通过对Log4j的ContextSelector之一进行子类化并覆盖其defaultContextName()方法来实现此目的。

You then tell Log4j to use your ContextSelector by starting your application with system property -DLog4jContextSelector=com.yourpackage.YourContextSelector . 然后,通过使用系统属性-DLog4jContextSelector=com.yourpackage.YourContextSelector启动应用程序,告诉Log4j使用ContextSelector。

If you're making all loggers async, you want to subclass AsyncLoggerContextSelector , otherwise ClassLoaderContextSelector . 如果要使所有记录器都异步, 则要子类化AsyncLoggerContextSelector ,否则要继承 ClassLoaderContextSelector

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM