简体   繁体   中英

log4j How can I dump the current log4j configuration as properties file format?

This is a log4j 1.X question. I have code running in a Tomcat servlet engine where I can't control the log4j.properties but I would like to change the logging configuration at runtime. I have found how to do this via PropertyConfigurator, and can update with properties. Is there a way to dynamically get the current configuration in Properties format?

If not available within log4j library is there an external open source library?

Based on your reply to my comment I really like your idea and I can see how it would be very useful. To my knowledge there is no feature provided by log4j 1.X that will dump the current config as a properties file. I'm pretty sure only the deserialization has been implemented - going from properties to objects in memory not vice versa.

Take for example a ConsoleAppender - if we look at the javadoc there is nothing to indicate that it is serializable or capable of dumping its configuration in any way let alone as properties.

Two options come to mind and they are:

  1. Download the source code for log4j 1.X and customize it to fit your needs by implementing the desired feature yourself. This may not be desirable since then you would be maintaining a customized version of log4j 1.X (making the switch to newer versions of log4j much harder) and it could take some time to implement it as you would need to analyze the existing code and determine how to add your feature.

OR

  1. Modify your design to attack the problem from another direction. Perhaps your web portal simply provides a way to edit a properties file and trigger a reload of that file. You would have to contend with the possibility of multiple users attempting to edit the file at the same time, but Java provides locking mechanisms you can use to deal with this. This way the properties file is always up to date so you can simply read it and display the contents rather than having to dump objects from memory.

Hope this helps!

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