在spring boot应用程序中从classpath以外的其他位置配置log4j.xml

时间:2017-03-09 11:38:34

标签: spring spring-mvc logging log4j slf4j

我在文件中使用sl4j logger打印日志,我已经配置了以下log4j.xml文件,因为我在JBOSS上部署我的spring应用程序它没有像tomcat那样创建目录结构所以我无法配置调试级别log,我希望我的应用程序从d:\ configuration之类的不同位置选择log4j.xml,以便我可以为我的应用程序配置调试级别如何做到这一点?我没有web.xml。 我尝试过使用PropertyPlaceholderConfigurer类但是由于文件存在而找不到文件会出错

<appender name="console" class="org.apache.log4j.ConsoleAppender">
        <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern"
            value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
        </layout>
    </appender>

    <appender name="file" class="org.apache.log4j.RollingFileAppender">
        <param name="append" value="false" />
        <param name="file" value="/home/client/webApp.log"/>
        <param name="maxFileSize" value="5MB" />
        <param name="maxBackupIndex" value="5" />
        <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" 
            value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
       </layout>
    </appender>
<root>
        <priority value="DEBUG" />
        <appender-ref ref="file" />
    </root>

1 个答案:

答案 0 :(得分:0)

首先在application.properties设置属性即。 server.context_parameters.log4jConfigLocation=path/to/log4j.xml

然后实现一个监听器类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);
    }

}

接下来将配置类中的侦听器注册为@Bean

@Bean
public Log4jConfigListener log4jConfigListener() {
   return new Log4jConfigListener();
}

更新pom.xml以排除默认的logback配置并包含log4j,如下所示

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter</artifactId>
   <exclusions>
      <exclusion>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-logging</artifactId>
      </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-log4j</artifactId>
</dependency>

请在评论中了解更多信息。

P.S。:对于非Spring Boot(或传统的web.xml ),请参阅我的回答here