使用 log4j 处理多个应用程序的应用程序级日志在 weblogic 上不起作用
我使用 weblogic 在服务器上部署了两个应用程序。我为每个应用程序创建了单独的属性文件和配置 servlet。但出现的问题是记录器附加到最新部署的应用程序的日志文件中。代码在 tomcat 上运行良好,但是当部署在 weblogic 服务器上时,它的行为就像只有一个实例正在运行,而且也是最新的实例。
我放入 WEB-INF 中的 log4j.properties 文件:
log4j.rootLogger=INFO, R
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=C:abc.log
log4j.appender.R.MaxFileSize=10MB
log4j.appender.R.MaxBackupIndex=10
log4j.appender.R.Append=true
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d %-5p %c (%F:%L) - %m%n
我已将 log4j-1.2.14.jar 放入所有库所在的 lib 文件夹 中。
在 servlet init() 方法中初始化它:
public void init() throws ServletException {
logger.info("Servlet...................");
super.init();
String prefix = getServletContext().getRealPath("/");
String log4j = getServletContext().getInitParameter"log4jConfig");
if (log4j != null) {
PropertyConfigurator.configure(prefix + log4j);
}
}
web.xml 条目是:
<context-param>
<param-name>log4jConfig</param-name>
<param-value>/WEB-INF/log4j.properties</param-value>
</context-param>
I have two applications deployed on server using weblogic. I have created separate properties files and configuration servlet for each application. But the problem occurring is that the logger appends to the log file of the application which is deployed latest. The code working fine on tomcat but when deployed on weblogic server it behaves like only one instance is working and that too is latest one.
log4j.properties file that i put in WEB-INF:
log4j.rootLogger=INFO, R
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=C:abc.log
log4j.appender.R.MaxFileSize=10MB
log4j.appender.R.MaxBackupIndex=10
log4j.appender.R.Append=true
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d %-5p %c (%F:%L) - %m%n
I had put the log4j-1.2.14.jar in lib folder where all the libraries are residing.
Inititalising it in servlet init() method:
public void init() throws ServletException {
logger.info("Servlet...................");
super.init();
String prefix = getServletContext().getRealPath("/");
String log4j = getServletContext().getInitParameter"log4jConfig");
if (log4j != null) {
PropertyConfigurator.configure(prefix + log4j);
}
}
and web.xml entry is:
<context-param>
<param-name>log4jConfig</param-name>
<param-value>/WEB-INF/log4j.properties</param-value>
</context-param>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该了解类加载器的结构:
以下是类加载器的层次结构的解释:
例如,如果您将 log4j.jar 放入通用类加载器,到 Tomcat 的 $CATALINA_BASE/lib ,所有 Web 应用程序将共享同一个类,并且您将只能为所有 Web 应用程序拥有一个 log4j 配置。
解决问题的最简单方法是将 log4j.jar 的单独副本放入 Web 应用程序的 WEB-INF/lib 文件夹中,将 log4j.properties 文件放入 Web 应用程序的 WEB-INF/classes 文件夹中。请注意,在这种情况下,您根本不需要任何 log4j 初始化。 这里是有关配置的文档Tomcat 的 log4j。
更新
正如 Weblogic 中所述,类加载模型与 Tomcat 略有不同:
请尝试在 Weblogic 中将 设置为 true。之后,每个 Web 应用程序可能都会获得它自己的类版本。
You should understand a structure of class loaders:
Here are hierarchies of class loaders explained:
If you will put your log4j.jar to Common classloader, for instance, to $CATALINA_BASE/lib for Tomcat, all web applications will share the same class, and you will be able to have only one log4j configuration for all web applications.
The easiest way to solve your problem is to put separate copies of log4j.jar to WEB-INF/lib folders and log4j.properties files to WEB-INF/classes folders of your web applications. Note that in this case you will not need any log4j initialization at all. Here is a documentation on configuration of log4j for Tomcat.
UPDATED
As said in Weblogic, class loading model slightly differs from Tomcat:
Please try to set <prefer-web-inf-classes> to true in Weblogic. After that, each web application probably will get it's own version of class.