如何在Spring Boot中关闭调试日志消息 [英] how turn off debug log messages in spring boot

查看:2454
本文介绍了如何在Spring Boot中关闭调试日志消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我阅读了spring boot docs( https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-logging.html )

您还可以在application.properties中指定debug = true"

所以我想我可以通过在application.properties中添加debug = false来关闭调试日志.我做到了,但不幸的是它没有用.然后我读了同一个文档

日志记录系统在应用程序生命周期的早期进行了初始化,因此在通过@PropertySource注释加载的属性文件中找不到此类日志记录属性"

由于日志记录是在创建ApplicationContext之前初始化的,因此无法通过Spring @Configuration文件中的@PropertySources控制日志记录."

如果可能,我们建议您将-spring变体用于日志记录配置",因此我在src/main/resources中添加了一个名为log4j-spring.properties的文件.

在这样的文件中,我添加了debug=false(仅此行,没有其他内容),但是我仍然看到所有当前日期" [main] DEBUG ...消息.因此,我的问题是当我将其部署到生产环境时,如何关闭Spring Boot应用程序中的调试消息.有没有推荐的方法可以通过Maven做到这一点?

*****在2月12日添加

两种主要方法: 1)通过使用AnnotationConfigApplicationContext:

public class DemoAppNoBoot {
       public static void main(String[] args) {
              AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
                           BatchConfiguration.class);
              JobLauncher jobLauncher = (JobLauncher) context.getBean("jobLauncher");
              Job job = (Job) context.getBean("job");
               try {
                     JobExecution execution = jobLauncher.run(job, new JobParameters());
              } catch (Exception e) {
                     e.printStackTrace();
              }
       }
}

摘要输出:

08:26:18.713 [main] DEBUG o.s.core.env.StandardEnvironment - Adding [systemProperties] PropertySource with lowest search precedence
08:26:18.744 [main] DEBUG o.s.core.env.StandardEnvironment - Adding [systemEnvironment] PropertySource with lowest search precedence
08:26:18.744 [main] DEBUG o.s.core.env.StandardEnvironment - Initialized StandardEnvironment with PropertySources [systemProperties,systemEnvironment]
08:26:18.947 [main] INFO  o.s.c.a.AnnotationConfigApplicationContext - Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@532760d8: startup date [Fri Feb 12 08:26:18 CST 2016]; root of context hierarchy
08:26:18.947 [main] DEBUG o.s.c.a.AnnotationConfigApplicationContext - Bean factory for org.springframework.context.annotation.AnnotationConfigApplicationContext@532760d8: org.springframework.beans.factory.support.DefaultListableBeanFactory@50b494a6: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.event.internalEventListenerProcessor,org.springframework.context.event.internalEventListenerFactory,batchConfiguration]; root of factory hierarchy
08:26:18.979 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating shared instance of singleton bean 
...
08:26:32.560 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'lifecycleProcessor'
08:26:32.560 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.context.annotation.internalScheduledAnnotationProcessor'
08:26:32.560 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor'
08:26:32.560 [main] DEBUG o.s.s.a.ScheduledAnnotationBeanPostProcessor - Could not find default TaskScheduler bean
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.scheduling.TaskScheduler] is defined     at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:372) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]      at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:332) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
...
08:26:33.529 [pool-1-thread-1] DEBUG o.s.jdbc.core.JdbcTemplate - Executing prepared SQL update
08:26:33.529 [pool-1-thread-1] DEBUG o.s.jdbc.core.JdbcTemplate - Executing prepared SQL statement [UPDATE BATCH_JOB_EXECUTION set START_TIME = ?, END_TIME = ?,  STATUS = ?, EXIT_CODE = ?, EXIT_MESSAGE = ?, VERSION = ?, CREATE_TIME = ?, LAST_UPDATED = ? where JOB_EXECUTION_ID = ? and VERSION = ?]
08:26:33.529 [pool-1-thread-1] DEBUG o.s.jdbc.core.JdbcTemplate - SQL update affected 1 rows
08:26:33.545 [pool-1-thread-1] DEBUG o.s.j.d.DataSourceTransactionManager - Initiating transaction commit
08:26:33.545 [pool-1-thread-1] DEBUG o.s.j.d.DataSourceTransactionManager - Committing JDBC transaction on Connection [org.hsqldb.jdbc.JDBCConnection@33bbce9c]
08:26:33.545 [pool-1-thread-1] DEBUG o.s.j.d.DataSourceTransactionManager - Releasing JDBC Connection [org.hsqldb.jdbc.JDBCConnection@33bbce9c] after transaction
08:26:33.545 [pool-1-thread-1] DEBUG o.s.jdbc.datasource.DataSourceUtils - Returning JDBC Connection to DataSource
08:26:33.545 [pool-1-thread-1] INFO  o.s.b.c.l.support.SimpleJobLauncher - Job: [SimpleJob: [name=job1]] completed with the following parameters: [{}] and the following status: [COMPLETED]

