将log4j.xml放在Tomcat 7上的哪里? [英] Where to put log4j.xml on tomcat 7?

查看:99
本文介绍了将log4j.xml放在Tomcat 7上的哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发Java Struts2网络应用程序(使用Maven),并且已经成功配置了数据源的使用以与数据库连接.由于May应用程序需要能够在环境之间移动(开发>同源>生产),因此它必须使用服务器配置文件来加载参数(例如数据源和日志级别).

I'm currently developing a Java Struts2 webapp (using Maven) and have successfully configured the use of datasources to connect with the database. Since May application needs the ability of being moved between environments (Development > Homologation > Production), it must use server config files to load paramenters (such as the datasources and log levels).

我找不到有关如何配置Java Web应用程序以从应用程序外部(WAR之外)使用log4j.xml文件的任何参考.使用Maven,我只需将log4j.xml放入我的src/main/resources文件夹中,它将自动加载.如何确保我的Web应用程序在部署时加载外部log4j.xml文件?

I can't find any references on how to configure a java webapp to use a log4j.xml file from outside the application (out of the WAR). With maven I simply put the log4j.xml inside my src/main/resources folder and it loads automatically. What should I do to ensure that my webapp loads a external log4j.xml file on deploy?

推荐答案

Maven默认将所有文件从src/main/resources放入/WEB-INF/classes/.使用log4j,可以在Web App部署描述符(web.xml)的帮助下指定配置文件的位置.只需添加log4j侦听器:

Maven by default places all files from src/main/resources into /WEB-INF/classes/. With log4j, you can specify where your configuration file is located with help of Web App Deployment Descriptor (web.xml). Simply, add log4j listener:

<listener>
    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>

和上下文参数:

<context-param>
    <param-name>log4jConfigLocation</param-name>
    <param-value>classpath:log4j.xml</param-value>
</context-param>

classpath:等效于/WEB-INF/classes,您可以指定任何路径,例如,使用toccat特定变量${catalina.home}.与file://前缀

classpath: is equivalent to /WEB-INF/classes, you may specify any path for example, with ${catalina.home}, a tomcat specific variable. Use it with file:// prefix

这篇关于将log4j.xml放在Tomcat 7上的哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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