简体   繁体   English

将log4j.xml放在Tomcat 7上的哪里?

[英]Where to put log4j.xml on tomcat 7?

I'm currently developing a Java Struts2 webapp (using Maven) and have successfully configured the use of datasources to connect with the database. 我目前正在开发Java Struts2网络应用程序(使用Maven),并且已经成功配置了数据源的使用以与数据库连接。 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). 由于May应用程序需要具有在环境之间移动的能力(“开发”>“同质化”>“生产”),因此它必须使用服务器配置文件来加载参数(例如数据源和日志级别)。

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). 我找不到有关如何配置Java Web应用程序以从应用程序外部(WAR之外)使用log4j.xml文件的任何参考。 With maven I simply put the log4j.xml inside my src/main/resources folder and it loads automatically. 使用Maven,我只需将log4j.xml放在src/main/resources文件夹中,即可自动加载。 What should I do to ensure that my webapp loads a external log4j.xml file on deploy? 如何确保我的Web应用程序在部署时加载外部log4j.xml文件?

Maven by default places all files from src/main/resources into /WEB-INF/classes/ . Maven默认将src/main/resources所有文件放入/WEB-INF/classes/ With log4j, you can specify where your configuration file is located with help of Web App Deployment Descriptor ( web.xml ). 使用log4j,可以在Web App部署描述符( web.xml )的帮助下指定配置文件的位置。 Simply, add log4j listener: 只需添加log4j侦听器:

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

And context parameter: 和上下文参数:

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

classpath: is equivalent to /WEB-INF/classes , you may specify any path for example, with ${catalina.home} , a tomcat specific variable. classpath:等效于/WEB-INF/classes ,您可以指定任何路径,例如,使用${catalina.home} (tomcat特定变量)。 Use it with file:// prefix file://前缀一起使用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM