zoukankan      html  css  js  c++  java
  • logback 配置

    logback 的使用说明

    1、maven 依赖配置

     <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <logback.version>1.1.7</logback.version>
        <slf4j.version>1.7.21</slf4j.version>
      </properties>
    
      <dependencies>
        <dependency>
          <groupId>org.slf4j</groupId>
          <artifactId>slf4j-api</artifactId>
          <version>${slf4j.version}</version>
          <scope>compile</scope>
        </dependency>
        <dependency>
          <groupId>ch.qos.logback</groupId>
          <artifactId>logback-core</artifactId>
          <version>${logback.version}</version>
        </dependency>
        <dependency>
          <groupId>ch.qos.logback</groupId>
          <artifactId>logback-classic</artifactId>
          <version>${logback.version}</version>
          </dependency>
      </dependencies>

    2、logback 配置

    <?xml version="1.0" encoding="UTF-8"?>
    <configuration scan="true" scanPeriod="60 seconds" debug="false">
        <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
            <encoding>UTF-8</encoding>
            <encoder>
                <pattern>
                    [%d{yyyy-MM-dd HH:mm:ss}] %5p %c:%L - %m%n
                </pattern>
            </encoder>
            <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
                    <level>INFO</level>
            </filter>
        </appender>
    
        <appender name="logback-demo-info" class="ch.qos.logback.core.rolling.RollingFileAppender">
            <File>${log.dirs}/logback-demo-info.log</File>
            <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                <fileNamePattern>${log.dirs}/net5decoration-info.log.%d{yyyy-MM-dd}.%i.gz</fileNamePattern>
                <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                    <maxFileSize>600 MB</maxFileSize>
                </timeBasedFileNamingAndTriggeringPolicy>
                <maxHistory>30</maxHistory>
            </rollingPolicy>
            <encoder>
                <pattern>
                    <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
                </pattern>
            </encoder>
        </appender>
      
      
        <appender name="logback-demo-error" class="ch.qos.logback.core.rolling.RollingFileAppender">
            <!-- 文件名 -->
            <File>${log.dirs}/logback-demo-error.log</File>
            <!-- 设置文件切割策略-->
            <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                <!-- 文件备份的名字 -->
                <fileNamePattern>${log.dirs}/net5decoration-error.log.%d{yyyy-MM-dd}.gz</fileNamePattern>
                <!-- 超过 600M 就压缩备份-->
                <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                    <maxFileSize>600 MB</maxFileSize>
                </timeBasedFileNamingAndTriggeringPolicy>
                 <!-- 保留最多 30 天的历史日志记录-->
                <maxHistory>30</maxHistory>
            </rollingPolicy>
            <encoder>
                <pattern>
                    <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
                </pattern>
            </encoder>
            <filter class="ch.qos.logback.classic.filter.LevelFilter">
                <level>ERROR</level>
                <onMatch>ACCEPT</onMatch>
                <onMismatch>DENY</onMismatch>
            </filter>
        </appender>
      
      <--mybatis 的日志输出级别配置-->
        <logger name="com.*.mapper" level="debug"/>
      
      <!-- error 级别日志的配置 -->
        <logger name="errorAppender" additivity="false" level="ERROR">
            <appender-ref ref="logback-demo-error"/>
        </logger>
    
        <root level="INFO">
            <appender-ref ref="logback-demo-info"/>
            <appender-ref ref="console"/>
        </root>
    </configuration>

    3、在 java 文件中的使用 

    private static final Logger logger = LoggerFactory.getLogger(ClassName.class);
    logger.error("");
    logger.info("");

    具体的更详细的介绍可以看看官网

    https://logback.qos.ch/manual/index.html

    或者看看这哥们写的,也很详细

    https://www.cnblogs.com/warking/p/5710303.html

  • 相关阅读:
    IDEA工具java开发之 开发中必会的技能
    IDEA工具java开发之项目初始化配置 细节和目录结构
    有关java编辑改变编码器时出现HelloWorld.java:3: 错误: 编码GBK的不可映射字符 * 瀹冨皢鎵撳嵃瀛楃�涓? Hello World
    java开发环境的搭建 第一步安装JDK 第二步配置环境变量 检查java中的jdk是否安装成功
    解决vue中使用echarts的formatter提示警告的问题
    数组去重及element UI 远程模糊查询
    有关echar图设置宽度100%不起作用的问题
    echarts 中的bar3D柱子图在geo3D地图中显示不出来
    【SQL】约束
    【SQL】视图
  • 原文地址:https://www.cnblogs.com/zhuzi91/p/8414296.html
Copyright © 2011-2022 走看看