您好,登录后才能下订单哦!
前言
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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。