如何加载我的log4j.xml文件,该文件不在类路径中并且存在于D盘中?

时间:2015-05-21 05:22:24

标签: spring spring-mvc logging log4j

我想通过xml配置log4j的日志记录;它存在于我的D驱动器中,而不是存在于类路径中。

我正在使用Spring MVC。

以下是我的log4j.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="true">

<appender name="console" class="org.apache.log4j.ConsoleAppender">
    <param name="Target" value="System.out" />
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%-5p %c{1} - %m%n" />
    </layout>
</appender>

<appender name="info-log" class="org.apache.log4j.FileAppender">
    <param name="File" value="d:/temp/info.log" />
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%-5p %c{1} - %m%n" />
    </layout>
    <filter class="org.apache.log4j.varia.LevelRangeFilter">
        <param name="LevelMin" value="debug" />
        <param name="LevelMax" value="info" />
        <param name="AcceptOnMatch" value="true" />
    </filter>
</appender>

<appender name="error-log" class="org.apache.log4j.FileAppender">
    <param name="File" value="d:/temp/error.log" />
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%-5p %c{1} - %m%n" />
    </layout>
    <filter class="org.apache.log4j.varia.LevelRangeFilter">
        <param name="LevelMin" value="warn" />
        <param name="LevelMax" value="fatal" />
        <param name="AcceptOnMatch" value="true" />
    </filter>
</appender>

<logger name="com.mvc.login.LoginController">
    <appender-ref ref="info-log" />
    <appender-ref ref="error-log" />
</logger>

<root>
    <level value="debug" />
    <appender-ref ref="console" />
</root>

1 个答案:

答案 0 :(得分:1)

web.xml中添加上下文参数和监听器类,如下所示

<context-param>
    <param-name>log4jConfigLocation</param-name>
    <param-value>path/to/log4j.xml</param-value>
</context-param>

<listener>
    <listener-class>
        com.something.utils.Log4jConfigListener</listener-class>
</listener>

然后实现如下的监听器类Log4jConfigListener

import java.io.File;
import java.io.IOException;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.log4j.LogManager;
import org.apache.log4j.xml.DOMConfigurator;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

public class Log4jConfigListener implements ServletContextListener {

@Override
public void contextDestroyed(ServletContextEvent servletcontextevent) {
    LogManager.shutdown();
}

@Override
public void contextInitialized(ServletContextEvent servletcontextevent) {

    ServletContext context = servletcontextevent.getServletContext();

    String path = null;

    path = context.getInitParameter("log4jConfigLocation");

    PathMatchingResourcePatternResolver pathResolver = new PathMatchingResourcePatternResolver();
    Resource[] resources = null;
    try {
        resources = pathResolver.getResources(path);

        for (Resource resource : resources) {
            File file = resource.getFile();
            path = file.getAbsolutePath();
            break; // read only the first configuration
        }
    } catch (IOException e) {
        context.log("Unable to load log4j configuration file", e);
    }

    LogManager.resetConfiguration();
    DOMConfigurator.configure(path);
}

}