zoukankan      html  css  js  c++  java
  • 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'
    
    }
  • 相关阅读:
    php学习【1】
    网页项目源码笔记
    python学习笔记
    php集成开发环境xampp的搭建
    ubuntu18.04.1LTS系统远程工具secureCRT
    关于IT人的一些消遣区
    linux系统的启动过程简要分析
    【shell脚本学习-1】
    Linux命令总结--cat命令
    Linux命令总结--vi/vim命令
  • 原文地址:https://www.cnblogs.com/yshyee/p/7298727.html
Copyright © 2011-2022 走看看