简体   繁体   English

如何将log4j.properties文件的路径更改为特定路径?

[英]How to change the path of the log4j.properties file to a specific one?

Cheers, 干杯,

I tried to change the file-path of the log4j.properties from /WEB-INF/classes to /WEB-INF/lib. 我试图将log4j.properties的文件路径从/ WEB-INF / classes更改为/ WEB-INF / lib。 Is there any way to archive this? 有什么办法可以存档吗? I tried context-param to set the path but I did not archive my goal. 我尝试使用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>

These are the outputs from the tomcat7-stdout.2015-04-01.log 这些是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:

I also tried to use a Listener: 我也尝试使用监听器:

<listener>
<listener-class>com.company.ecm.appsrv.logging.impl.LoggingSetup</listener-class>
</listener>

But doesn't seem to change anything. 但似乎并没有改变任何东西。

LoggingSetup.java: 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 */ }
            }
        }
    }
}

Complete web.xml: 完整的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>

Issue gut fixed. 问题肠已修复。 I just messed up with the Java code and my supervisor was back in the office this time and helped me out. 我只是搞砸了Java代码,这次主管回到了办公室,并帮助了我。 Thanks four your help vijay. 谢谢四位朋友的帮助。

Set following context param: 设置以下上下文参数:

  <context-param>
    <param-name>log4jConfigLocation</param-name>
    <param-value>/WEB-INF/config/log4j/log4j.properties</param-value>
  </context-param>

Don't forget to add 不要忘记添加

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

in web.xml 在web.xml中

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

相关问题 如何在Websphere中为log4j.properties文件设置相对路径 - How to set relative path for log4j.properties file in Websphere 如何使用log4j.properties更改log4j 1.2.17的备份文件名 - how to change backup file name of log4j 1.2.17 using log4j.properties 如何在服务器上使用-Dlog4j.configuration = file:/path/to/log4j.properties运行我的flink作业 - how to run my flink job with -Dlog4j.configuration=file:/path/to/log4j.properties on server 为“ log4j.properties”文件设置绝对路径 - set an absolute path for a “log4j.properties” file 如何在不使用绝对路径的情况下使用log4j.properties为Java Web Service创建日志文件 - How to use log4j.properties to create a log file for a Java Web Service without using an absolute path 在log4j.properties中,带空间的每日附加器文件路径不起作用 - In log4j.properties Daily appender file path with space not working 如何在log4j.properties文件中互助追加器属性? - How to mutualize appenders properties in log4j.properties file? 如何将 log4j.properties 文件转换为 log4j2.xml 或 log4j2.properties 文件 - How to convert the log4j.properties file to log4j2.xml or log4j2.properties file 如何在Eclipse中的项目类路径中添加log4j.properties文件的路径? - How to add path of log4j.properties file in my project classpath in eclipse? 如何覆盖log4j.properties的特定值? - How to override a specific value of log4j.properties?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM