我如何加载我的log4j.xml文件,该文件不在classpath中,但它存在于D盘?[英] How can i load my log4j.xml file which is not in classpath and it present in D drive?

本文是小编为大家收集整理的关于我如何加载我的log4j.xml文件,该文件不在classpath中,但它存在于D盘?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我想通过XML配置log4j的记录;这是我的D驱动器中存在的,不是classpath上的.

我正在使用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>

推荐答案

在您的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);
}

}

本文地址:https://www.itbaoku.cn/post/1575154.html

问题描述

I want to configure logging with log4j via xml; which is present in my D drive and not on classpath.

I am using Spring MVC.

Below is my 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>

推荐答案

In your web.xml add a context parameter and listener class as below

<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>

Then implement the listener class Log4jConfigListener as below

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);
}

}