码迷,mamicode.com
首页 > 编程语言 > 详细

Spring logger 配置

时间:2019-06-18 12:21:54      阅读:108      评论:0      收藏:0      [点我收藏+]

标签:ica   false   保存时间   cto   XML   htm   work   image   ima   

1. logback-spring.xml 

<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="60 seconds" debug="false">
    <contextName>logback</contextName>
    <!--输出到控制台-->
    <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger -%msg%n</pattern>
        </encoder>
    </appender>

    <appender name="INFO_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <File>C:\gather-logs\crm-info.log</File>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>C:\gather-logs\server-info-%d{yyyyMMdd}.log.%i</fileNamePattern>
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>5MB</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 -%msg%n</Pattern>
        </layout>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符-->
            <pattern>%date{yyyy-MM-dd HH:mm:ss} | %highlight(%-5level) | %boldYellow(%thread) | %boldGreen(%logger) |
                %msg%n
            </pattern>
        </encoder>

    </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>C:\gather-logs\crm-error.log</File>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>C:\gather-logs\server-error-%d{yyyyMMdd}.log.%i
            </fileNamePattern>
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>5MB</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 -%msg%n
            </Pattern>
        </layout>
    </appender>

    <!--    <logger name="com.example.demo1.controller" level="INFO">-->

    <!--    </logger>-->

    <root level="INFO">
        <appender-ref ref="console"/>
        <appender-ref ref="INFO_FILE"/>
        <appender-ref ref="ERROR_FILE"/>
    </root>


    <!-- TRACE, DEBUG, INFO, WARN, ERROR, ALL 和 OFF -->
    <logger name="org.springframework" level="DEBUG" additivity="true"/>


    <logger name="org.apache" level="OFF"/>
    <logger name="com.alibaba" level="OFF"/>
    <!-- 如果这里设置了additivity=true 代码打印信息向上传递,将打印信息传递到root ,并且自己的appender-ref 也会打印,这样就是打印了俩次日志-->
    <logger name="com.example.demo1" level="OFF" additivity="false">
        <appender-ref ref="console"/>
    </logger>
</configuration>

 (1) 

    <contextName>logback</contextName>
    <!-- name的值是变量的名称,value的值时变量定义的值。通过定义的值会被插入到logger上下文中。定义后,可以使“${}”来使用变量。 -->
    <property name="log.path" value="G:/logs/pmp" />

 (2)

<configuration scan="true" scanPeriod="60 seconds" debug="false">

scan: 当此属性设置为true时,配置文档如果发生改变,将会被重新加载,默认值为true 

scanPeriod: 设置监测配置文档是否有修改的时间间隔,如果没有给出时间单位,默认单位是毫秒。当scan为true时,此属性生效。默认的时间间隔为1分钟。

debug: 当此属性设置为true时,将打印出logback内部日志信息,实时查看logback运行状态。默认值为false。 

    

2. 设置Logger 

 <logger name="com.example.demo1" level="INFO" additivity="true">
        <appender-ref ref="console"/>
 </logger>

  (1)logger 设置 level 时,则根据level 过滤日志信息,如果没有设置level ,则继承root的level。

  (2)logger 的additivity属性,如果设置为true,则会将日志信息向上传递给root,同时也执行自己的<appender-ref> 这里会出现打印2次的现象。

      如下:

   

package com.example.demo1.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class MyController {

    private static Logger logger= LoggerFactory.getLogger(MyController.class);

    @RequestMapping("/getOut")
    @ResponseBody
    public String getBody(){

        logger.info("-----------info");
        logger.debug("----------debug");
        logger.warn("-----------warn");
        logger.error("----------error");
        return "body";
    }
}

技术图片  

 3. appender  

 

    <appender name="INFO_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <File>C:\gather-logs\crm-info.log</File>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>C:\gather-logs\server-info-%d{yyyyMMdd}.log.%i</fileNamePattern>
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>5MB</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 -%msg%n</Pattern>
        </layout>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符-->
            <pattern>%date{yyyy-MM-dd HH:mm:ss} | %highlight(%-5level) | %boldYellow(%thread) | %boldGreen(%logger) |
                %msg%n
            </pattern>
        </encoder>

    </appender>

  (1)  日志打印保存的路径  

   <File>C:\gather-logs\crm-info.log</File>

  (2) 日志文件的命名方式 如果: server-info-20190617.log.1   

<fileNamePattern>C:\gather-logs\server-info-%d{yyyyMMdd}.log.%i</fileNamePattern>

  (3) 日志的最大设置为5M ,如果大于5M ,则归档,创建文件如:  server-info-20190617.log.2

<maxFileSize>5MB</maxFileSize>

 (4)日志的保存时间 2天 

  <maxHistory>2</maxHistory>

 (5)Logger 的输入格式 eg: 

  

<layout class="ch.qos.logback.classic.PatternLayout">
     <Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger -%msg%n</Pattern>
</layout>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
    <!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符-->
     <pattern>%date{yyyy-MM-dd HH:mm:ss} | %highlight(%-5level) | %boldYellow(%thread) | %boldGreen(%logger) | %msg%n
     </pattern>
 </encoder>    

  

 

Spring logger 配置

标签:ica   false   保存时间   cto   XML   htm   work   image   ima   

原文地址:https://www.cnblogs.com/galibujianbusana/p/11044322.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!