赞
踩
日志系统是非常重要的,常用于日常问题排查及数据分析统计等。我们将介绍如何在 SpringBoot 项目中集成 Log4j,并对 Log4j 配置文件(XML格式)的各种配置进行详细讲解。
之所以使用log4j,是因为log4j在高并发的场景下的表现相对比较优异,配置灵活且可靠,是目前市面上呗最广泛使用的日志组件。
首先,你需要在 pom.xml
文件中添加 Log4j 的 Maven 依赖。以下是一个典型的依赖配置:
<dependencies>
<!-- Spring Boot Starter -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- Log4j Dependency -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
</dependencies>
由于Spring Boot默认使用Logback,Logback的性能方面弱于Log4j,对性能要求不是非常高的系统也可以采用Logback。我们需要移除src/main/resources
目录下的logback-spring.xml
文件。
在 src/main/resources
目录下创建一个名为 log4j2.xml
的配置文件。
以下是一个基本的 Log4j2 XML 配置文件示例:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n"/>
</Console>
<File name="File" fileName="logs/app.log">
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n"/>
</File>
<RollingFile name="RollingFile" fileName="logs/rolling.log" filePattern="logs/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz">
<PatternLayout>
<pattern>%d %p %c{1.} [%t] %m%n</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy size="10MB"/>
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="Console"/>
<AppenderRef ref="File"/>
<AppenderRef ref="RollingFile"/>
</Root>
</Loggers>
</Configuration>
<Configuration>
: 根标签,包含所有的配置。<Appenders>
: 定义日志输出源,例如控制台、文件等。<Loggers>
: 定义日志记录器,可以为不同的包或类设置不同的日志级别和输出目标。Appenders配置主要是用于控制日志的输出位置的,主要有控制台、文件等位置。
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n"/>
</Console>
<File name="File" fileName="logs/app.log">
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n"/>
</File>
<RollingFile name="RollingFile" fileName="logs/rolling.log" filePattern="logs/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz">
<PatternLayout>
<Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy size="10MB"/>
</Policies>
</RollingFile>
<Root level="info">
<AppenderRef ref="Console"/>
<AppenderRef ref="File"/>
<AppenderRef ref="RollingFile"/>
</Root>
如果你想要将指定文件的输出级别和日志输出位置,可以参考下方配置:
使用Logger指定类的全限定名,指定level为debug,并将日志输出到RollingDemoFile,最终日志就会输出到logs/demo.log文件中。
各占位符详解
%d: 显示当前日志的时间和日期,默认格式是 ISO8601,可以自定义日期时间格式,例如 %d{yyyy-MM-dd HH:mm:ss}。
%p: 显示日志级别,类似 DEBUG, INFO, WARN, ERROR, FATAL。
%c{1.}: 显示日志记录器的类别名(一般是类的全名)。 {1.} 表示级别数,只显示类名不包含包名。如果指定 {2.}, 则显示包名的最后两段和类名,如 com.example.ClassName。
%t: 显示记录该日志的线程名称。
%m: 显示日志消息内容。
%n: 换行符,根据操作系统不同会自动选择合适的换行符。
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<RollingFile name="RollingDemoFile" fileName="logs/demo.log"
filePattern="logs/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz">
<PatternLayout>
<Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy size="10MB"/>
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<!-- 为某个类单独设置日志级别和输出目标 -->
<Logger name="com.example.DemoClass" level="debug" additivity="false">
<AppenderRef ref="RollingDemoFile"/>
</Logger>
<Root level="info">
<AppenderRef ref="Console"/>
<AppenderRef ref="File"/>
<AppenderRef ref="RollingFile"/>
</Root>
</Loggers>
</Configuration>
日志在软件开发中具有重要作用,主要表现在以下几个方面:
在现代分布式系统中,日志数据的采集往往依赖于一些专业的日志管理系统,如 ELK(Elasticsearch, Logstash, Kibana)堆栈或 Graylog。
日志数据采集后,我们可以使用强大的分析工具进行数据分析:
以下是一个ELK堆栈的简单使用案例:
filebeat.yml
文件,将日志文件输送至 Logstash:filebeat.inputs:
- type: log
paths:
- /path/to/your/logs/*.log
output.logstash:
hosts: ["logstash:5044"]
logstash.conf
文件,接收 Filebeat 发送的日志并存储到 Elasticsearch:input {
beats {
port => 5044
}
}
filter {
# 可以添加一些滤镜,例如解析日志格式
}
output {
elasticsearch {
hosts => ["elasticsearch:9200"]
index => "your-log-index-%{+YYYY.MM.dd}"
}
}
通过上述方式,我们可以实现对 SpringBoot 项目日志的全面采集与分析,有效提高系统的可维护性和性能监控能力。
在这篇博客中,我们详细介绍了如何在 SpringBoot 项目中集成 Log4j 以及 Log4j 配置文件的各种配置方法。通过合理地配置日志系统,我们能够更好地进行故障排查、性能监控和安全审计。进一步地,通过日志数据采集和分析工具,我们能够实现对日志数据的深度挖掘,为我们的应用提供了更便捷的数据可视化等支持。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。