Spring Boot QuickStart 10: Log Usage

Source: Internet
Author: User

First Step: Pom file

<?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.payease</groupId> <artifactId>sell</artifactId> <ve Rsion>0.0.1-snapshot</version> <packaging>jar</packaging> <name>sell</name> < Description>demo Project for Spring boot</description> <parent> <groupId>org.springframework.boot</groupId> &        Lt;artifactid>spring-boot-starter-parent</artifactid> <version>1.5.8.RELEASE</version> <relativePath/> <!--lookup parent from repository to </parent> <properties> <pro Ject.build.sourceencoding>utf-8</project.build.sourceencoding> <project.reporting.outputencoding >UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties&    Gt            <dependencies> <dependency> <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactid>spring-boot-starter-test</ar            Tifactid><scope>test</scope> </dependency> <!--===================== MySQL driver ================== ======-<dependency> <groupId>mysql</groupId> <artifactid>mysql -connector-java</artifactid> </dependency> <!--===================== JPA =================== =====-<dependency> <groupId>org.springframework.boot</groupId> <a Rtifactid>spring-boot-starter-data-jpa</artifactid> </dependency> <!--=================== = = @Getter @Setter @Slf4j @Data ========================-<dependency> &LT;GROUPID&GT;ORG.PR ojectlombok</groupid> <artifactId>lombok</artifactId> </dependency> </depe ndencies> <build> <plugins> <plugin> <groupid>org.springfra mework.boot</groupid&Gt <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </b Uild></project>

Step Two: Your test class

 Packagecom.payease;Importlombok.extern.slf4j.Slf4j;Importorg.junit.Test;ImportOrg.junit.runner.RunWith;Importorg.springframework.boot.test.context.SpringBootTest;ImportOrg.springframework.test.context.junit4.SpringRunner;/*** Created by liuxiaoming on 2017/11/8.*/@RunWith (Springrunner.class) @SpringBootTest @slf4j//@Data Public classLoggertest {//private final Logger Logger = Loggerfactory.getlogger (loggertest.class);@Test Public voidtest1 () {//logger.debug ("Debug ..."); //logger.info ("info ..."); //logger.error ("error ..."); //examples of the use of print variables in logsString name= "Liuxiaoming"; String Password= "123456"; Log.debug ("Debug ...");//@Slf4jLog.info ("info ..." + "Name:" + name + ", Password:" + password);//@Slf4jLog.error ("Error ..." + "name:{}, password:{}", Name,password);//@Slf4j    }}

Step three: Configuration in the configuration file Application.yml file

 spring:datasource:driver -class - Span style= "COLOR: #000000" >name:com.mysql.jdbc.driver username:root password:  1234 Url:jdbc:mysql:  // 127.0.0.1/sell? Characterencoding=utf-8&usessl=false   jpa:show -sql: true  server:context -path:/ selllogging:pattern:console:  "%d-%msg%n" #日志格式 date-/users/liuxiaoming/documents/ideawork/sell_log #日志路径 Default name spring.log file: /users/liuxiaoming/documents/ideawork/sell_log/sell.log #日志文件 +  path level: #日志级别 com.payease.LoggerTest:debug #日志级别指定某个类 can also step up the class directly at level: after configuring  pre> 

How to configure the second log:

Step one: Comment on the configuration of logs in Application.yml

Step Two: Create a new logback-spring.xml log configuration file

<?XML version= "1.0" encoding= "UTF-8"?><Configuration>    <!--display of the console -    <Appendername= "ConsoleLog"class= "Ch.qos.logback.core.ConsoleAppender">        <Layoutclass= "Ch.qos.logback.classic.PatternLayout">            <pattern>%d-%msg%n</pattern>        </Layout>    </Appender>    <!--log file: Fileinfolog -    <Appendername= "Fileinfolog"class= "Ch.qos.logback.core.rolling.RollingFileAppender">        <!--Filter by Range -        <Filterclass= "Ch.qos.logback.classic.filter.LevelFilter">            < Level>ERROR</ Level>            <Onmatch>DENY</Onmatch>  <!--prohibited -            <Onmismatch>ACCEPT</Onmismatch>  <!--Accept -        </Filter>        <!--scrolling -        <Encoder>            <pattern>%msg%n</pattern>        </Encoder>        <!--scrolling Policy -        <Rollingpolicyclass= "Ch.qos.logback.core.rolling.TimeBasedRollingPolicy">            <!--Path -            <Filenamepattern>/users/liuxiaoming/documents/ideawork/sell_log/info.%d.log</Filenamepattern>        </Rollingpolicy>    </Appender>    <!--log file: Fileerrorlog -    <Appendername= "Fileerrorlog"class= "Ch.qos.logback.core.rolling.RollingFileAppender">        <!--Filter by Range -        <Filterclass= "Ch.qos.logback.classic.filter.ThresholdFilter">            < Level>ERROR</ Level>        </Filter>        <Encoder>            <pattern>%msg%n</pattern>        </Encoder>        <!--scrolling Policy -        <Rollingpolicyclass= "Ch.qos.logback.core.rolling.TimeBasedRollingPolicy">            <!--Path -            <Filenamepattern>/users/liuxiaoming/documents/ideawork/sell_log/error.%d.log</Filenamepattern>        </Rollingpolicy>    </Appender>    <!--Log Level -    <Root Level= "Info">        <Appender-refref= "ConsoleLog" />        <Appender-refref= "Fileinfolog" />        <Appender-refref= "Fileerrorlog" />    </Root></Configuration>

Step three: Start a project view generates two configuration files

Spring Boot QuickStart 10: Log Usage

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.