示例版本:
springboot:3.2.4
jdk:21
lombok:1.18.30
maven:3.6.3
1、添加依赖
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.30</version>
<scope>provided</scope>
</dependency>
2、创建 logback-springboot.xml 配置文件
在 resource 目录下创建 logback-springboot.xml 文件,并添加如下内容:
<?xml version="1.0" encoding="UTF-8" ?>
<springProperty scope="context" name="application.name" source="spring.application.name" defaultValue="javadog"/>
<springProperty scope="context" name="logging.path" source="logging.file.path" defaultValue="logs"/>
<springProperty scope="context" name="logging.file.max-size" source="logging.logback.rollingpolicy.max-size"
defaultValue="10MB"/>
<springProperty scope="context" name="logging.file.max-history" source="logging.logback.rollingpolicy.max-history"
defaultValue="30"/>
<contextName>${application.name}</contextName>
<springProfile name="dev,prod">
<!--输出到控制台-->
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>${CONSOLE_LOG_PATTERN}</pattern>
</encoder>
</appender>
<root level="info">
<appender-ref ref="console"/>
</root>
</springProfile>
<springProfile name="dev,test,prod">
<!-- info日志文件保存设置 -->
<appender name="file-info" class="ch.qos.logback.core.rolling.RollingFileAppender">
<!-- info日志文件保存地址,达到 maxHistory 时间后,更改为 fileNamePattern 文件名,并且重新创建 file 文件 -->
<file>${logging.path}/info/yuqnboot-info.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- 用于集合周期时间的日志 -->
<fileNamePattern>${logging.path}/info/yuqnboot-info.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>${logging.file.max-size}</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<!--只保留最近n天的日志-->
<maxHistory>${logging.file.max-history}</maxHistory>
</rollingPolicy>
<!-- 追加方式记录日志 -->
<append>true</append>
<encoder>
<pattern>${FILE_LOG_PATTERN}</pattern>
</encoder>
</appender>
<!-- warn日志文件保存设置 -->
<appender name="file-warn" class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>warn</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<!-- warn日志文件保存地址,达到 maxHistory 时间后,更改为 fileNamePattern 文件名,并且重新创建 file 文件 -->
<file>${logging.path}/warn/yuqnboot-warn.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${logging.path}$/warn/yuqnboot-warn.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>${logging.file.max-size}</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<!--只保留最近n天的日志-->
<maxHistory>${logging.file.max-history}</maxHistory>
</rollingPolicy>
<encoder>
<pattern>${FILE_LOG_PATTERN}</pattern>
</encoder>
</appender>
<!-- error日志文件保存设置 -->
<appender name="file-error" class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>error</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<file>${logging.path}/error/yuqnboot-error.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${logging.path}/error/yuqnboot-error.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>${logging.file.max-size}</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<!--只保留最近n天的日志-->
<maxHistory>${logging.file.max-history}</maxHistory>
</rollingPolicy>
<encoder>
<pattern>${FILE_LOG_PATTERN}</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="file-info"/>
<appender-ref ref="file-warn"/>
<appender-ref ref="file-error"/>
</root>
</springProfile>
3、测试
这里直接在启动类中测试,启动项目后,会在日志文件中添加 info 日志信息。
@SpringBootApplication
@ServletComponentScan
@Slf4j
//@EnableSwagger2
//@EnableKnife4j
public class YuqnBootStartApplication {
public static void main(String[] args) throws UnknownHostException {
ConfigurableApplicationContext application = SpringApplication.run(YuqnBootStartApplication.class,args);
info(application);
}
static void info(ConfigurableApplicationContext application) throws UnknownHostException {
Environment env = application.getEnvironment();
String ip = InetAddress.getLocalHost().getHostAddress();
String port = env.getProperty("server.port");
String active = env.getProperty("spring.profiles.active");
String contextPath = env.getProperty("server.servlet.context-path");
if (contextPath == null) {
contextPath = "";
}
log.info("\n----------------------------------------------------------\n\t" +
"欢迎访问 \thttp://www.yuqn.top\n\t" +
"示例程序【" + active + "】环境已启动! 地址如下:\n\t" +
"Local: \t\thttp://localhost:" + port + contextPath + "\n\t" +
"External: \thttp://" + ip + ':' + port + contextPath + '\n' +
"Swagger文档: \thttp://" + ip + ":" + port + contextPath + "/doc.html\n" +
"----------------------------------------------------------");
}
}
注意添加上 @Slf4j 注解,才能使用日志打印。
4、查看结果
启动项目后,可以看到项目目录下有多个 log 日志文件夹,进入 info 日志,打开 log 文件就能看到 info 信息已经写进去了。