SpringBoot集成Logback将日志写入文件夹

一、logback简介:

目前比较常用的ava日志框架:Logback、log4j、log4j2、JUL等等。

Logback是在log4j的基础上重新开发的一套日志框架,是完全实现SLF4J接口API(也叫日志门面)。

Logback 的架构非常通用,可以应用于不同的环境。目前logback分为三个模块,logback-core,logback-classic和logback-access。

logback-core模块为其他两个模块奠定了基础。logback-classic模块原生实现了SLF4JAPI,因此您可以轻松地在logback和其他日志记录框架(例如 log4j1.x或java.util.logging (JUL))之间来回切换。

logback-access模块与Tomcat和 Jetty 等 Servlet容器集成,以提供HTTP访问日志功能。请注意,您可以轻松地在logback-core之上构建自己的模块。

二、 SpringBoot集成Logback

  1. 创建项目:不做过多的描述,不会点这里
  2. 导入pom依赖
xml 复制代码
<dependencies>

        <!-- Spring Boot Starter -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <!-- Spring Boot Starter Logging (默认已经包含在starter中) -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
  1. 编写logback.xml
xml 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <!-- 控制台输出 -->
    <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%white(%contextName - %d{yyyy-MM-dd HH:mm:ss}) %green([%c][%t][%L]) %highlight(%-5level) - %yellow(%msg%n)</pattern>
        </encoder>
    </appender>
    <!-- 文件输出 -->
    <appender name="file" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>logs/springboot.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>logs/springboot-%d{yyyy-MM-dd}.log</fileNamePattern>
            <maxHistory>30</maxHistory>
        </rollingPolicy>
    <!--文件中的输出格式-->
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%c][%t][%L][%p] - %msg%n</pattern>
        </encoder>
    </appender>
    <!-- 日志级别配置 -->
    <root level="info">
        <appender-ref ref="console"/>
        <appender-ref ref="file"/>
    </root>
</configuration>
  1. 编写Controller层
java 复制代码
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author Ysl
 * @Date 2024/5/22
 * @name Myselflean
 **/
@RestController
public class LogController {

    private static final Logger log = LoggerFactory.getLogger(LogController.class);

    @GetMapping("/log")
    public String log() {
        log.info("开始输出日志--------start");
        String msg = "编写业务代码";
        log.info("日志输出结束--------end:"+ msg);
        return msg;
    }
}
  1. 输入网址进行访问:http://localhost:8080/log
  2. 控制台输出后会出现logs文件夹,里面存放.log日志

  3. 如果需要更改输出格式,可在logback.xml中进行修改配置,可查相关资料完善。
相关推荐
脚本自动化开发1 小时前
柔光屏平板怎么选?华为 MatePad Air Z 柔光版学
后端
程序猿DD2 小时前
OctaFuse 2.19.0:重做管理后台界面与交互,模型目录同步更新
后端
金銀銅鐵3 小时前
带您看懂字节码指令(一):一帧里有什么
java·后端·ai编程
码事漫谈4 小时前
Agent 测评是什么?大白话讲一讲
后端
皮皮林5514 小时前
在 IDEA 里斗个地主不过分吧!
后端
nice_lcj5204 小时前
【从零理解 Spring AI · 14】RAG 的四个环节:加载、切分、向量化、检索
java·spring boot·embedding·向量数据库·rag·spring ai·ai应用开发
SomeOtherTime5 小时前
Rust所有权(AI回答)
开发语言·后端·rust
程序员cxuan6 小时前
离谱,AI 大佬顺手搓了个 YouTube ,而我顺手给他搓了个中文插件
人工智能·后端·程序员
学长毕设6 小时前
基于springboot科创竞赛信息平台-附源码
java·spring boot·后端
深蓝AI7 小时前
Gemini Decisions API 来了:一次前向打分取代逐字解码,校准误差 0.0250 才是 agent 分支调用的真门槛
人工智能·后端