且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

log4j如何将当前的log4j配置转储为属性文件格式?

更新时间:2023-01-06 15:17:00

基于您对我的评论的回复,我非常喜欢您的想法,并且可以看到它会非常有用.据我所知,log4j 1.X没有提供将当前配置作为属性文件转储的功能.我很确定只实现了反序列化-从属性到内存中的对象,而不是相反.

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.

ConsoleAppender 为例-如果我们查看

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. 下载log4j 1.X的源代码,并通过自己实现所需的功能对其进行自定义以满足您的需求.这可能不是所希望的,因为那样您将维护定制版本的log4j 1.X(使切换到较新版本的log4j更加困难),并且可能需要花费一些时间来实现它,因为您需要分析现有代码并确定如何添加功能.

OR

  1. 修改您的设计以从另一个方向解决问题.也许您的Web门户仅提供了一种编辑属性文件并触发该文件重新加载的方法.您将不得不应对多个用户同时尝试编辑文件的可能性,但 Java 提供了可用于处理此问题的锁定机制.这样,属性文件始终是最新的,因此您只需读取文件并显示内容,而不必从内存中转储对象.

希望这会有所帮助!