Reputation: 759
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.
If that's the case, then the XML file cannot be changed after the program has been exported to the .jar file. 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.
Upvotes: 1
Views: 2763
Reputation: 2546
You can change the logger level by using the Java Management Extensions Bean (JMX Bean) included in the library:
Enable the JMX port in your application start up:
-Dcom.sun.management.jmxremote.port=[port_num]
Use any of the available JMX clients (the JVM provides one in JAVA_HOME/bin/jconsole.exe) while executing your application.
In JConsole look for the "org.apache.logging.log4j2.Loggers" bean
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
Upvotes: 2
Reputation: 354
I've used this in the past
LogManager.getRootLogger().setLevel(Level.DEBUG);
You can change to any of the log levels that way.
Upvotes: 0