道阻且长,行则将至,走慢一点没关系,不停下就好|

Ac_c0mpany丶

园龄:3年7个月粉丝:6关注:3

2022-11-18 17:59阅读: 109评论: 0推荐: 0

Spring Boot集成日志框架

日志框架

一、日志的作用

日志 (Log) 的作用:

开发调试:在开发过程中,进行逻辑跟踪、查看运行结果
问题排查:辅助排查和定位线上问题,优化程序运行性能
状态监控:监控系统的运行状态、检测非授权的操作
数据分析:日志中蕴含了大量的用户数据,包括点击行为,兴趣偏好等,用户数据对于公司下一步的战略方向有一定的指导方向

二、日志框架总结

  • 日志接口:JCL、SLF4J、Log4j2
  • 日志实现:JUL、Log4j 1.x、Logback、Log4j 2.x

三、Spring Boot集成日志框架

在SpringBoot中,日志框架的建议:

  • SLF4J + Logback
  • SLF4J + Log4j 2.x

四、Spring Boot集成Logback

SpringBoot 默认已经继承了 Logback,不用再添加 Logback 的依赖,配置文件位置是:

  • classpath:logback.xml
  • classpath:logback-spring.xml(SpringBoot 推荐)

SpringBoot 内置的 Logback 配置

  • spring-boot.jar
  • org/springframework/boot/logging/logback/defaults.xml

五、Spring Boot集成Log4j 2.x

  • 通过exclusion排除包
<dependencies>
   <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <exclusions>
        	<!-- 去掉logging -->
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
	<!-- 添加Log4j2的starter -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-log4j2</artifactId>
    </dependency>
</dependencies>

配置文件的位置应该是:

  • classpath:log4j2.xml
  • classpath: log4j2-spring.xml(SpringBoot 推荐)

SpringBoot 内置的 Log4j 2.x 默认配置

  • spring-boot.jar
  • org/springframework/boot/logging/log4j2/log4j2.xml

六、注解@Slf4j

@Slf4j是用来做日志输出的,相当于手写下面这行代码:

private final Logger logger = LoggerFactory.getLogger(当前类名.class);

如果不想每次都写上面代码,可以用注解@Slf4j,然后就可以使用log.info()打印日志了。

例如:

@Slf4j
public class LogDemo {
    public static void main(String[] args) {
        log.info("message");
        log.error("error");
        log.warn("warning");
    }
}

本文作者:Ac_c0mpany丶

本文链接:https://www.cnblogs.com/keyongkang/p/16902470.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   Ac_c0mpany丶  阅读(109)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起
  1. 1 You Are My Sunshine REOL
You Are My Sunshine - REOL
00:00 / 00:00
An audio error has occurred.

作曲 : Traditional

You are my sunshine

My only sunshine.

You make me happy

When skies are gray.

You'll never know, dear,

How much I love you.

Please don't take my sunshine away

The other night, dear,

When I lay sleeping

I dreamed I held you in my arms.

When I awoke, dear,

I was mistaken

So I hung my head and cried.

You are my sunshine,

My only sunshine.

You make me happy

When skies are gray.

You'll never know, dear,

How much I love you.

Please don't take my sunshine away.

You are my sunshine,

My only sunshine

You make me happy

When skies are gray.

You'll never know, dear

How much I love you

Please don't take my sunshine away

Please don't take my sunshine away.

Please don't take my sunshine away.