简体   繁体   中英

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). 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. I think there is a way to do it for web applications, but I would like to set this for a standalone java application.

Yes this is possible but not in configuration. You can achieve this by subclassing one of Log4j's ContextSelectors and overriding its defaultContextName() method.

You then tell Log4j to use your ContextSelector by starting your application with system property -DLog4jContextSelector=com.yourpackage.YourContextSelector .

If you're making all loggers async, you want to subclass AsyncLoggerContextSelector , otherwise ClassLoaderContextSelector .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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