Based on the configuration springboot of Logback

SpringBoot defaults to logback as a logging framework, when generating springboot project can be directly checked logback, then you can use logback directly, this will save a step import dependence can be used Logback directly.

Examples of projects get logs

There are two ways to get the code in the example log

  • Annotations manner
    by relying on packet lombok using @ Slf4j annotation used directly log instance.
  • LoggerFactory way
    LoggerFactory.getLogger (Class clazz <?>) ; Gets the instance

Then you can call a method where we need

Configuring logback-spring.xml

This time has been called. We will need to configure the log xml file
xml file location placed in the resources file and springboot the application file at the same level.
Because springboot recommended format is logback-spring.xml, you can solve a number of multi-environment variable names in the application file. However, the log file can not read the question, so we put the file name is set to logback-spring.xml

The following is logback-spring.xml file my own use

<?xml version="1.0" encoding="UTF-8" ?>

<!-- 级别从高到低 OFF 、 FATAL 、 ERROR 、 WARN 、 INFO 、 DEBUG 、 TRACE 、 ALL -->
<!-- 日志输出规则 根据当前ROOT 级别,日志输出时,级别高于root默认的级别时 会输出 -->
<!-- 以下 每个配置的 filter 是过滤掉输出文件里面,会出现高级别文件,依然出现低级别的日志信息,通过filter 过滤只记录本级别的日志 -->
<!-- scan 当此属性设置为true时,配置文件如果发生改变,将会被重新加载,默认值为true。 -->
<!-- scanPeriod 设置监测配置文件是否有修改的时间间隔,如果没有给出时间单位,默认单位是毫秒。当scan为true时,此属性生效。默认的时间间隔为1分钟。 -->
<!-- debug 当此属性设置为true时,将打印出logback内部日志信息,实时查看logback运行状态。默认值为false。 -->
<configuration scan="true" scanPeriod="60 seconds" debug="false">

    <!-- 动态日志级别 -->
    <jmxConfigurator/>

    <!-- 定义日志文件 输出位置 -->
    <property name="log.log_dir" value="logs"/>
    <property name="log.log_name" value="log"/>
    <!-- 日志最大的历史 30天 -->
    <property name="log.maxHistory" value="30"/>
    <property name="log.level" value="debug"/>
    <property name="log.maxSize" value="5MB" />

    <!-- ConsoleAppender 控制台输出日志 -->
    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>
                <!-- 设置日志输出格式 -->
                <!-- 日期 [线程] [class类]-[日志级别] log内容 -->
                %blue(%d{yyyy-MM-dd HH:mm:ss,SSS}) [%cyan(%t)] [%yellow(%c)]-[%highlight(%p)] %m%n
            </pattern>
        </encoder>
    </appender>

    <!-- ERROR级别日志 -->
    <!-- 滚动记录文件,先将日志记录到指定文件,当符合某个条件时,将日志记录到其他文件 RollingFileAppender -->
    <appender name="ERROR" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <!-- 过滤器,只记录WARN级别的日志 -->
        <!-- 果日志级别等于配置级别,过滤器会根据onMath 和 onMismatch接收或拒绝日志。 -->
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <!-- 设置过滤级别 -->
            <level>ERROR</level>
            <!-- 用于配置符合过滤条件的操作 -->
            <onMatch>ACCEPT</onMatch>
            <!-- 用于配置不符合过滤条件的操作 -->
            <onMismatch>DENY</onMismatch>
        </filter>
        <!-- 最常用的滚动策略,它根据时间来制定滚动策略.既负责滚动也负责触发滚动 -->
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <!--日志输出位置 可相对、和绝对路径 -->
            <fileNamePattern>
                ${log.log_dir}/error/%d{yyyy-MM-dd}/error_${log.log_name}-%i.log
            </fileNamePattern>
            <!-- 可选节点,控制保留的归档文件的最大数量,超出数量就删除旧文件,假设设置每个月滚动,且<maxHistory>是6,
            则只保存最近6个月的文件,删除之前的旧文件。注意,删除旧文件是,那些为了归档而创建的目录也会被删除 -->
            <maxHistory>${log.maxHistory}</maxHistory>
            <!--日志文件最大的大小-->
            <MaxFileSize>${log.maxSize}</MaxFileSize>
        </rollingPolicy>
        <encoder>
            <pattern>
                <!-- 设置日志输出格式 -->
                %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n
            </pattern>
        </encoder>
    </appender>

    <!-- INFO级别日志 appender -->
    <appender name="INFO" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>INFO</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <fileNamePattern>${log.log_dir}/info/%d{yyyy-MM-dd}/info_${log.log_name}-%i.log</fileNamePattern>
            <maxHistory>${log.maxHistory}</maxHistory>
            <MaxFileSize>${log.maxSize}</MaxFileSize>
        </rollingPolicy>
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%-5level] %logger - %msg%n</pattern>
        </encoder>
    </appender>


    <!-- DEBUG级别日志 appender -->
    <appender name="DEBUG" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>DEBUG</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <fileNamePattern>${log.log_dir}/debug/%d{yyyy-MM-dd}/debug_${log.log_name}-%i.log</fileNamePattern>
            <maxHistory>${log.maxHistory}</maxHistory>
            <MaxFileSize>${log.maxSize}</MaxFileSize>
        </rollingPolicy>
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%-5level] %logger - %msg%n</pattern>
        </encoder>
    </appender>

    <!--设置一个向上传递的appender,所有级别的日志都会输出-->
    <appender name="app" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <fileNamePattern>${log.log_dir}/app/%d{yyyy-MM-dd}/app_${log.log_name}-%i.log</fileNamePattern>
            <maxHistory>${log.maxHistory}</maxHistory>
            <MaxFileSize>${log.maxSize}</MaxFileSize>
        </rollingPolicy>
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%-5level] %logger - %msg%n</pattern>
        </encoder>
    </appender>

    <!--name包下的类的日志输出-->
    <logger name="com.example.webtest.controller" additivity="true" level="DEBUG" >
        <appender-ref ref="app" />
        <appender-ref ref="ERROR" />
        <appender-ref ref="INFO" />
        <!--打印控制台-->
        <appender-ref ref="CONSOLE" />
    </logger>

    <!-- root级别   DEBUG -->
    <root>
        <!-- 控制台输出 -->
        <appender-ref ref="CONSOLE"/>
        <!-- 不管什么包下的日志都输出文件 -->
        <!--<appender-ref ref="ERROR"/>-->
    </root>