2)使用SpringApplication.run的主要方法

@SpringBootApplication
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(BatchConfiguration.class, args);
    }
}

整个输出:

: Spring Boot ::        (v1.3.1.RELEASE)
2016-02-12 08:02:36.145  INFO 12172 --- [           main] com.example.DemoApplication              : Starting DemoApplication on GH-VDIKCISV252 with PID 12172 (C:\STS\wsRestTemplate\demo\target\classes started by e049447 in C:\STS\wsRestTemplate\demo)
2016-02-12 08:02:36.145  INFO 12172 --- [           main] com.example.DemoApplication              : No active profile set, falling back to default profiles: default
2016-02-12 08:02:36.473  INFO 12172 --- [           main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@4e4aea35: startup date [Fri Feb 12 08:02:36 CST 2016]; root of context hierarchy
2016-02-12 08:02:42.176  WARN 12172 --- [           main] o.s.c.a.ConfigurationClassEnhancer       : @Bean method ScopeConfiguration.stepScope is non-static and returns an object assignable to Spring's BeanFactoryPostProcessor interface. This will result in a failure to process annotations such as @Autowired, @Resource and @PostConstruct within the method's declaring @Configuration class. Add the 'static' modifier to this method to avoid these container lifecycle issues; see @Bean javadoc for complete details.
2016-02-12 08:02:42.349  WARN 12172 --- [           main] o.s.c.a.ConfigurationClassEnhancer       : @Bean method ScopeConfiguration.jobScope is non-static and returns an object assignable to Spring's BeanFactoryPostProcessor interface. This will result in a failure to process annotations such as @Autowired, @Resource and @PostConstruct within the method's declaring @Configuration class. Add the 'static' modifier to this method to avoid these container lifecycle issues; see @Bean javadoc for complete details.
2016-02-12 08:02:42.724  INFO 12172 --- [           main] o.s.j.d.e.EmbeddedDatabaseFactory        : Starting embedded database: url='jdbc:hsqldb:mem:testdb', username='sa'
2016-02-12 08:02:43.802  WARN 12172 --- [           main] o.s.b.c.l.AbstractListenerFactoryBean    : org.springframework.batch.item.ItemReader is an interface.  The implementing class will not be queried for annotation based listener configurations.  If using @StepScope on a @Bean method, be sure to return the implementing class so listner annotations can be used.
2016-02-12 08:02:44.990  INFO 12172 --- [           main] o.s.jdbc.datasource.init.ScriptUtils     : Executing SQL script from class path resource [org/springframework/batch/core/schema-hsqldb.sql]
2016-02-12 08:02:45.179  INFO 12172 --- [           main] o.s.jdbc.datasource.init.ScriptUtils     : Executed SQL script from class path resource [org/springframework/batch/core/schema-hsqldb.sql] in 189 ms.
2016-02-12 08:02:46.804  INFO 12172 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2016-02-12 08:02:46.868  INFO 12172 --- [           main] o.s.b.a.b.JobLauncherCommandLineRunner   : Running default command line with: []
2016-02-12 08:02:46.962  INFO 12172 --- [           main] o.s.b.c.r.s.JobRepositoryFactoryBean     : No database type set, using meta data indicating: HSQL
2016-02-12 08:02:47.040  INFO 12172 --- [pool-2-thread-1] o.s.b.c.r.s.JobRepositoryFactoryBean     : No database type set, using meta data indicating: HSQL
2016-02-12 08:02:47.243  INFO 12172 --- [           main] o.s.b.c.l.support.SimpleJobLauncher      : No TaskExecutor has been set, defaulting to synchronous executor.
2016-02-12 08:02:47.259  INFO 12172 --- [pool-2-thread-1] o.s.b.c.l.support.SimpleJobLauncher      : No TaskExecutor has been set, defaulting to synchronous executor.
2016-02-12 08:02:47.321  INFO 12172 --- [           main] o.s.b.c.l.support.SimpleJobLauncher      : Job: [SimpleJob: [name=job1]] launched with the following parameters: [{run.id=1}]
2016-02-12 08:02:47.368  INFO 12172 --- [           main] o.s.batch.core.job.SimpleStepHandler     : Executing step: [step1]
2016-02-12 08:02:47.400  INFO 12172 --- [           main] com.example.CustomItemReader             : read method - collecting the MYAPP2 out file names
2016-02-12 08:02:47.525  INFO 12172 --- [           main] com.example.CustomItemReader             : read method - no file found
2016-02-12 08:02:47.556  INFO 12172 --- [           main] o.s.b.c.l.support.SimpleJobLauncher      : Job: [SimpleJob: [name=job1]] completed with the following parameters: [{run.id=1}] and the following status: [COMPLETED]
2016-02-12 08:02:47.556  INFO 12172 --- [           main] com.example.DemoApplication              : Started DemoApplication in 12.1 seconds (JVM running for 13.405)
2016-02-12 08:02:47.556  INFO 12172 --- [pool-2-thread-1] o.s.b.c.l.support.SimpleJobLauncher      : Job: [SimpleJob: [name=job1]] launched with the following parameters: [{}]
2016-02-12 08:02:47.618  INFO 12172 --- [pool-2-thread-1] o.s.batch.core.job.SimpleStepHandler     : Executing step: [step1]
2016-02-12 08:02:47.634  INFO 12172 --- [pool-2-thread-1] com.example.CustomItemReader             : read method - collecting the MYAPP2 out file names
2016-02-12 08:02:47.634  INFO 12172 --- [pool-2-thread-1] com.example.CustomItemReader             : read method - no file found
2016-02-12 08:02:47.650  INFO 12172 --- [pool-2-thread-1] o.s.b.c.l.support.SimpleJobLauncher      : Job: [SimpleJob: [name=job1]] completed with the following parameters: [{}] and the following status: [COMPLETED]

BatchConfiguration类

@Configuration
@ComponentScan("com.example")
@EnableBatchProcessing
@EnableAutoConfiguration
@EnableScheduling
@PropertySource("config.properties")
public class BatchConfiguration {
       @Autowired
       private JobBuilderFactory jobBuilderFactory;
       @Autowired
       private StepBuilderFactory stepBuilderFactory;
       @Bean
       public Step step1(ItemReader<String> reader,
                     ItemProcessor<String, String> processor, ItemWriter<String> writer) {
              return stepBuilderFactory.get("step1").<String, String> chunk(1)                          .reader(reader).processor(processor).writer(writer)
                           .allowStartIfComplete(true).build();
       }
//I took out the rest of BatchConfiguration class

Application.properties(仅一行)

logging.level.*=OFF

P.S.我不会显示config.properties,因为它仅包含几个属性名称,这些名称与业务逻辑中使用的路径设置有关.

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
       <modelVersion>4.0.0</modelVersion>
       <groupId>com.example</groupId>
       <artifactId>demo</artifactId>
       <version>0.0.1-SNAPSHOT</version>
       <packaging>jar</packaging>
       <name>demo</name>
       <description>Demo project for Spring Boot</description>
       <parent>
              <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-starter-parent</artifactId>
              <version>1.3.1.RELEASE</version>
              <relativePath /> <!-- lookup parent from repository -->
       </parent>
       <properties>
              <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
              <java.version>1.8</java.version>
              <spring.batch.version>3.0.6.RELEASE</spring.batch.version>
       </properties>
       <dependencies>
              <dependency>
                     <groupId>org.springframework.boot</groupId>
                     <artifactId>spring-boot-starter-batch</artifactId>
              </dependency>
              <dependency>
                     <groupId>org.springframework.boot</groupId>
                     <artifactId>spring-boot-starter-test</artifactId>
                     <scope>test</scope>
              </dependency>
       </dependencies>
       <build>
              <plugins>
                     <plugin>
                           <groupId>org.springframework.boot</groupId>
                           <artifactId>spring-boot-maven-plugin</artifactId>
                     </plugin>
                     <plugin>
                           <artifactId>maven-assembly-plugin</artifactId>
                           <configuration>
                                  <archive>
                                         <manifest>
                                                <mainClass>com.example.DemoAppNoBoot</mainClass>
                                         </manifest>
                                  </archive>
                                  <descriptorRefs>
                                         <descriptorRef>jar-with-dependencies</descriptorRef>
                                  </descriptorRefs>
                           </configuration>
                           <executions>
                                  <execution>
                                         <phase>install</phase>
                                         <goals>
                                                <goal>single</goal>
                                         </goals>
                                  </execution>
                           </executions>
                     </plugin>
                     <plugin>
                           <groupId>org.apache.maven.plugins</groupId>
                           <artifactId>maven-release-plugin</artifactId>
                           <version>2.5.1</version>
                           <configuration>
                                  <goals>install</goals>
                                  <preparationGoals>install</preparationGoals>
                           </configuration>
                     </plugin>
                     <plugin>
                           <groupId>org.apache.maven.plugins</groupId>
                           <artifactId>maven-jar-plugin</artifactId>
                           <configuration>
                                  <archive>
                                         <manifest>
                                                <addClasspath>true</addClasspath>
                                                <mainClass>com.example.DemoAppNoBoot</mainClass>
                                         </manifest>
                                  </archive>
                           </configuration>
                     </plugin>
              </plugins>
       </build>
</project>

Maven依赖关系(我怀疑的相关问题):

logback-classic-1.1.3.jar
logback-core-1.1.3.jar
slf4j-api-1.7.13.jar
log4j-over-slf4j-1.7.13.jar

解决方案

在application.properties中,您可以添加'logging.level.* = LEVEL',其中"LEVEL"是TRACE,DEBUG,INFO,WARN,ERROR,致命,关闭. *负责包/类.

例如

logging.level.root=WARN
logging.level.org.springframework.web=DEBUG
logging.level.org.hibernate=ERROR

这意味着root记录程序具有WARN级别. org.springframework.web处于DEBUG级别,但所有休眠文件仅记录为ERROR.

在这种情况下,必须将logging.level.root设置为INFO,WARN,ERROR,FATAL或OFF之一,以关闭所有日志记录.

请参见 https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-logging.html#boot-features-custom-log-levels

I read in spring boot docs (https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-logging.html)

you can also specify debug=true in your application.properties"

So I guess I can turn off the debug logs by adding debug=false in application.properties. I did it but unfortunately it didn't work. Then I read in same doc

The logging system is initialized early in the application lifecycle and as such logging properties will not be found in property files loaded via @PropertySource annotations"

and

"Since logging is initialized before the ApplicationContext is created, it isn’t possible to control logging from @PropertySources in Spring @Configuration files"

and

"When possible we recommend that you use the -spring variants for your logging configuration" so I added a file named log4j-spring.properties in src/main/resources.

In such file I added debug=false (only this line and nothing else) but I am still seeing all "current date" [main] DEBUG ... messages. So, my question is how can I turn off the debug messages in my Spring Boot Application as I will deploy to the application to production. Is there a recommend way to reach this via maven?

***** Added in Feb 12th

The two main methods: 1)By using AnnotationConfigApplicationContext:

public class DemoAppNoBoot {
       public static void main(String[] args) {
              AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
                           BatchConfiguration.class);
              JobLauncher jobLauncher = (JobLauncher) context.getBean("jobLauncher");
              Job job = (Job) context.getBean("job");
               try {
                     JobExecution execution = jobLauncher.run(job, new JobParameters());
              } catch (Exception e) {
                     e.printStackTrace();
              }
       }
}

