Log ic settings for Spring Boot

Keywords: Spring Hibernate Apache Tomcat

1. Maven dependence

No additional dependencies need to be introduced, just the starter required by Spring Boot:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

2. Log format

Without modifying the default settings, Spring Boot prints the following format of Log on the console:

2014-03-05 10:57:51.112  INFO 45469 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/7.0.52
2014-03-05 10:57:51.253  INFO 45469 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2014-03-05 10:57:51.253  INFO 45469 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1358 ms
2014-03-05 10:57:51.698  INFO 45469 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean        : Mapping servlet: 'dispatcherServlet' to [/]
2014-03-05 10:57:51.702  INFO 45469 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]

The default output items are in order:

  • Date and time, accurate to milliseconds
  • Log Level, all possible levels are ERROR, WARN, INFO, DEBUG or TRACE
  • Process ID
  • Separator
  • [Thread name]
  • log information

3. Console Output

By default, only ERROR, WARN, and INFO-level messages are printed to the console. It can be modified in two ways:

  • When running the program, add -- debug or -- trace after the command:
java -jar myapp.jar --debug
  • Or add in application.properties:
debug=true
# Or trace=true

The above two methods control the LOG of Spring Boot, Hibernate and other parts at the same time. If you need to set these modules separately, you can use "logging.level. *" to set them in the application.properties file, for example:

# Set default log level
logging.level.root=WARN
# Setting the log level of the web
logging.level.org.springframework.web=ERROR
# Setting hibernate's log level
logging.level.org.hibernate=ERROR

4. File Output

By default, file output will not be done, but the information that the console can record is limited, and we need file output in many cases.

File output can also be achieved in a variety of ways, the most direct way is to directly use the export function of the corresponding system. For example, in a windows server, the command to run is changed to:

java -jar myApp.jar >> log.txt

All console output can be output to log.txt file.

In addition, it can also be controlled by configuration files. In the application.properties file, add:

Log. file = filename. txt

You can save the log to a local file. Note that with this method, when the total size of the file reaches 10MB, the log will be output from scratch, overwriting the previous log.

5. Use Logback

Most Spring designs are non-intrusive, which means that we can configure and use LogBack as well as other applications without additional settings.

Posted by bad_goose on Sat, 15 Jun 2019 17:04:11 -0700