动态更改 log4j 日志级别
动态更改 log4j 日志级别有哪些不同的方法,这样我就不必重新部署应用程序。在这些情况下,这些变化会是永久性的吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
动态更改 log4j 日志级别有哪些不同的方法,这样我就不必重新部署应用程序。在这些情况下,这些变化会是永久性的吗?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(10)
如果您想更改所有记录器的日志记录级别,请使用以下方法。这将枚举所有记录器并将日志记录级别更改为给定级别。请确保您没有在
log4j.properties
文件中设置了log4j.appender.loggerName.Threshold=DEBUG
属性。If you would want to change the logging level of all the loggers use the below method. This will enumerate over all the loggers and change the logging level to given level. Please make sure that you DO NOT have
log4j.appender.loggerName.Threshold=DEBUG
property set in yourlog4j.properties
file.我已成功使用此方法来减少“org.apache.http”日志的冗长性:
I have used this method with success to reduce the verbosity of the "org.apache.http" logs:
您可以使用以下代码片段
You can use following code snippet
文件看门狗
Log4j 能够监视
log4j.xml
文件中的配置更改。如果您更改了log4j文件,log4j将根据您的更改自动刷新日志级别。有关详细信息,请参阅 org.apache.log4j.xml.DOMConfigurator.configureAndWatch(String,long) 的文档。检查之间的默认等待时间为 60 秒。这些更改将是持久的,因为您直接更改文件系统上的配置文件。您所需要做的就是调用 DOMConfigurator.configureAndWatch() 一次。警告:由于线程泄漏,configureAndWatch 方法在 J2EE 环境中使用不安全
JMX
设置日志级别(或一般重新配置)log4j 的另一种方法是使用 JMX。 Log4j 将其记录器注册为 JMX MBean。使用应用程序服务器 MBeanServer 控制台(或 JDK 的 jconsole.exe),您可以重新配置每个单独的记录器。这些更改不是持久性的,并且会在您重新启动应用程序(服务器)后重置为配置文件中设置的配置。
自制
正如 Aaron 所描述的,您可以通过编程方式设置日志级别。您可以按照您希望的方式在应用程序中实现它。例如,您可以有一个 GUI,用户或管理员可以在其中更改日志级别,然后在记录器上调用
setLevel()
方法。是否将设置保留在某处取决于您。File Watchdog
Log4j is able to watch the
log4j.xml
file for configuration changes. If you change the log4j file, log4j will automatically refresh the log levels according to your changes. See the documentation oforg.apache.log4j.xml.DOMConfigurator.configureAndWatch(String,long
) for details. The default wait time between checks is 60 seconds. These changes would be persistent, since you directly change the configuration file on the filesystem. All you need to do is to invoke DOMConfigurator.configureAndWatch() once.Caution: configureAndWatch method is unsafe for use in J2EE environments due to a Thread leak
JMX
Another way to set the log level (or reconfiguring in general) log4j is by using JMX. Log4j registers its loggers as JMX MBeans. Using the application servers MBeanServer consoles (or JDK's jconsole.exe) you can reconfigure each individual loggers. These changes are not persistent and would be reset to the config as set in the configuration file after you restart your application (server).
Self-Made
As described by Aaron, you can set the log level programmatically. You can implement it in your application in the way you would like it to happen. For example, you could have a GUI where the user or admin changes the log level and then call the
setLevel()
methods on the logger. Whether you persist the settings somewhere or not is up to you.更改日志级别很简单;修改配置的其他部分将构成更深入的方法。
这些更改在 Logger 的整个生命周期中都是永久性的。重新初始化时,将读取并使用配置,因为在运行时设置级别不会保留级别更改。
更新:如果您使用的是 Log4j 2,则应根据 setLevel 的调用。 x/manual/migration.html">文档,因为这可以通过实现类来实现。
Changing the log level is simple; modifying other portions of the configuration will pose a more in depth approach.
The changes are permanent through the life cyle of the
Logger
. On reinitialization the configuration will be read and used as setting the level at runtime does not persist the level change.UPDATE: If you are using Log4j 2 you should remove the calls to
setLevel
per the documentation as this can be achieved via implementation classes.可以将 Log4j2 配置为通过按给定时间间隔扫描 log4j2.xml 文件(或等效文件)来刷新其配置。只需将“monitorInterval”参数添加到您的配置标记中即可。请参阅示例 log4j2.xml 文件的第 2 行,该文件告诉 log4j 如果自上次日志事件以来已经过去超过 5 秒,则重新扫描其配置。
如果您要部署到 Tomcat 实例、IDE 内或使用 Spring Boot,则需要执行额外的步骤才能完成这项工作。这似乎有点超出了这里的范围,可能值得一个单独的问题。
Log4j2 can be configured to refresh its configuration by scanning the log4j2.xml file (or equivalent) at given intervals. Just add the "monitorInterval" parameter to your configuration tag. See line 2 of the sample log4j2.xml file, which tells log4j to to re-scan its configuration if more than 5 seconds have passed since the last log event.
There are extra steps to make this work if you are deploying to a tomcat instance, inside an IDE, or when using spring boot. That seems somewhat out of scope here and probably merits a separate question.
对于 log4j 2 API ,您可以使用
For log4j 2 API , you can use
我这样做是为了更改 log4j 日志级别,它对我有用,我没有引用任何文档。我使用此系统属性值来设置我的日志文件名称。我也使用相同的技术来设置日志记录级别,并且将
其作为 JVM 参数传递(我使用 Java 1.7)
抱歉,这不会动态更改日志记录级别,它需要重新启动服务
在 log4j.properties 文件中,我添加了此条目,
我尝试了
一切都有效。希望这有帮助!
我的 pom.xml 中有以下依赖项
I did this to Change log4j log level and it worked for me, I have n't referred any document. I used this system property value to set my logfile name. I used the same technique to set logging level as well, and it worked
passed this as JVM parameter (I use Java 1.7)
Sorry this won't dynamically change the logging level, it requires a restart of the service
in the log4j.properties file, I added this entry
I tried
It all worked. hope this helps!
I have these following dependencies in my pom.xml
对于 log4j 1.x,我发现最好的方法是使用 DOMConfigurator 提交一组预定义的 XML 日志配置之一(例如,一个用于正常使用,一个用于调试)。
使用这些可以通过以下方式完成:
只需使用适当的配置名称调用它,并确保将模板放在类路径上。
With log4j 1.x I find the best way is to use a DOMConfigurator to submit one of a predefined set of XML log configurations (say, one for normal use and one for debugging).
Making use of these can be done with something like this:
Just call this with the appropriate config name, and make sure that you put the templates on the classpath.
问题分为两部分:
收集用户意图有以下几种方式:
POST /log-levels?logger=com.example.FooClass&level=DEBUG
传播更改可以通过以下方式完成:
更详细的文章请参见 https://www.prefab。 cloud/blog/dynamically-change-java-log-level/ 包含过滤器方法的示例以及集中式 UI 的外观。
至于这些变化是否是永久性的,这取决于你采取哪种方法。如果您使用 PubSub 或 ServiceDiscovery,则更改只会在重新启动之前生效,因为新服务不会收到消息。如果您希望应用程序的新实例能够获取日志级别的当前值,您将需要更像 Kafka / Redis Streams 的东西。这基本上是动态配置选项。
The question breaks down into 2 parts:
Collecting the user intent comes in a few flavors:
POST /log-levels?logger=com.example.FooClass&level=DEBUG
Propagating the change can be done by:
A more detailed write-up in https://www.prefab.cloud/blog/dynamically-changing-java-log-level/ with an example of the filter approach and a look at centralized UIs.
As far as whether the changes will be permanent, that depends on which approach you take. If you PubSub or ServiceDiscovery, the change will only take effect until something restarts, because the new service won't get the message. You'll want something more like Kafka / Redis Streams if you want to be able to have new instance of your application get the current value of the log level. That's basically the dynamic configuration option.