阳光沙滩博客系统-配置log日志输出

# 添加依赖

如果我们是添加了

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
1
2
3
4

依赖,则不需要再添加log的依赖了。

因为已经包含了log依赖了

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
    <version>2.0.3.RELEASE</version>
    <scope>compile</scope>
</dependency>
1
2
3
4
5
6

Log的

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-logging</artifactId>
    <version>2.0.3.RELEASE</version>
    <scope>compile</scope>
</dependency>
1
2
3
4
5
6

# Log级别

ALL < TRACE < DEBUG < INFO < WARN < ERROR < FATAL
1

# 配置文件logback-spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <!--控制台输出log-->
    <appender name="Console" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>[%d{yyyy/MM/dd-HH:mm:ss}] [%thread] [%-5level] [%logger] : %msg%n</pattern>
            <!--编码-->
            <charset>UTF-8</charset>
        </encoder>
    </appender>
    <!--文件存储-->
    <appender name="fileStore" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>INFO</level>
        </filter>
        <File>logs/sob_system_blog.log</File>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!--分日期存储,最多保存30天,每个文件最大10M-->
            <fileNamePattern>logs/sob_system_blog.%d.%i.log</fileNamePattern>
            <maxHistory>30</maxHistory>
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>10MB</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
        </rollingPolicy>
        <!--输出的内容格式-->
        <encoder>
            <pattern>[%d{yyyy/MM/dd-HH:mm:ss}] [%thread] [%-5level] [%logger] : %msg%n</pattern>
            <!--编码-->
            <charset>UTF-8</charset>
        </encoder>
    </appender>

    <logger name="net.sunofbeach.blog" level="DEBUG" additivity="false">
        <appender-ref ref="Console"/>
        <appender-ref ref="fileStore"/>
    </logger>

    <!-- 控制台输出日志级别 -->
    <root level="INFO">
        <appender-ref ref="Console"/>
    </root>
</configuration>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42

# lombok依赖

为了方便使用我们直接添加@Slf4j注解,就可以使用log了

<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.12</version>
    <scope>provided</scope>
</dependency>
1
2
3
4
5
6
7

安装lombok插件

使用时添加@Slf4j注解,通过log调用即可。

上次更新: 2022/03/28, 23:04:38