Snippet output:

08:26:18.713 [main] DEBUG o.s.core.env.StandardEnvironment - Adding [systemProperties] PropertySource with lowest search precedence
08:26:18.744 [main] DEBUG o.s.core.env.StandardEnvironment - Adding [systemEnvironment] PropertySource with lowest search precedence
08:26:18.744 [main] DEBUG o.s.core.env.StandardEnvironment - Initialized StandardEnvironment with PropertySources [systemProperties,systemEnvironment]
08:26:18.947 [main] INFO  o.s.c.a.AnnotationConfigApplicationContext - Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@532760d8: startup date [Fri Feb 12 08:26:18 CST 2016]; root of context hierarchy
08:26:18.947 [main] DEBUG o.s.c.a.AnnotationConfigApplicationContext - Bean factory for org.springframework.context.annotation.AnnotationConfigApplicationContext@532760d8: org.springframework.beans.factory.support.DefaultListableBeanFactory@50b494a6: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.event.internalEventListenerProcessor,org.springframework.context.event.internalEventListenerFactory,batchConfiguration]; root of factory hierarchy
08:26:18.979 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating shared instance of singleton bean 
...
08:26:32.560 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'lifecycleProcessor'
08:26:32.560 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.context.annotation.internalScheduledAnnotationProcessor'
08:26:32.560 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor'
08:26:32.560 [main] DEBUG o.s.s.a.ScheduledAnnotationBeanPostProcessor - Could not find default TaskScheduler bean
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.scheduling.TaskScheduler] is defined     at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:372) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]      at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:332) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
...
08:26:33.529 [pool-1-thread-1] DEBUG o.s.jdbc.core.JdbcTemplate - Executing prepared SQL update
08:26:33.529 [pool-1-thread-1] DEBUG o.s.jdbc.core.JdbcTemplate - Executing prepared SQL statement [UPDATE BATCH_JOB_EXECUTION set START_TIME = ?, END_TIME = ?,  STATUS = ?, EXIT_CODE = ?, EXIT_MESSAGE = ?, VERSION = ?, CREATE_TIME = ?, LAST_UPDATED = ? where JOB_EXECUTION_ID = ? and VERSION = ?]
08:26:33.529 [pool-1-thread-1] DEBUG o.s.jdbc.core.JdbcTemplate - SQL update affected 1 rows
08:26:33.545 [pool-1-thread-1] DEBUG o.s.j.d.DataSourceTransactionManager - Initiating transaction commit
08:26:33.545 [pool-1-thread-1] DEBUG o.s.j.d.DataSourceTransactionManager - Committing JDBC transaction on Connection [org.hsqldb.jdbc.JDBCConnection@33bbce9c]
08:26:33.545 [pool-1-thread-1] DEBUG o.s.j.d.DataSourceTransactionManager - Releasing JDBC Connection [org.hsqldb.jdbc.JDBCConnection@33bbce9c] after transaction
08:26:33.545 [pool-1-thread-1] DEBUG o.s.jdbc.datasource.DataSourceUtils - Returning JDBC Connection to DataSource
08:26:33.545 [pool-1-thread-1] INFO  o.s.b.c.l.support.SimpleJobLauncher - Job: [SimpleJob: [name=job1]] completed with the following parameters: [{}] and the following status: [COMPLETED]

