2015-04-22 92 views
2

我有一个项目,在启动SpringApplication之前需要日志记录机制。我怎样才能做到这一点?Spring应用程序启动之前的Spring启动安装日志记录

我试图设置自己的日志记录机制(LogManager.getLogManager()。readConfiguration()),但它在Spring应用程序启动时被覆盖。

基本上我想在任何地方使用相同的日志记录机制。

回答

0

我设法从我的项目中删除“弹簧引导启动日志记录”的依赖,增加“组织来解决我的问题.slf4j:slf4j-jdk14:1.7.5'和'commons-logging:commons-logging:1.1.1'。

我使用gradle这个所以我来说,我做到这一点的:

compile("org.springframework.boot:spring-boot-starter-web") { 
    exclude module: "spring-boot-starter-logging" 
} 
compile("org.springframework.boot:spring-boot-starter-actuator") { 
    exclude module: "spring-boot-starter-logging" 
} 

compile('org.slf4j:slf4j-jdk14:1.7.5') 
compile('commons-logging:commons-logging:1.1.1') 

删除LoggingApplicationListener和logback.xml没有奏效。

0

您可以在web.xml代替弹簧的context.xml

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

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

所以它是春天开始之前配置Log4j听众。

+0

我的项目没有web.xml文件,我宁愿不使用它。我也有使用Java日志记录机制的依赖项目。 – telebog

1

Spring Boot使用LoggingApplicationListener为您的应用程序配置日志记录。此侦听器是SpringApplication的默认侦听器之一。要使用您自己的已配置日志记录系统,您需要配置您的SpringApplication,以便它没有此侦听器。例如,删除不需要的听众,同时保留所有其他默认听众:

@SpringBootApplication 
public class CustomLoggingApplication { 

    public static void main(String[] args) {   
     SpringApplication application = 
       new SpringApplication(CustomLoggingApplication.class); 

     Collection<ApplicationListener<?>> listeners = 
       new ArrayList<ApplicationListener<?>>(); 
     for (ApplicationListener<?> listener: application.getListeners()) { 
      if (!(listener instanceof LoggingApplicationListener)) { 
       listeners.add(listener); 
      } 
     } 
     application.setListeners(listeners); 

     application.run(args); 
    } 

} 
+0

这段代码对我的情况没有影响 – telebog