【问题标题】:configuring log4j.xml from different location other than classpath in spring boot application从 Spring Boot 应用程序中的类路径以外的其他位置配置 log4j.xml
【发布时间】:2017-07-30 09:11:59
【问题描述】:

我在文件中使用 sl4j 记录器打印日志,我配置了以下 log4j.xml 文件,因为我在 JBOSS 上部署我的 spring 应用程序它不会创建像 tomcat 这样的目录结构,所以我无法配置调试级别日志,我想让我的应用程序从 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>

【问题讨论】:

标签: spring spring-mvc logging log4j slf4j


【解决方案1】:

首先在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>

请在 cmets 中了解更多信息。

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

【讨论】:

  • 我已经尝试过了,但日志打印在标准输出上,不在文件中,不在文件中,我在 jboss 上部署它
  • 请参考pom.xml中修改的更新答案
  • 抱歉回复晚了,但我没有在文件中获取日志,也没有错误..!
  • 我可以使用 Spring Boot 版本 1.5.1.RELEASE、spring-boot-starter-log4j 版本来配置和运行设置。 1.3.8.RELEASE 和 JDK 1.8.0_121
  • 我可以建议的一个快速检查是Log4jConfigListener bean 是否被拾取。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-05-20
  • 2021-12-16
  • 2018-06-20
  • 2016-11-24
  • 2020-11-09
  • 2017-11-04
  • 2015-11-11
相关资源
最近更新 更多