Spring Boot Introduction (iii)-Log management

Source: Internet
Author: User

Spring Boot supports jul,log4j2 and logback.
1. Default Settings
-With SLF4J + Logback
-Log output to console only
-Level is info
-Log file rotate size is 10Mb
-Output format: Timestamp level process ID---[Thread name] class name log content

Reference: 2018-01-22 18:54:16.076 INFO 8296---[main] s.b.c.e.t.tomcatembeddedservletcontainer:tomcat started on Port (s): 8080 (http)

2. Input to File

Reference: Logging.path=d:

The Spring.log file is created in this directory and the output is the same as the console.

3. Detailed configuration

Src/main/resources/application.properties

1 # Location of the log file.2logging.file=d:\\spring-boot-Demo.log3 # Appender Pattern4Logging.pattern.console=%date%-5level%logger{0}-%msg%N5Logging.pattern.file=%date%-5level [%thread]%logger{0} [%file:%line]-%msg%N6logging.pattern.level=%5p7# Log Levels-Root8logging.level.=DEBUG9# Log Levels- PackageTenlogging.level.org.springframework.web=INFO One# Log Levels-class ALogging.level.com.rensanning.springboot.homecontroller=trace

Note: Logging.file and Logging.path cannot be set simultaneously

4. Test Code
Src/main/java/com/rensanning/springboot/hellocontroller.java

