您好,登錄后才能下訂單哦!
前言
Spring Boot 默認使用Logback,來打印日志,這里還想說的SLFJ(Simple Logging Facade for Java),它們之間的關系,一張圖,說明一切:
maven 配置
<!--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.
<?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
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。