2) Main method with SpringApplication.run

@SpringBootApplication
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(BatchConfiguration.class, args);
    }
}

Entire output:

: Spring Boot ::        (v1.3.1.RELEASE)
2016-02-12 08:02:36.145  INFO 12172 --- [           main] com.example.DemoApplication              : Starting DemoApplication on GH-VDIKCISV252 with PID 12172 (C:\STS\wsRestTemplate\demo\target\classes started by e049447 in C:\STS\wsRestTemplate\demo)
2016-02-12 08:02:36.145  INFO 12172 --- [           main] com.example.DemoApplication              : No active profile set, falling back to default profiles: default
2016-02-12 08:02:36.473  INFO 12172 --- [           main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@4e4aea35: startup date [Fri Feb 12 08:02:36 CST 2016]; root of context hierarchy
2016-02-12 08:02:42.176  WARN 12172 --- [           main] o.s.c.a.ConfigurationClassEnhancer       : @Bean method ScopeConfiguration.stepScope is non-static and returns an object assignable to Spring's BeanFactoryPostProcessor interface. This will result in a failure to process annotations such as @Autowired, @Resource and @PostConstruct within the method's declaring @Configuration class. Add the 'static' modifier to this method to avoid these container lifecycle issues; see @Bean javadoc for complete details.
2016-02-12 08:02:42.349  WARN 12172 --- [           main] o.s.c.a.ConfigurationClassEnhancer       : @Bean method ScopeConfiguration.jobScope is non-static and returns an object assignable to Spring's BeanFactoryPostProcessor interface. This will result in a failure to process annotations such as @Autowired, @Resource and @PostConstruct within the method's declaring @Configuration class. Add the 'static' modifier to this method to avoid these container lifecycle issues; see @Bean javadoc for complete details.
2016-02-12 08:02:42.724  INFO 12172 --- [           main] o.s.j.d.e.EmbeddedDatabaseFactory        : Starting embedded database: url='jdbc:hsqldb:mem:testdb', username='sa'
2016-02-12 08:02:43.802  WARN 12172 --- [           main] o.s.b.c.l.AbstractListenerFactoryBean    : org.springframework.batch.item.ItemReader is an interface.  The implementing class will not be queried for annotation based listener configurations.  If using @StepScope on a @Bean method, be sure to return the implementing class so listner annotations can be used.
2016-02-12 08:02:44.990  INFO 12172 --- [           main] o.s.jdbc.datasource.init.ScriptUtils     : Executing SQL script from class path resource [org/springframework/batch/core/schema-hsqldb.sql]
2016-02-12 08:02:45.179  INFO 12172 --- [           main] o.s.jdbc.datasource.init.ScriptUtils     : Executed SQL script from class path resource [org/springframework/batch/core/schema-hsqldb.sql] in 189 ms.
2016-02-12 08:02:46.804  INFO 12172 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2016-02-12 08:02:46.868  INFO 12172 --- [           main] o.s.b.a.b.JobLauncherCommandLineRunner   : Running default command line with: []
2016-02-12 08:02:46.962  INFO 12172 --- [           main] o.s.b.c.r.s.JobRepositoryFactoryBean     : No database type set, using meta data indicating: HSQL
2016-02-12 08:02:47.040  INFO 12172 --- [pool-2-thread-1] o.s.b.c.r.s.JobRepositoryFactoryBean     : No database type set, using meta data indicating: HSQL
2016-02-12 08:02:47.243  INFO 12172 --- [           main] o.s.b.c.l.support.SimpleJobLauncher      : No TaskExecutor has been set, defaulting to synchronous executor.
2016-02-12 08:02:47.259  INFO 12172 --- [pool-2-thread-1] o.s.b.c.l.support.SimpleJobLauncher      : No TaskExecutor has been set, defaulting to synchronous executor.
2016-02-12 08:02:47.321  INFO 12172 --- [           main] o.s.b.c.l.support.SimpleJobLauncher      : Job: [SimpleJob: [name=job1]] launched with the following parameters: [{run.id=1}]
2016-02-12 08:02:47.368  INFO 12172 --- [           main] o.s.batch.core.job.SimpleStepHandler     : Executing step: [step1]
2016-02-12 08:02:47.400  INFO 12172 --- [           main] com.example.CustomItemReader             : read method - collecting the MYAPP2 out file names
2016-02-12 08:02:47.525  INFO 12172 --- [           main] com.example.CustomItemReader             : read method - no file found
2016-02-12 08:02:47.556  INFO 12172 --- [           main] o.s.b.c.l.support.SimpleJobLauncher      : Job: [SimpleJob: [name=job1]] completed with the following parameters: [{run.id=1}] and the following status: [COMPLETED]
2016-02-12 08:02:47.556  INFO 12172 --- [           main] com.example.DemoApplication              : Started DemoApplication in 12.1 seconds (JVM running for 13.405)
2016-02-12 08:02:47.556  INFO 12172 --- [pool-2-thread-1] o.s.b.c.l.support.SimpleJobLauncher      : Job: [SimpleJob: [name=job1]] launched with the following parameters: [{}]
2016-02-12 08:02:47.618  INFO 12172 --- [pool-2-thread-1] o.s.batch.core.job.SimpleStepHandler     : Executing step: [step1]
2016-02-12 08:02:47.634  INFO 12172 --- [pool-2-thread-1] com.example.CustomItemReader             : read method - collecting the MYAPP2 out file names
2016-02-12 08:02:47.634  INFO 12172 --- [pool-2-thread-1] com.example.CustomItemReader             : read method - no file found
2016-02-12 08:02:47.650  INFO 12172 --- [pool-2-thread-1] o.s.b.c.l.support.SimpleJobLauncher      : Job: [SimpleJob: [name=job1]] completed with the following parameters: [{}] and the following status: [COMPLETED]

BatchConfiguration class

@Configuration
@ComponentScan("com.example")
@EnableBatchProcessing
@EnableAutoConfiguration
@EnableScheduling
@PropertySource("config.properties")
public class BatchConfiguration {
       @Autowired
       private JobBuilderFactory jobBuilderFactory;
       @Autowired
       private StepBuilderFactory stepBuilderFactory;
       @Bean
       public Step step1(ItemReader<String> reader,
                     ItemProcessor<String, String> processor, ItemWriter<String> writer) {
              return stepBuilderFactory.get("step1").<String, String> chunk(1)                          .reader(reader).processor(processor).writer(writer)
                           .allowStartIfComplete(true).build();
       }
//I took out the rest of BatchConfiguration class

Application.properties (only one line)

logging.level.*=OFF

P.S. I will not show config.properties because it only contains several property names with path settup used in business logic

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
       <modelVersion>4.0.0</modelVersion>
       <groupId>com.example</groupId>
       <artifactId>demo</artifactId>
       <version>0.0.1-SNAPSHOT</version>
       <packaging>jar</packaging>
       <name>demo</name>
       <description>Demo project for Spring Boot</description>
       <parent>
              <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-starter-parent</artifactId>
              <version>1.3.1.RELEASE</version>
              <relativePath /> <!-- lookup parent from repository -->
       </parent>
       <properties>
              <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
              <java.version>1.8</java.version>
              <spring.batch.version>3.0.6.RELEASE</spring.batch.version>
       </properties>
       <dependencies>
              <dependency>
                     <groupId>org.springframework.boot</groupId>
                     <artifactId>spring-boot-starter-batch</artifactId>
              </dependency>
              <dependency>
                     <groupId>org.springframework.boot</groupId>
                     <artifactId>spring-boot-starter-test</artifactId>
                     <scope>test</scope>
              </dependency>
       </dependencies>
       <build>
              <plugins>
                     <plugin>
                           <groupId>org.springframework.boot</groupId>
                           <artifactId>spring-boot-maven-plugin</artifactId>
                     </plugin>
                     <plugin>
                           <artifactId>maven-assembly-plugin</artifactId>
                           <configuration>
                                  <archive>
                                         <manifest>
                                                <mainClass>com.example.DemoAppNoBoot</mainClass>
                                         </manifest>
                                  </archive>
                                  <descriptorRefs>
                                         <descriptorRef>jar-with-dependencies</descriptorRef>
                                  </descriptorRefs>
                           </configuration>
                           <executions>
                                  <execution>
                                         <phase>install</phase>
                                         <goals>
                                                <goal>single</goal>
                                         </goals>
                                  </execution>
                           </executions>
                     </plugin>
                     <plugin>
                           <groupId>org.apache.maven.plugins</groupId>
                           <artifactId>maven-release-plugin</artifactId>
                           <version>2.5.1</version>
                           <configuration>
                                  <goals>install</goals>
                                  <preparationGoals>install</preparationGoals>
                           </configuration>
                     </plugin>
                     <plugin>
                           <groupId>org.apache.maven.plugins</groupId>
                           <artifactId>maven-jar-plugin</artifactId>
                           <configuration>
                                  <archive>
                                         <manifest>
                                                <addClasspath>true</addClasspath>
                                                <mainClass>com.example.DemoAppNoBoot</mainClass>
                                         </manifest>
                                  </archive>
                           </configuration>
                     </plugin>
              </plugins>
       </build>
</project>

Maven Dependencies (the relevant ones for my doubt):

logback-classic-1.1.3.jar
logback-core-1.1.3.jar
slf4j-api-1.7.13.jar
log4j-over-slf4j-1.7.13.jar

解决方案

In application.properties you can add ‘logging.level.*=LEVEL’ where ‘LEVEL’ is one of TRACE, DEBUG, INFO, WARN, ERROR, FATAL, OFF. * is responsible for package/class.

For example

logging.level.root=WARN
logging.level.org.springframework.web=DEBUG
logging.level.org.hibernate=ERROR

This means that root logger has WARN level. org.springframework.web is on DEBUG level, but all hibernates files are logged only ERROR.

In your case you must set logging.level.root on one of level from INFO, WARN, ERROR,FATAL or OFF to turn off all logging.

See https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-logging.html#boot-features-custom-log-levels

这篇关于如何在Spring Boot中关闭调试日志消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