如何将 log4j.properties 文件的路径更改为特定路径?
How to change the path of the log4j.properties file to a specific one?
干杯,
我试图将 log4j.properties 的文件路径从 /WEB-INF/classes 更改为 /WEB-INF/lib。有什么办法可以存档吗?
我尝试使用 context-param 来设置路径,但我没有归档我的目标。
<context-param>
<!-- specifiy <path> to a log4j.properties file to superseed shipped version -->
<param-name>LOG4J_PROPERTIES</param-name>
<param-value>/lib/log4j.properties</param-value>
</context-param>
这些是 tomcat7-stdout.2015-04-01.log
的输出
----------> Parent Classloader:
org.apache.catalina.loader.StandardClassLoader@2bb57fd1
.
log4j: Trying to find [log4j.properties] using org.apache.catalina.loader.StandardClassLoader@2bb57fd1 class loader.
log4j: Trying to find [log4j.properties] using ClassLoader.getSystemResource().
log4j: Could not find resource: [null].
[EL Info]: 2015-04-01 09:05:07.764--ServerSession(598705739)--EclipseLink, version: Eclipse Persistence Services - 2.5.1.v20130918-f2b9fc5
[EL Info]: connection: 2015-04-01 09:05:08.246--ServerSession(598705739)--file:/C:/Program Files/Apache Software Foundation/Tomcat 7.0/webapps/archive/WEB-INF/lib/ot-ads.jar_AdministrationStore_url=jdbc:sqlserver://WIN-500S3SD3IQB:1433;databaseName=ECR_user=ecr login successful
started AS_BIZADMIN API Service
log4j: Trying to find [log4j.xml] using context classloader WebappClassLoader
context: /DynamicLogService
delegate: false
repositories:
我也试过使用监听器:
<listener>
<listener-class>com.company.ecm.appsrv.logging.impl.LoggingSetup</listener-class>
</listener>
但似乎没有任何改变。
LoggingSetup.java:
package com.company.ecm.appsrv.logging.impl;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
public class LoggingSetup
implements ServletContextListener
{
@Override
public void contextDestroyed(ServletContextEvent sce)
{
// Nope
}
@Override
public void contextInitialized(ServletContextEvent sce)
{
ServletContext sctx = sce.getServletContext();
Properties props = new Properties();
InputStream ins = null;
try
{
ins = sctx.getResourceAsStream("/WEB-INF/lib/log4j.properties");
if(ins == null)
throw new RuntimeException("Could not find log4j properties");
props.load(ins);
String ctxName = sctx.getContextPath().substring(1);
props.put("contextname", ctxName);
PropertyConfigurator.configure(props);
Logger.getRootLogger().info("Loggin set up.");
}
catch(IOException ex)
{
ex.printStackTrace();
sctx.log("Could not setup logging", ex);
}
finally
{
if(ins != null)
{
try { ins.close(); } catch(IOException ex) { /* ignored */ }
}
}
}
}
完成web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems,
Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/j2ee/dtds/web-app_2_3.dtd">
<web-app>
<listener>
<listener-class>com.company.ecm.appsrv.logging.impl.LoggingSetup</listener-class>
</listener>
<context-param>
<!-- specifiy <path> to a log4j.properties file to superseed shipped version -->
<param-name>LOG4J_PROPERTIES</param-name>
<param-value>/lib/log4j.properties</param-value>
</context-param>
<listener>
<listener-class>
com.sun.xml.ws.transport.http.servlet.WSServletContextListener
</listener-class>
</listener>
<servlet>
<servlet-name>jaxws</servlet-name>
<servlet-class>
com.sun.xml.ws.transport.http.servlet.WSServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jaxws</servlet-name>
<url-pattern>/services</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>120</session-timeout>
</session-config>
</web-app>
设置以下上下文参数:
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/config/log4j/log4j.properties</param-value>
</context-param>
别忘了加
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
在web.xml
问题已解决。我刚刚搞砸了 Java 代码,这次我的主管回到办公室并帮助了我。感谢四位 vijay 的帮助。
干杯,
我试图将 log4j.properties 的文件路径从 /WEB-INF/classes 更改为 /WEB-INF/lib。有什么办法可以存档吗? 我尝试使用 context-param 来设置路径,但我没有归档我的目标。
<context-param>
<!-- specifiy <path> to a log4j.properties file to superseed shipped version -->
<param-name>LOG4J_PROPERTIES</param-name>
<param-value>/lib/log4j.properties</param-value>
</context-param>
这些是 tomcat7-stdout.2015-04-01.log
的输出----------> Parent Classloader:
org.apache.catalina.loader.StandardClassLoader@2bb57fd1
.
log4j: Trying to find [log4j.properties] using org.apache.catalina.loader.StandardClassLoader@2bb57fd1 class loader.
log4j: Trying to find [log4j.properties] using ClassLoader.getSystemResource().
log4j: Could not find resource: [null].
[EL Info]: 2015-04-01 09:05:07.764--ServerSession(598705739)--EclipseLink, version: Eclipse Persistence Services - 2.5.1.v20130918-f2b9fc5
[EL Info]: connection: 2015-04-01 09:05:08.246--ServerSession(598705739)--file:/C:/Program Files/Apache Software Foundation/Tomcat 7.0/webapps/archive/WEB-INF/lib/ot-ads.jar_AdministrationStore_url=jdbc:sqlserver://WIN-500S3SD3IQB:1433;databaseName=ECR_user=ecr login successful
started AS_BIZADMIN API Service
log4j: Trying to find [log4j.xml] using context classloader WebappClassLoader
context: /DynamicLogService
delegate: false
repositories:
我也试过使用监听器:
<listener>
<listener-class>com.company.ecm.appsrv.logging.impl.LoggingSetup</listener-class>
</listener>
但似乎没有任何改变。
LoggingSetup.java:
package com.company.ecm.appsrv.logging.impl;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
public class LoggingSetup
implements ServletContextListener
{
@Override
public void contextDestroyed(ServletContextEvent sce)
{
// Nope
}
@Override
public void contextInitialized(ServletContextEvent sce)
{
ServletContext sctx = sce.getServletContext();
Properties props = new Properties();
InputStream ins = null;
try
{
ins = sctx.getResourceAsStream("/WEB-INF/lib/log4j.properties");
if(ins == null)
throw new RuntimeException("Could not find log4j properties");
props.load(ins);
String ctxName = sctx.getContextPath().substring(1);
props.put("contextname", ctxName);
PropertyConfigurator.configure(props);
Logger.getRootLogger().info("Loggin set up.");
}
catch(IOException ex)
{
ex.printStackTrace();
sctx.log("Could not setup logging", ex);
}
finally
{
if(ins != null)
{
try { ins.close(); } catch(IOException ex) { /* ignored */ }
}
}
}
}
完成web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems,
Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/j2ee/dtds/web-app_2_3.dtd">
<web-app>
<listener>
<listener-class>com.company.ecm.appsrv.logging.impl.LoggingSetup</listener-class>
</listener>
<context-param>
<!-- specifiy <path> to a log4j.properties file to superseed shipped version -->
<param-name>LOG4J_PROPERTIES</param-name>
<param-value>/lib/log4j.properties</param-value>
</context-param>
<listener>
<listener-class>
com.sun.xml.ws.transport.http.servlet.WSServletContextListener
</listener-class>
</listener>
<servlet>
<servlet-name>jaxws</servlet-name>
<servlet-class>
com.sun.xml.ws.transport.http.servlet.WSServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jaxws</servlet-name>
<url-pattern>/services</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>120</session-timeout>
</session-config>
</web-app>
设置以下上下文参数:
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/config/log4j/log4j.properties</param-value>
</context-param>
别忘了加
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
在web.xml
问题已解决。我刚刚搞砸了 Java 代码,这次我的主管回到办公室并帮助了我。感谢四位 vijay 的帮助。