</configuration>
复制代码

Simply put, what role Configuration tab

  • property global properties may be used $ {key} elsewhere reference
  • Appender configuration log output, and storage time encoded file path, etc.

    name: that is, their own identity
    class: the two main ConsoleAppender log printed to the console, RollingFileAppender logging print to a file
    filter: save to a file does not meet the level requirements are not saved to the file level according to the log level, such error. log only stores log information of error
    rollingPolicy: print to the policy logs are generally SizeAndTimeBasedRollingPolicy, when the arrival time or the size of the log file will create a new log file
    fileNamePattern: log file path

  • logger to set the logging level to print one specific packet or a certain class of

    name: the path setup packet is used to print the class
    additivity: whether the log information passed up to the root default is false true if the change will not be transmitted log information to the root.
    level: the log level TRACE, DEBUG, INFO, WARN, ERROR, ALL and OFF
    to note here is: level if set too high additivity into a false words, if the root is at the console, then print the log, then if the level is error go through the logger will save print the error message when an error message to the log, but additivity is false does not report the log information. So root on the console will not print your error logs may cause no error illusion. So try not to such configuration.

  • It is the root element, but it is the root logger. Only one level property, should have been named "root". But the global print log

Written in the last

This article just to record and share the results of their learning log configuration for other small partners when needed may be able to achieve their desired results according to their modification, if there is an error welcome that. Thank you!

Finally, if you want a small partner logback more detailed understanding of the configuration can look at Kapok open the most complete logback fast practice

Guess you like

Origin juejin.im/post/5e265a666fb9a03001755b73