Java Spring应用程序启动前的Spring启动设置日志记录
我有一个项目,在启动Spring应用程序之前需要日志机制。我怎样才能做到这一点 我试图设置我自己的日志机制(LogManager.getLogManager().readConfiguration()),但它在spring应用程序启动时被覆盖Java Spring应用程序启动前的Spring启动设置日志记录,java,spring,spring-boot,Java,Spring,Spring Boot,我有一个项目,在启动Spring应用程序之前需要日志机制。我怎样才能做到这一点 我试图设置我自己的日志机制(LogManager.getLogManager().readConfiguration()),但它在spring应用程序启动时被覆盖 基本上,我希望在任何地方都使用相同的日志机制。您可以在web.xml而不是spring-context.xml中配置Log4j监听器 <context-param> <param-name>log4jConfigLocati
基本上,我希望在任何地方都使用相同的日志机制。您可以在web.xml而不是spring-context.xml中配置Log4j监听器
<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>
log4jConfigLocation
/WEB-INF/classes/log4j.properties
org.springframework.web.util.Log4jConfigListener
因此,它在Spring启动之前就启动了。Spring Boot使用
LoggingApplicationListener
为应用程序配置日志记录。此侦听器是SpringApplication
的默认侦听器之一。要使用您自己已经配置好的日志系统,您需要配置SpringApplication
,这样它就没有这个监听器了。例如,要删除不需要的侦听器,同时保留所有其他默认侦听器,请执行以下操作:
@springboot应用程序
公共类CustomLogging应用程序{
公共静态void main(字符串[]args){
SpringApplication应用程序=
新的SpringApplication(CustomLoggingApplication.class);
收藏>();
for(ApplicationListener侦听器:application.getListeners()){
if(!(记录应用程序侦听器的侦听器实例)){
添加(侦听器);
}
}
application.setListeners(侦听器);
application.run(args);
}
}
我通过从项目中删除“spring boot starter logging”依赖项并添加“org.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不起作用。我的项目没有web.xml文件,我不想使用它。我还有一个使用Java日志机制的依赖项目。