Spring-boot logback日志处理

1:在resources目录下面创建logback.xml配置文件

<?xml version="1.0"?>
<configuration>
    <!-- 控制台输出 -->
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>

    <!-- 文件输出-->
    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>G:/tmp/logs/ydbg.%d{yyyyMMdd}.%i.log</fileNamePattern>
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">>
                <maxFileSize>10MB</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
        </rollingPolicy>
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>


    <!-- 日志级别 -->
    <root level="ERROR">
        <appender-ref ref="STDOUT" />
        <appender-ref ref="FILE" />
    </root>
</configuration>

 

2:TestCtrl.java测试

package com.example.springbootlogback.test;

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.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by yan on 2017/8/7.
 */
@RestController
@RequestMapping(
        value = "/test",
        produces = "application/json;charset=utf-8",
        headers = "Accept=application/json")
public class TestCtrl {
    private static final Logger log = LoggerFactory.getLogger(TestCtrl.class);

    @RequestMapping(
            value = "/{key}/{value}",
            method = RequestMethod.GET
    )
    public Map<String,String> test(@PathVariable String key, @PathVariable String value){
        Map<String,String> map  = new HashMap<String, String>();
        map.put(key, value);

        log.error("key:"+key+",value:"+value);

        return map;
    }
}

 

3:build.gradle

dependencies {
    compile('org.springframework.boot:spring-boot-starter-web')
    testCompile('org.springframework.boot:spring-boot-starter-test')

    // https://mvnrepository.com/artifact/ch.qos.logback/logback-core
    compile group: 'ch.qos.logback', name: 'logback-core', version: '1.2.3'

}

 

posted @ 2017-08-07 13:42  yshy  阅读(967)  评论(0编辑  收藏  举报