我改的日志配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="60 seconds" debug="false">
    <contextName>logback</contextName>
    <!--自定义一个属性,用于配置日志文件名称前缀,可以用应用名-->
    <property name="log.app" value="account"/>
    <!--配置日志文件的输出路径,如果不存在会自动创建-->
    <!--<property name="log.path" value="D:/ty/account/logs"/>-->
    <property name="log.path" value="../${log.app}-logs/"/>
    <!--输出到控制台-->
    <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
        <!--此日志appender是为开发使用,只配置最底级别,控制台输出的日志级别是大于或等于此级别的日志信息-->
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>info</level>
        </filter>
        <encoder>
            <!--<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %contextName [%thread] %-5level %logger{36} - %msg%n</pattern>-->
            <!-- 格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度,%logger表示类全名,%line显示代码行号,%msg:日志消息,%n是换行符 -->
            <pattern>%yellow(%date{yyyy-MM-dd HH:mm:ss})|%cyan(%-5level) %highlight(%X{trackId})|%cyan(%thread) |%green(%logger:%yellow(%line)) |%cyan(%msg%n)</pattern>
            <charset>utf-8</charset>
        </encoder>
    </appender>

    <!--输出到info-->
    <appender name="info" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <!--时间转换器-->
        <!--<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">-->
        <!--大小和时间转换器-->
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <fileNamePattern>${log.path}/${log.app}-info-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
            <!-- each file should be at most 1GB, keep 60 days worth of history, but at most 20GB -->
            <maxFileSize>1GB</maxFileSize>
            <maxHistory>60</maxHistory>
            <totalSizeCap>20GB</totalSizeCap>
        </rollingPolicy>
        <append>true</append>
        <encoder>
            <!--<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %contextName [%thread] %-5level %logger{36}:%line - %msg%n</pattern>-->
            <pattern>%d{yyyy-MM-dd HH:mm:ss} |%-5level%X| [%thread] %logger{60}:%line|%msg%n</pattern>
            <charset>utf-8</charset>
        </encoder>
        <filter class="ch.qos.logback.classic.filter.LevelFilter"><!-- 只打印INFO日志 -->
            <level>INFO</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
      <!-- 如果想打印debug及以上级别日志就把上面3行替换成一行:<level>DEBUG</level> 即可(下面的两行是严格匹配第一行的意思)--> </filter> </appender> <!--输出到error--> <appender name="error" class="ch.qos.logback.core.rolling.RollingFileAppender"> <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy"> <fileNamePattern>${log.path}/${log.app}-error-%d{yyyy-MM-dd}.%i.log</fileNamePattern> <maxFileSize>1GB</maxFileSize> <maxHistory>60</maxHistory> <totalSizeCap>20GB</totalSizeCap> </rollingPolicy> <append>true</append> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss} |%-5level%X| [%thread] %logger:%line|%msg%n</pattern> <charset>utf-8</charset> </encoder> <filter class="ch.qos.logback.classic.filter.LevelFilter"><!-- 只打印ERROR日志 --> <level>ERROR</level> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter> </appender> <!--分别设置对应的日志输出节点 --> <root> <appender-ref ref="console" /> <!--<appender-ref ref="debug" />--> <appender-ref ref="info" /> <appender-ref ref="error" /> <!--<appender-ref ref="warn" />--> </root>

<!--排除多余心跳info及以下级别日志--> <logger name="com.ctg.itrdc.cache" level="error"> </logger> <logger name="com.alibaba.cloud.dubbo" level="error"> </logger> <logger name="org.apache.dubbo" level="error"> </logger> </configuration>

 

 

 

 

 (%logger{36}  代码class的全名,后面加{36}是对包名缩写后的最大字符为36位,可以参考如下链接:

http://logback.qos.ch/manual/layouts.html#conversionWord)

 

转:

Logback日志配置(分级别输出到不同文件)

 

项目使用springboot2.0

springboot2.0基于jdk1.8,不兼容1.7

springboot1.5.x基于jdk1.7,兼容1.8

 

项目结构图如下:

springboot日志配置【我改】_日志输出

 

 

logback配置文件名为logback-spring.xml

路径需要写在application.properties中,如下:

logging.config=classpath:log/logback-spring.xml 

注意:springboot2.0与1.5.x有区别

2.0配置:logging.config=classpath:    log/logback-spring.xml 

1.5.10配置:logging.config=classpath:  ./log/logback-spring.xml (注意前面的./)

或者logging.config=classpath:    log/logback-spring.xml ,本人暂时发现两个配置不知道什么原因,有的时候第一个行,有的时候第二个行

=======================================================================

<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="60 seconds" debug="false">
<contextName>logback</contextName>
<property name="log.path" value="D:\\JAVA_LOGS\\transferservice_Logs\\" />
<!--输出到控制台-->
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<!--此日志appender是为开发使用,只配置最底级别,控制台输出的日志级别是大于或等于此级别的日志信息-->
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>debug</level>
</filter>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %contextName [%thread] %-5level %logger{36} - %msg%n</pattern>
<charset>utf-8</charset>
</encoder>
</appender>

<!--输出到debug-->
<appender name="debug" class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${log.path}/logback-debug-%d{yyyy-MM-dd}.log</fileNamePattern>
</rollingPolicy>
<append>true</append>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %contextName [%thread] %-5level %logger{36} - %msg%n</pattern>
<charset>utf-8</charset>
</encoder>
<filter class="ch.qos.logback.classic.filter.LevelFilter"><!-- 只打印DEBUG日志 -->
<level>DEBUG</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
</appender>

<!--输出到info-->
<appender name="info" class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${log.path}/logback-info-%d{yyyy-MM-dd}.log</fileNamePattern>
</rollingPolicy>
<append>true</append>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %contextName [%thread] %-5level %logger{36} - %msg%n</pattern>
<charset>utf-8</charset>
</encoder>
<filter class="ch.qos.logback.classic.filter.LevelFilter"><!-- 只打印INFO日志 -->
<level>INFO</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
</appender>

<!--输出到error-->
<appender name="error" class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${log.path}/logback-error-%d{yyyy-MM-dd}.log</fileNamePattern>
</rollingPolicy>
<append>true</append>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %contextName [%thread] %-5level %logger{36} - %msg%n</pattern>
<charset>utf-8</charset>
</encoder>
<filter class="ch.qos.logback.classic.filter.LevelFilter"><!-- 只打印ERROR日志 -->
<level>ERROR</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
</appender>

<!--输出到warn-->
<appender name="warn" class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${log.path}/logback-warn-%d{yyyy-MM-dd}.log</fileNamePattern>
</rollingPolicy>
<append>true</append>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %contextName [%thread] %-5level %logger{36} - %msg%n</pattern>
<charset>utf-8</charset>
</encoder>
<filter class="ch.qos.logback.classic.filter.LevelFilter"><!-- 只打印WARN日志 -->
<level>WARN</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
</appender>

<!--分别设置对应的日志输出节点 -->
<root level="info">
<appender-ref ref="console" />
<appender-ref ref="debug" />
<appender-ref ref="info" />
<appender-ref ref="error" />
<appender-ref ref="warn" />
</root>

</configuration>