集群环境中的Log4j [英] Log4j in clustered environment
问题描述
我正在将log4j用于在集群环境中运行的Web应用程序.在某些情况下,某些过程(并非特定过程)会丢失日志记录.我无法指出原因.在单服务器实例上运行时,它可以正常工作.我正在使用每日滚动文件追加器.还有其他要添加/修改的属性吗? Async Appender会帮助您吗?有什么解决办法吗?
I am using log4j for a web application running in clustered environment. Under few circumstances the logging is lost for some of the processes (not particular). I am not able to pin point why. When run on single server instance, it works fine. I am using a daily rolling file appender. Any additional attributes to be added/modified ? Will Async Appender help ? Any solution on this ?
以下是我计划使用的log4j XML以及AsyncAppender
EDIT : Following is log4j XML along with AsyncAppender that I am planning to use
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="true">
<appender name="appLog" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="/logs/app.log" />
<param name="DatePattern" value="'.'yyyy-MM-dd" />
<param name="Threshold" value="DEBUG" />
<param name="Append" value="true" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p (%F:%L) - %m%n" />
</layout>
</appender>
<appender name="async" class="org.apache.log4j.AsyncAppender">
<param name="BufferSize" value="256" />
<param name="LocationInfo" value="true"/>
<appender-ref ref="appLog" />
</appender>
<root>
<priority value="debug" />
<appender-ref ref="async" />
</root>
</log4j:configuration>
推荐答案
按照 Isaac 的建议,可以避免两个不同的类加载器/jvm写入同一物理日志文件的情况:
As per suggestion given by Isaac to avoid a situation in which two different classloaders/jvm write into the same physical log file:
有两种解决方法:
There are two ways to resolve it:
-
每个JVM日志都放入不同的日志文件中.实际上,试一试并证明其有效
Have each JVM log into a different log file. Actually, give it a shot and witness that it works
请改用SocketAppender.您所有的日志记录(来自所有服务器实例)最终将通过网络连接发送,并且 服务器程序(由log4j提供)将收集它们并将其写入 到一个文件中.解释如何使用它可以在这里找到.
Use a SocketAppender instead. All your logging (from all server instances) will end up being sent over a network connection, and a server program (provided by log4j) will collect them and write them into a file. Explanation how to use it can be found here.
对于第1点,可以使用以下方法:
For point#1, below approach can work:
使用:
<param name="File" value="${sys:log4j.logFile}" />
相反:
<param name="File" value="/logs/app.log" />
对于JVM1,设置JVM属性: log4j.logFile =/logs/ jvm1 /app.log
For JVM1 set the JVM property: log4j.logFile=/logs/jvm1/app.log
对于JVM2,设置JVM属性: log4j.logFile =/logs/ jvm2 /app.log
For JVM2 set the JVM property: log4j.logFile=/logs/jvm2/app.log
有关更多详细信息和其他选项: 请参阅: link1 , 链接2
For more detail and other options: Refer: link1, link2
这篇关于集群环境中的Log4j的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!