1 ImportOrg.slf4j.Logger;2 Importorg.slf4j.LoggerFactory;3 Importorg.springframework.web.bind.annotation.RequestMapping;4 ImportOrg.springframework.web.bind.annotation.RestController;5 6 @RestController7  Public classHellocontroller {8     9     Private FinalLogger log = Loggerfactory.getlogger ( This. GetClass ());Ten      One@RequestMapping ("/") A      PublicString Index () { -          -         //Log a simple message theLog.debug ("Debug Level Log"); -Log.info ("Info level log"); -Log.warn ("Warn level log"); -Log.error ("Error level Log"); +          -         return"Hello World from Spring boot!"; +     } A      at}

Access http://localhost:8080/After you start the app.

5. External configuration
src/main/resources/

    • Logback, Logback.xml or Logback-spring.xml
    • Log4j2, Log4j2.xml or Log4j2-spring.xml
    • Logging.properties, JUL

Src/main/resources/logback-spring.xml

1<?xml version= "1.0" encoding= "UTF-8"?>2<configuration scan= "true" scanperiod= "seconds" >3 4<property name= "Log.base" value= "D:"/>5<property name= "Log.level" value= "DEBUG"/>6 7<!--appenders--8<appender name= "STDOUT"class= "Ch.qos.logback.core.ConsoleAppender" >9<encoder>Ten<pattern>%-5level%logger{0}-%msg%n</pattern> One</encoder> A</appender> -  -<appender name= "FILE"class= "Ch.qos.logback.core.rolling.RollingFileAppender" > the<file>${log.base}/SpringSample.log</file> -<rollingpolicyclass= "Ch.qos.logback.core.rolling.TimeBasedRollingPolicy" > -<fileNamePattern>${log.base}/SpringSample.%d{yyyyMMdd}.%i.log</fileNamePattern> -<timebasedfilenamingandtriggeringpolicyclass= "Ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP" > +<maxFileSize>5MB</maxFileSize> -</timeBasedFileNamingAndTriggeringPolicy> +</rollingPolicy> A<encoder> at<pattern>%date%-5level [%thread]%logger{0} [%file:%line]-%msg%n</pattern> -</encoder> -</appender> -      -<!--3rdparty Logger-- -<logger name= "Org.springframework" level= "${log.level}"/> in<logger name= "org.apache.commons" level= "INFO"/> -<logger name= "Org.apache.tiles" level= "INFO"/> to<logger name= "Org.apache.ibatis" level= "${log.level}"/> +<logger name= "Org.apache.shiro" level= "${log.level}"/> -<logger name= "Org.dozer" level= "INFO"/> the<logger name= "Org.scribe" level= "${log.level}"/> *      $<!--application Logger--Panax Notoginseng<logger name= "Com.rensanning.springboot" level= "${log.level}"/> -      the<!--Root Logger-- +<root level= "${log.level}" > A<appender-ref ref= "STDOUT"/> the<appender-ref ref= "FILE"/> +</root> -  $</configuration>

Access http://localhost:8080/After you start the app.

Console output: DEBUG hellocontroller-debug level Log
INFO Hellocontroller-info Level Log
WARN Hellocontroller-warn Level Log
ERROR Hellocontroller-error Level Log

Springboot Default configuration file:
Base.xml (Console-appender.xml file-appender.xml)
https://github.com/spring-projects/spring-boot/blob/master/spring-boot/src/main/resources/org/springframework/ Boot/logging/logback/base.xml

  1  <included> 2  <include Resource= "Org/springframework/boot/logging/logback/defaults.xml"/> 3  < Property Name= "Log_file" value= "${log_file:-${log_path:-${log_temp:-${java.io.tmpdir:-/tmp}}}/spring.log}"/>< Span style= "COLOR: #008080" > 4  <include resource= "org/springframework/boot/logging/logback/ Console-appender.xml "/> 5  <include resource=" org/springframework/boot/ Logging/logback/file-appender.xml "/> 6  <root level=" INFO "> 7  <appender-ref ref= "CONSOLE"/> 8  < ; Appender-ref ref= "FILE"/> 9  </root>10< /span> </included> 

6. Partial Setup (introduction of Base.xml)
Src/main/resources/application.properties

Reference: Logging.path=d:

Src/main/resources/logback-spring.xml

1 <?xml version= "1.0" encoding= "UTF-8"?>2 <configuration>3     <include Resource= "Org/springframework/boot/logging/logback/base.xml"/>4     <logger name= " Com.rensanning.springboot "level=" WARN "additivity=" false ">5         <appender-ref ref=" CONSOLE "/> 6         <appender-ref ref= "FILE"/>7     </logger>8 </configuration>

Access http://localhost:8080/After you start the app.

Use <springProfile>
Src/main/resources/application.properties

Reference: Logging.path=d:src/main/resources/logback-spring.xml
1<?xml version= "1.0" encoding= "UTF-8"?>2<configuration>3<include resource= "Org/springframework/boot/logging/logback/base.xml"/>4<springprofile name= "Dev,staging" >5<logger name= "Com.rensanning.springboot" level= "DEBUG" additivity= "false" >6<appender-ref ref= "CONSOLE"/>7</logger>8</springProfile>9<springprofile name= "Production" >Ten<logger name= "Com.rensanning.springboot" level= "WARN" additivity= "false" > One<appender-ref ref= "FILE"/> A</logger> -</springProfile> -</configuration>

Add VM Parameters-dspring.profiles.active=dev Access http://localhost:8080/after starting the app.

Use <if><then><else>

Logback through the Janino package also supports conditional judgments in configuration files. http://janino-compiler.github.io/janino/

 <dependency> <groupId>org.codehaus.janino</groupId> <artifactid>janino</ Artifactid> <version>2.7.8</version></dependency> 
<?xml version= "1.0" encoding= "UTF-8"?><configuration> <include    resource= "org/springframework/ Boot/logging/logback/base.xml "/>    <if condition= ' property (" Spring.profiles.active "). Contains ( "Dev") ' >        <then>            <logger name= "Com.rensanning.springboot" level= "DEBUG" additivity= "false" >                <appender-ref ref= "CONSOLE"/>            </logger>        </then>        <else>            <logger name= "Com.rensanning.springboot" level= "WARN" additivity= "false" >                <appender-ref ref= "FILE"/ >            </logger>        </Else>    </if></configuration>

7. Using Log4j2

Pom.xml

1<dependency>2<groupId>org.springframework.boot</groupId>3<artifactId>spring-boot-starter-web</artifactId>4<exclusions>5<exclusion>6<groupId>org.springframework.boot</groupId>7<artifactId>spring-boot-starter-logging</artifactId>8</exclusion>9</exclusions>Ten</dependency> One<dependency> A<groupId>org.springframework.boot</groupId> -<artifactId>spring-boot-starter-log4j2</artifactId> -</dependency>

Src/main/resources/log4j2-spring.xml

<?xml version= "1.0" encoding= "UTF-8"? ><configuration monitorinterval= "The" > <Properties> &LT;PR Operty name= "Log-path" >d:</Property> </Properties> <Appenders> <console name= "Console -appender "target=" System_out "> <PatternLayout> <pattern>                    [%-5level]%d{yyyy-mm-dd HH:mm:ss. SSS} [%t]%c{1}-%msg%N</pattern>> </PatternLayout> </Console> <file name= "File-appender" Filena Me= "${log-path}/springboot_log.log" > <PatternLayout> <pattern>                    [%-5level]%d{yyyy-mm-dd HH:mm:ss. SSS} [%t]%c{1}-%msg%N</pattern> </PatternLayout> </File> </Appenders> <Loggers> &L T Logger name= "Com.rensanning.springboot" level= "info" additivity= "false" > <appenderref ref= "console-append Er "/> </Logger> <Root> <appenderref ref=" File-appender "/> <app Enderref ref= "Console-appender"/> </Root> </Loggers></Configuration>

Access http://localhost:8080/After you start the app.

--end--

Spring Boot Introduction (iii)-Log management

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.