zoukankan      html  css  js  c++  java
  • SpringBoot使用logback日志记录

      在resources里的配置文件: logback-spring.xml

    <?xml version="1.0" encoding="UTF-8" ?>
    
    <configuration>
    
        <appender name="consoleLog" class="ch.qos.logback.core.ConsoleAppender">
            <layout class="ch.qos.logback.classic.PatternLayout">
                <pattern>
                    %d - %msg%n
                </pattern>
            </layout>
        </appender>
    
        <appender name="fileInfoLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
            <filter class="ch.qos.logback.classic.filter.LevelFilter">
                <level>ERROR</level>
                <onMatch>DENY</onMatch>
                <onMismatch>ACCEPT</onMismatch>
            </filter>
            <encoder>
                <pattern>
                    %d - %msg%n
                </pattern>
            </encoder>
            <!--滚动策略-->
            <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                <!--路径-->
                <fileNamePattern>D:\logger\info.%d.log</fileNamePattern>
            </rollingPolicy>
        </appender>
    
    
        <appender name="fileErrorLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
            <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
                <level>ERROR</level>
            </filter>
            <encoder>
                <pattern>
                    %msg%n
                </pattern>
            </encoder>
            <!--滚动策略-->
            <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                <!--路径-->
                <fileNamePattern>D:\logger\error.%d.log</fileNamePattern>
            </rollingPolicy>
        </appender>
    
        <root level="info">
            <appender-ref ref="consoleLog" />
            <appender-ref ref="fileInfoLog" />
            <appender-ref ref="fileErrorLog" />
        </root>
    
    </configuration>

      SpringBoot启动类,测试日志打印

    package com.st;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class SpringbApplication {
        
        private static final Logger logger = LoggerFactory.getLogger(SpringbApplication.class);
    
        public static void main(String[] args) {
            SpringApplication.run(SpringbApplication.class, args);
            String msg = "习惯日志记录";
            logger.info("打印日志...{}",msg);
        }
    }

      启动后,控制台输出如下:

      保存的日志文件

  • 相关阅读:
    python 字典
    python set集合
    python封装和解构
    python 内置数据结构 切片
    CPU 上下文切换及案例分析
    怎么理解linux的平均负载及平均负载高后的排查工具
    Linux性能优化
    python 内置数据结构 字符串
    python内置数据结构
    python GC、分支、循环
  • 原文地址:https://www.cnblogs.com/QW-lzm/p/8947970.html
Copyright © 2011-2022 走看看