服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - Spring Boot使用Log4j2的实例代码

Spring Boot使用Log4j2的实例代码

2020-12-08 14:51OrangeJoke Java教程

这篇文章主要介绍了Spring Boot使用Log4j2的实例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

前言

Spring Boot 默认使用Logback,来打印日志,这里还想说的SLFJ(Simple Logging Facade for Java),它们之间的关系,一张图,说明一切:

Spring Boot使用Log4j2的实例代码

maven 配置

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<!--use log4j2 property-->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter</artifactId>
      <exclusions>
        <exclusion>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-logging</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-log4j2</artifactId>
    </dependency>
 
    <!--flume log4j appender-->
    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-flume-ng</artifactId>
      <version>2.6</version>
    </dependency>

log4j2.xml 配置

Log4j2 提供非常丰富的Appender,比如CassandraAppender,AsyncAppender,我们这里配置了简单的Flume Appnder,Failover Appender.

?
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
43
44
45
46
47
48
49
50
51
52
53
54
<?xml version="1.0" encoding="UTF-8"?>
<Configuration monitorInterval="60">
  <Properties>
    <Property name="source">api-web</Property>
    <Property name="log-pattern">%d{yyyy-MM-dd HH:mm:ss.SSS} [%X{userId}] [%t] [%-5level] %c{1.}@%M%L - %m%n</Property>
    <!--get product dynamic property-->
  </Properties>
  <Appenders>
 
    <RollingFile name="RollingFile" fileName="logs/app.log"
           filePattern="logs/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz">
      <PatternLayout>
        <Pattern>${log-pattern}</Pattern>
      </PatternLayout>
      <Policies>
        <TimeBasedTriggeringPolicy />
        <SizeBasedTriggeringPolicy size="250 MB"/>
      </Policies>
    </RollingFile>
 
    <Flume name="eventLogger" compress="false" type="avro" ignoreExceptions="false">
      <Agent host="192.168.31.115" port="4444"/>
      <Agent host="127.0.0.1" port="4444"/>
      <PatternLayout>
        <pattern>
          <pattern>${log-pattern}</pattern>
        </pattern>
      </PatternLayout>
    </Flume>
 
    <Console name="Console-Appender" target="SYSTEM_OUT" ignoreExceptions="false">
      <PatternLayout>
        <pattern>${log-pattern}</pattern>
      </PatternLayout>
    </Console>
 
    <!-- 主要Apeender失败的时候使用备用Appender-->
    <Failover name="Failover" primary="eventLogger">
      <Failovers>
        <AppenderRef ref="RollingFile"/>
      </Failovers>
    </Failover>
  </Appenders>
  <Loggers>
    <!--在需要同时打印文件和发送至flume的时候使用-->
    <!--<Logger name="" level="info" additivity="false">-->
      <!--<AppenderRef ref="RollingFile"/>-->
    <!--</Logger>-->
    <Root level="info">
      <AppenderRef ref="Console-Appender"/>
      <AppenderRef ref="Failover"/>
    </Root>
  </Loggers>
</Configuration>

总结

如果遇到识别不了log4j2.properties/xml,可以在application.properties,中指定 logging.config=classpath:log4j2.properties/xml

参考

https://springframework.guru/introducing-log4j-enterprise-class-logging/

http://logging.apache.org/log4j/2.x/manual/appenders.html#RollingFileAppender

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家

原文链接:https://my.oschina.net/tigerlene/blog/1492015

延伸 · 阅读

精彩推荐