log4j如何将当前的log4j配置转储为属性文件格式? [英] log4j How can I dump the current log4j configuration as properties file format?

查看:33
本文介绍了log4j如何将当前的log4j配置转储为属性文件格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个log4j 1.X问题.我的代码在Tomcat Servlet引擎中运行,在该引擎中我无法控制log4j.properties,但是我想在运行时更改日志记录配置.我已经找到了如何通过PropertyConfigurator做到这一点,并且可以使用属性进行更新.有没有一种方法可以动态获取属性"格式的当前配置?

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?

如果log4j库中不可用,是否有外部开源库?

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

推荐答案

基于您对我的评论的回复,我非常喜欢您的想法,并且可以看到它会非常有用.据我所知,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 提供了可用于处理此问题的锁定机制.这样,属性文件始终是最新的,因此您只需读取文件并显示内容,而不必从内存中转储对象.

希望这会有所帮助!

这篇关于log4j如何将当前的log4j配置转储为属性文件格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