[JAVA] logback log

Uncle Cthulhu 2022-11-24 21:24:43 阅读数:201

javalogbacklog

1. pom.xml

<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>org.logback-extensions</groupId>
<artifactId>logback-ext-spring</artifactId>
<version>0.1.2</version>
<exclusions>
<exclusion>
<artifactId>logback-classic</artifactId>
<groupId>ch.qos.logback</groupId>
</exclusion>
</exclusions>
</dependency>

2. logback.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="false">
<!--定义日志文件的存储地址 勿在 LogBack 的配置中使用相对路径 -->
<!--<property name="LOG_HOME" value="/home/work/logs/procuresLog"/>-->
<property name="LOG_HOME" value="/Users/zhangenke/Documents/construn/logback"/>
<!-- 控制台输出 -->
<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{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %X{requestUUID} %X{userId} %X{User-Agent} %X{requestMap}
%-5level %message %n
</pattern>
</encoder>
</appender>
<!-- 按照每天生成日志文件 -->
<appender name="TRACE_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_HOME}/trace.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<FileNamePattern>${LOG_HOME}/trace.%d{yyyy-MM-dd}.%i.log</FileNamePattern>
<MaxHistory>10</MaxHistory>
<MaxFileSize>2048MB</MaxFileSize>
<totalSizeCap>20GB</totalSizeCap>
</rollingPolicy>
<layout>
<pattern>[%thread][%date{yyyy-MM-dd HH:mm:ss}][%-5level] [%logger:%line]%X{requestUUID} %X{userId}
%X{User-Agent} %X{requestMap} %msg%n
</pattern>
</layout>
</appender>
<appender name="INFO_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_HOME}/info/info.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<FileNamePattern>${LOG_HOME}/info/info.%d{yyyy-MM-dd}.%i.log</FileNamePattern>
<MaxHistory>10</MaxHistory>
<MaxFileSize>2048MB</MaxFileSize>
<totalSizeCap>20GB</totalSizeCap>
</rollingPolicy>
<layout>
<pattern>[%thread][%date{yyyy-MM-dd HH:mm:ss}][%-5level] [%logger:%line] %X{requestUUID} %X{userId}
%X{User-Agent} %X{requestMap} %msg%n
</pattern>
</layout>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>INFO</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
</appender>
<appender name="DEBUG_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_HOME}/debug/debug.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<FileNamePattern>${LOG_HOME}/debug/debug.%d{yyyy-MM-dd}.%i.log</FileNamePattern>
<MaxHistory>10</MaxHistory>
<MaxFileSize>2048MB</MaxFileSize>
<totalSizeCap>20GB</totalSizeCap>
</rollingPolicy>
<layout>
<pattern>[%thread][%date{yyyy-MM-dd HH:mm:ss}][%-5level] [%logger:%line] %X{requestUUID} %X{userId}
%X{User-Agent} %X{requestMap} %msg%n
</pattern>
</layout>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>DEBUG</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
</appender>
<appender name="WARN_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_HOME}/warn/warn.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<FileNamePattern>${LOG_HOME}/warn/warn.%d{yyyy-MM-dd}.%i.log</FileNamePattern>
<MaxHistory>10</MaxHistory>
<MaxFileSize>2048MB</MaxFileSize>
<totalSizeCap>20GB</totalSizeCap>
</rollingPolicy>
<layout>
<pattern>[%thread][%date{yyyy-MM-dd HH:mm:ss}][%X{userId}] [%X{requestUUID}][%X{terminalPhone}]
[%-5level] [%logger:%line] %X{requestUUID} %X{userId} %X{User-Agent} %X{requestMap} %msg%n
</pattern>
</layout>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>WARN</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
</appender>
<appender name="ERROR_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_HOME}/error/error.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<FileNamePattern>${LOG_HOME}/error/error.%d{yyyy-MM-dd}.%i.log</FileNamePattern>
<MaxHistory>10</MaxHistory>
<MaxFileSize>2048MB</MaxFileSize>
<totalSizeCap>20GB</totalSizeCap>
</rollingPolicy>
<layout>
<pattern>[%thread][%date{yyyy-MM-dd HH:mm:ss}][%-5level] [%logger:%line] %X{requestUUID} %X{userId}
%X{User-Agent} %X{requestMap} %msg%n
</pattern>
</layout>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERROR</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
</appender>
<!-- 打印sql -->
<appender name="daoFILE" class="ch.qos.logback.core.rolling.RollingFileAppender">-->
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<FileNamePattern>${LOG_HOME}/SQL/SQL.log.%d{yyyy-MM-dd}.%i.log</FileNamePattern>
<MaxHistory>10</MaxHistory>
<MaxFileSize>2048MB</MaxFileSize>
<totalSizeCap>20GB</totalSizeCap>
</rollingPolicy>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %X{requestUUID} %X{userId} %X{User-Agent}
%X{requestMap} %logger{50} %message %n
</pattern>
</encoder>
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<MaxFileSize>10MB</MaxFileSize>
</triggeringPolicy>
</appender>
<!--<logger name="dao" level="DEBUG">-->
<!--&lt;!&ndash;daoFILEdefined for the actualappender&ndash;&gt;-->
<!--<appender-ref ref="daoFILE"/>-->
<!--</logger>-->
<!--输出sql语句-->
<logger name="com.construn.procure.dao" level="DEBUG">
<appender-ref ref="daoFILE"/>
</logger>
<root level="INFO">
<!--控制台输出-->
<appender-ref ref="STDOUT"/>
<!--文件输出-->
<appender-ref ref="TRACE_FILE"/>
<appender-ref ref="INFO_FILE"/>
<appender-ref ref="DEBUG_FILE"/>
<appender-ref ref="WARN_FILE"/>
<appender-ref ref="ERROR_FILE"/>
<appender-ref ref="daoFILE"/>
</root>
</configuration>

2018-10-23优化

copyright:author[Uncle Cthulhu],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/328/202211242120378667.html