参考博客:长路
logback可以看作是log4j的升级版;logback分为三个模块, logback-core
, logback-classic
和logback-access
,其中 logback-core是核心,其他两个模块依赖core。
logback-core
是其它两个模块的基础模块。logback-access
访问模块与Servlet容器集成提供通过Http
来访问日志的功能。logback-classic
是 log4j 的一个改良版本。logback-classic完整实现SLF4J API。package ch.qos.logback.classic;
public final class Level implements Serializable {
public static final Level OFF = new Level(2147483647, "OFF");
public static final Level ERROR = new Level(40000, "ERROR");
public static final Level WARN = new Level(30000, "WARN");
public static final Level INFO = new Level(20000, "INFO");
public static final Level DEBUG = new Level(10000, "DEBUG");
public static final Level TRACE = new Level(5000, "TRACE");
public static final Level ALL = new Level(-2147483648, "ALL");
...
}
日志级别排序为:TRACE
< DEBUG
<INFO
< WARN
<ERROR
。
默认日志等级为Debug,其中OFF
与ALL
作为日志开关。
Logger
:日志的记录器,主要用于存放日志对象,也可以定义日志类型、级别。
Appender
:用于指定日志输出的目的地,目的地可以是控制台、文件、数据库等等。
Layout
:负责把事件转换成字符串,格式化的日志信息的输出。
在 Logback 中 Layout 对象被封装在encoder中。 也就是说我们未来使用的 encoder 其实就是Layout。
配置 | 介绍 |
---|---|
`` | |
|
通过该标签可以直接读取 application.yml 等配置文件中的配置 |
`` |
Logback提供了3种配置文件
logback.groovy
logback-test.xml
logback.xml
(常用)(Maven 项目通常放在resource
文件夹中)如果都不存在则采用默认的配置。(日志等级为DEBUG,默认输出到console)
命令 | 解释 |
---|---|
%-5level |
level日志等级设置为 5 个字符,左对齐 |
%d{yyyy-MM-dd HH:mm:ss.SSS} |
日期(例如:2022-10-18 21:39:22.681) |
%c |
当前类的完整名称(全限定名) |
%M |
当前执行日志的方法 |
%L |
行号 |
%thread |
线程名称 |
%m或者%msg |
信息 |
%n |
换行 |
注意:SpringBoot 配置文件的加载顺序如下:
logback.xml
——> application.properties
——> logback-spring.xml
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-apiartifactId>
<version>1.7.25version>
dependency>
<dependency>
<groupId>ch.qos.logbackgroupId>
<artifactId>logback-classicartifactId>
<version>1.2.11version>
<configuration>
<property name="pattern" value="[%-5level] %d{yyyy-MM-dd HH:mm:ss} %c %M %L %thread %m%n"/>
<appender name="consoleAppender" class="ch.qos.logback.core.ConsoleAppender">
<target>
System.out
target>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>${pattern}pattern>
encoder>
appender>
<root level="All">
<appender-ref ref="consoleAppender">appender-ref>
root>
configuration>
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* logback日志框架管理
*/
public class LogbackTest {
@Test
public void logTest(){
Logger logger = LoggerFactory.getLogger(LogbackTest.class);
logger.error("error 错误信息");
logger.warn("warn 警告信息");
logger.info("info 运行信息");
logger.debug("debug 调试信息");
logger.trace("trace 追踪信息");
}
}
<configuration>
<property name="pattern" value="[%-5level] %d{yyyy-MM-dd HH:mm:ss} %c %M %L %thread %m%n"/>
<property name="logDirectory" value="./logs/log.txt" />
<appender name="consoleAppender" class="ch.qos.logback.core.ConsoleAppender">
<target>
System.out
target>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>${pattern}pattern>
encoder>
appender>
<appender name="fileAppender" class="ch.qos.logback.core.FileAppender">
<file>${logDirectory}file>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>${pattern}pattern>
encoder>
appender>
<root level="All">
<appender-ref ref="consoleAppender" />
<appender-ref ref="fileAppender" />
root>
configuration>
yml 配置:
# logback日志存放位置
log:
path:
windows: /home/data/logs
# 打印sql日志
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.slf4j.Slf4jImpl
备注:
mybatis-plus日志配置,使用org.apache.ibatis.logging.stdout.StdOutImpl
只能打印SQL日志在控制台;如果想要SQL日志d印在日志文件中需要使用org.apache.ibatis.logging.slf4j.Slf4jImpl
;
<configuration scan="true">
<include resource="org/springframework/boot/logging/logback/defaults.xml"/>
<include resource="org/springframework/boot/logging/logback/console-appender.xml"/>
<include resource="org/springframework/boot/logging/logback/file-appender.xml"/>
<property name="FILE_LOG_PATTERN" value="[%-5level] %d{yyyy-MM-dd HH:mm:ss} %c %M %L %thread %m%n"/>
<property name="APP_NAME" value="monitor" />
<springProperty scope="context" name="APP_HOME" source="log.path.linux" />
<property name="LOG_HOME_PATH" value="${APP_HOME}/${APP_NAME}" />
<property name="DEBUG_LOG_FILE" value="${LOG_HOME_PATH}/debug/${APP_NAME}_debug"/>
<property name="INFO_LOG_FILE" value="${LOG_HOME_PATH}/info/${APP_NAME}_info"/>
<property name="WARN_LOG_FILE" value="${LOG_HOME_PATH}/warn/${APP_NAME}_warn"/>
<property name="ERROR_LOG_FILE" value="${LOG_HOME_PATH}/error/${APP_NAME}_error"/>
<property name="SQL_LOG_FILE" value="${LOG_HOME_PATH}/sql/${APP_NAME}_sql"/>
<appender name="ERROR_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<encoder>
<pattern>${FILE_LOG_PATTERN}pattern>
encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>${ERROR_LOG_FILE}.%d{yyyy-MM-dd}.logFileNamePattern>
<MaxHistory>60MaxHistory>
rollingPolicy>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERRORlevel>
<onMatch>ACCEPTonMatch>
<onMismatch>DENYonMismatch>
filter>
appender>
<appender name="INFO_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<encoder>
<pattern>${FILE_LOG_PATTERN}pattern>
encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>${INFO_LOG_FILE}.%d{yyyy-MM-dd}.logFileNamePattern>
<MaxHistory>60MaxHistory>
rollingPolicy>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>INFOlevel>
<onMatch>ACCEPTonMatch>
<onMismatch>DENYonMismatch>
filter>
appender>
<appender name="WARN_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<encoder>
<pattern>${FILE_LOG_PATTERN}pattern>
encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>${WARN_LOG_FILE}.%d{yyyy-MM-dd}.logFileNamePattern>
<MaxHistory>60MaxHistory>
rollingPolicy>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>WARNlevel>
<onMatch>ACCEPTonMatch>
<onMismatch>DENYonMismatch>
filter>
appender>
<appender name="DEBUG_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<encoder>
<pattern>${FILE_LOG_PATTERN}pattern>
encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>${DEBUG_LOG_FILE}.%d{yyyy-MM-dd}.logFileNamePattern>
<MaxHistory>60MaxHistory>
rollingPolicy>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>DEBUGlevel>
<onMatch>ACCEPTonMatch>
<onMismatch>DENYonMismatch>
filter>
appender>
<appender name="SQL_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>${SQL_LOG_FILE}.%d{yyyy-MM-dd}.logFileNamePattern>
<MaxHistory>60MaxHistory>
rollingPolicy>
<encoder>
<pattern>${FILE_LOG_PATTERN}pattern>
encoder>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>DEBUGlevel>
filter>
appender>
<logger name="com.jpin.water.dal.mapper" level="DEBUG" additivity="false">
<appender-ref ref="SQL_FILE" />
logger>
<root level="INFO">
<appender-ref ref="DEBUG_FILE"/>
<appender-ref ref="INFO_FILE"/>
<appender-ref ref="WARN_FILE"/>
<appender-ref ref="ERROR_FILE"/>
root>
configuration>