简体   繁体   English

程序执行期间如何更改log4j2中的日志记录级别

[英]How to change the logging level in log4j2 during program execution

Please correct me if I'm wrong, but it seems the log4j2.xml must be in the classpath of the program, and everything in the classpath gets packaged up into a .jar file when you make a standalone runnable .jar file of the program. 如果我错了,请纠正我,但是看来log4j2.xml必须在程序的类路径中,并且当您制作程序的独立可运行.jar文件时,类路径中的所有内容都打包到.jar文件中。

If that's the case, then the XML file cannot be changed after the program has been exported to the .jar file. 如果真是这样,那么在将程序导出到.jar文件之后,就无法再更改XML文件。 Thus, it doesn't seem that there is any way to change the logging level without re-exporting the program. 因此,似乎没有任何方法可以在不重新导出程序的情况下更改日志记录级别。

Please tell me I'm wrong about this and that there is a way that I can change the logging level while the program is running by using, say, a drop-down list in JFrame so that the user can select the logging level. 请告诉我我错了,有一种方法可以在程序运行时通过使用JFrame中的下拉列表来更改日志记录级别,以便用户可以选择日志记录级别。

I've used this in the past 我过去曾经用过

LogManager.getRootLogger().setLevel(Level.DEBUG);

You can change to any of the log levels that way. 您可以通过这种方式更改为任何日志级别。

You can change the logger level by using the Java Management Extensions Bean (JMX Bean) included in the library: 您可以使用库中包含的Java管理扩展Bean(JMX Bean)来更改记录器级别:

  1. Enable the JMX port in your application start up: 在应用程序启动中启用JMX端口:

    -Dcom.sun.management.jmxremote.port=[port_num] -Dcom.sun.management.jmxremote.port = [port_num]

  2. Use any of the available JMX clients (the JVM provides one in JAVA_HOME/bin/jconsole.exe) while executing your application. 在执行应用程序时,请使用任何可用的JMX客户端(JVM在JAVA_HOME / bin / jconsole.exe中提供了一个)。

  3. In JConsole look for the "org.apache.logging.log4j2.Loggers" bean 在JConsole中,查找“ org.apache.logging.log4j2.Loggers” bean

  4. Change the level of your logger 更改记录器的级别

The thing that I like most of this is that you don´t have to modify your code or configuration for managing this. 我最喜欢的是,您无需修改​​代码或配置即可管理该代码或配置。 It´s all external and transparent. 这一切都是外部的和透明的。

More info: http://logging.apache.org/log4j/2.x/manual/jmx.html 更多信息: http : //logging.apache.org/log4j/2.x/manual/jmx.html

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

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