• slf4japi整合maven 工程日志配置文件


    springmvc项目

    pom.xml:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.5</version>
    </dependency>
    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-core</artifactId>
        <version>1.2.3</version>
    </dependency>
    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>1.2.3</version>
    </dependency>

    log.properties:

    log.level=debug
    log.path=../logs

    配置文件logback.xml:

    复制代码

    <?xml version="1.0" encoding="UTF-8"?>
    <configuration scan="true" scanPeriod="10 seconds">
    <property resource="log.properties"/>

    <!-- 控制台输出 -->
    <appender name="STDOUT"
    class="ch.qos.logback.core.ConsoleAppender">
    <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
    <!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符 -->
    <pattern>%d{yy-MM-dd HH:mm:ss.sss} [%thread] %-5level %logger \(%file:%line\) - %X{trackInfo} %msg%n</pattern>
    <charset>UTF-8</charset> <!-- 此处设置字符集 -->
    </encoder>
    </appender>


    <!-- 按照每天生成日志文件 -->
    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
    <!--日志文件输出的文件名 -->
    <FileNamePattern>${log.path}/log-%d{yyyy-MM-dd}.%i.log</FileNamePattern>
    <!--日志文件保留天数 -->
    <MaxHistory>30</MaxHistory>
    </rollingPolicy>
    <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
    <!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符 -->
    <!-- <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} -%msg%n</pattern> -->

    <pattern>%d{yy-MM-dd HH:mm:ss.sss} [%thread] %-5level %logger - %X{trackInfo} %msg%n</pattern>
    <charset>UTF-8</charset> <!-- 此处设置字符集 -->
    </encoder>
    <!--日志文件最大的大小 -->
    <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
    <MaxFileSize>64MB</MaxFileSize>
    </triggeringPolicy>
    </appender>

    <logger name="org.springframework" level="WARN" />
    <logger name="org.mybatis" level="WARN"/>

    <!-- 日志输出级别 -->
    <root level="${log.level}">
    <appender-ref ref="STDOUT" />
    <appender-ref ref="FILE" />
    </root>
    </configuration>

    复制代码

    SpringBoot项目:

    pom.xml:

    <dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.25</version>
    </dependency>

    application.properties:

    log: 
      path: ../logs
      level: info

    logback-spring.xml:

    复制代码

    <!-- Logback configuration. See http://logback.qos.ch/manual/index.html -->
    <configuration scan="true" scanPeriod="10 seconds">
    <springProperty scope="context" name="logLevel" source="log.level"/>
    <springProperty scope="context" name="logPath" source="log.path"/>
    <!-- Simple file output -->
    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <encoder>
    <pattern>%d{yy-MM-dd HH:mm:ss.sss} [%thread] %-5level %logger - %X{trackInfo} %msg%n</pattern>
    <charset>UTF-8</charset> <!-- 此处设置字符集 -->
    </encoder>

    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
    <!-- rollover daily 配置日志所生成的目录以及生成文件名的规则 -->
    <fileNamePattern>${logPath}/log-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
    <timeBasedFileNamingAndTriggeringPolicy
    class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
    <!-- or whenever the file size reaches 64 MB -->
    <maxFileSize>64 MB</maxFileSize>
    </timeBasedFileNamingAndTriggeringPolicy>
    </rollingPolicy>
    </appender>


    <!-- Console output -->
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <!-- encoder defaults to ch.qos.logback.classic.encoder.PatternLayoutEncoder -->
    <encoder>
    <pattern>%d{yy-MM-dd HH:mm:ss.sss} [%thread] %-5level %logger \(%file:%line\) - %X{trackInfo} %msg%n</pattern>
    <charset>UTF-8</charset> <!-- 此处设置字符集 -->
    </encoder>
    </appender>


    <logger name="jndi" level="WARN" />
    <logger name="org.eclipse" level="WARN" />
    <logger name="org.springframework" level="WARN" />
    <logger name="org.mongodb" level="WARN" />
    <logger name="org.mybatis" level="WARN"/>

    <root level="${logLevel}">
    <appender-ref ref="FILE" />
    <appender-ref ref="STDOUT" />
    </root>

    <!-- For loggers in the these namespaces, log at all levels. -->
    <logger name="pedestal" level="ALL" />
    <logger name="hammock-cafe" level="ALL" />
    <logger name="user" level="ALL" />
    </configuration>

    复制代码

     

     
     
  • 相关阅读:
    MySQL 重置Mysql root用户账号密码
    Git 在同一台机器上配置多个Git帐号
    JQuery 获取元素到浏览器可视窗口边缘的距离
    JQuery Ztree 树插件配置与应用小结
    JQuery iframe宽高度自适应浏览器窗口大小的解决方法
    JQuery jquerysessionjs插件使用介绍
    JQuery Deferred对象使用小结
    JQuery javascript实现父子页面相互调用
    JavaScript 使用new关键字调用函数
    CSS 让div,span等块级、非快级元素排列在同一行
  • 原文地址:https://www.cnblogs.com/exmyth/p/15529121.html
Copyright © 2020-2023  润新知