Spring Boot series (11) Spring Boot log control

Source: Internet
Author: User
Tags bind character set log log
Spring Boot series (11) Spring Boot log Control

This article describes spring Boot log control, which requires a complete video tutorial of Springboot, click here.


Spring boot to the log processing, and our usual log processing exactly the same, through the Logback.xml log management function. For simplicity, the project in the section "Spring Boot Build Framework" is used to describe how to process logs in spring boot.

As a first step, although the Application.properties configuration file in spring boot provides the configuration of the log, the individual prefers the old configuration. Add the Logback.xml log file in the Src/main/resources directory, the file content is as follows (the configuration is relatively simple, the individual according to the project situation, the corresponding configuration):


<configuration scan= "true" scanperiod= "ten Seconds" >
<include resource= "Org/springframework/boot/logging/logback/base.xml"/>

<appender name= "Info_file" class= "Ch.qos.logback.core.rolling.RollingFileAppender" >
<File>${LOG_PATH}/info.log</File>
<rollingpolicy class= "Ch.qos.logback.core.rolling.TimeBasedRollingPolicy" >
<filenamepattern>${log_path}/info-%d{yyyymmdd}.log.%i
</fileNamePattern>
<timebasedfilenamingandtriggeringpolicy class= "Ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP" >
<maxFileSize>500MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<maxHistory>2</maxHistory>
</rollingPolicy>
<layout class= "Ch.qos.logback.classic.PatternLayout" >
<pattern>%d{yyyy-mm-dd HH:mm:ss. SSS} [%thread]%-5level%logger{36}-%msg%n
</Pattern>
</layout>
</appender>

<appender name= "Error_file" class= "Ch.qos.logback.core.rolling.RollingFileAppender" >
<filter class= "Ch.qos.logback.classic.filter.ThresholdFilter" >
<level>ERROR</level>
</filter>
<File>${LOG_PATH}/error.log</File>
<rollingpolicy class= "Ch.qos.logback.core.rolling.TimeBasedRollingPolicy" >
<filenamepattern>${log_path}/error-%d{yyyymmdd}.log.%i
</fileNamePattern>
<timebasedfilenamingandtriggeringpolicy class= "Ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP" >
<maxFileSize>500MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<maxHistory>2</maxHistory>
</rollingPolicy>
<layout class= "Ch.qos.logback.classic.PatternLayout" >
<pattern>%d{yyyy-mm-dd HH:mm:ss. SSS} [%thread]%-5level%logger{36}-%msg%n

</Pattern>
</layout>
</appender>

<logger name= "Com.example" level= "DEBUG" >
<appender-ref ref= "Baselog"/>
</logger>

<root level= "INFO" >
<appender-ref ref= "Info_file"/>
<appender-ref ref= "Error_file"/>
</root>

</configuration>


Attention:
1) The character set of the console and log files
2) The location of the log files, you need to follow the Linux naming rules

In the second step, specify the path of Logback.xml and log generation in Application.properties, as follows:


Logging.config=classpath:logback.xml
Logging.path=/workspace/log


The third step, the new Hellocontroller class, the specific content is as follows:


Package com.example;

Import Org.slf4j.Logger;
Import Org.slf4j.LoggerFactory;
Import org.springframework.web.bind.annotation.PathVariable;
Import org.springframework.web.bind.annotation.RequestMapping;
Import Org.springframework.web.bind.annotation.RestController;

@RestController
public class Hellocontroller {

protected static Logger Logger=loggerfactory.getlogger (Hellocontroller.class);

@RequestMapping ("/")
Public String HelloWorld () {
Logger.debug ("Access Hello");
Return "Hello world!";
}

@RequestMapping ("/hello/{name}")
public string Helloname (@PathVariable string name) {
Logger.debug ("Access helloname,name={}", Name);
Return "Hello" +name;
}
}


Note: When adding a reference, the package for the log must be a Org.slf4j.Logger, Org.slf4j.LoggerFactory class.

Fourth step, test
1) running the main program
2) in the browser, enter


http://localhost:8080/
http://localhost:8080/hello/Motobumi Zhai Blog


3) Since my project finds the D:/workspace/log folder in the directory where all log log files are located in the D disk, the name of the log file is set in the configuration file Logback.xml.

Log-related properties can be configured in Application.properties


Console output
Log levels from low to high are divided into trace < DEBUG < info < WARN < ERROR < FATAL, and if set to WARN, information below WARN is not output.
The log output from the error, warn, and info levels is configured by default in Spring boot to the console. You can also enable debug mode by launching your application--debug flag (recommended for development).


The following two ways are available:
1) Add the--DEBUG flag after running the command, such as: $ Java-jar Springtest.jar--debug
2) Configure Debug=true in Application.properties, when this property is set to True, the core logger (including embedded container, Hibernate, Spring) will output more content. But the logs you apply are not output to the debug level.

File output
By default, Spring boot outputs the log to the console and does not write to the log file. If you want to write a log file other than console output, you need to set the Logging.file or Logging.path property in Application.properties.
1) logging.file, set file, can be an absolute path, or it can be a relative path. such as: Logging.file=my.log
2) Logging.path, set the directory, the directory will be created Spring.log file, and write to the log content, such as: Logging.path=/var/log
If you configure only logging.file, a xxx.log log file is generated under the current path of the project. If you only configure Logging.path, generate a log file in the/var/log folder for Spring.log

Note: Both cannot be used at the same time, and only logging.file takes effect if used simultaneously. By default, the size of the log file is sliced once when it reaches 10MB, creating a new log file with the default level: ERROR, WARN, INFO.

Level control
All supported logging systems can set the logging level in the spring environment (set in application.properties)
The format is: ' logging.level.* = Level '
Logging.level: Log level control prefix, * for package name or logger name
Level: Option Trace, DEBUG, INFO, WARN, ERROR, FATAL, OFF

Instance:


All classes are output at DEBUG level under the Logging.level.com.dudu=debug:com.example package
Logging.level.root=warn:root log output at WARN level


Custom log Configuration
&nb

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.