Log4j 2.x 在Springboot应用中如何配置

Log4j 2.x 在Springboot应用中如何配置

摘要:通过本章节的学习掌握Log4j 2.x 在Springboot中配置方法


引入log4j2的依赖包 pom.xml

<dependency>
		    <groupId>org.springframework.bootgroupId>
		    <artifactId>spring-boot-starterartifactId>
		    <exclusions>
		        <exclusion>
		            <groupId>org.springframework.bootgroupId>
		            <artifactId>spring-boot-starter-loggingartifactId>
		        exclusion>
		    exclusions>
		dependency>
		<dependency>
		    <groupId>org.springframework.bootgroupId>
		    <artifactId>spring-boot-starter-log4j2artifactId>
		dependency>

resources/log4j2.xml 的配置文件


 
<Configuration status="OFF">
    <Appenders>
        
        <Console name="Console" target="SYSTEM_OUT" follow="true">
            <PatternLayout>
                <pattern>[%-5p] %d %c - %m%npattern>
            PatternLayout>
        Console>
        
       
        <RollingFile name="infoFile" fileName="logs/info.log"  filePattern="logs/info.%d{yyyy-MM-dd}-%i.log" ignoreExceptions="false">
			    <LevelRangeFilter minLevel="INFO" maxLevel="INFO" onMatch="ACCEPT" onMismatch="DENY"/>
			    <PatternLayout>
			        <Pattern>%d{yyyy-MM-dd HH:mm:ss} %-5p %m%nPattern>
			    PatternLayout>
			    <Policies>
			        <SizeBasedTriggeringPolicy size="1KB" />
			    Policies>
		    	<DefaultRolloverStrategy max="2" />
		RollingFile>
		<RollingFile name="errorFile" fileName="logs/error.log"  filePattern="logs/error.%d{yyyy-MM-dd}-%i.log" ignoreExceptions="false">
			   <LevelRangeFilter minLevel="ERROR" maxLevel="ERROR" onMatch="ACCEPT" onMismatch="DENY"/>
			    <PatternLayout>
			        <Pattern>%d{yyyy-MM-dd HH:mm:ss} %-5p %m%nPattern>
			    PatternLayout>
			    <Policies>
			        <SizeBasedTriggeringPolicy size="1KB" />
			    Policies>
		    	<DefaultRolloverStrategy max="2" />
		RollingFile>
    Appenders>
    <Loggers>
        
        <Root level="ERROR">
            <AppenderRef ref="Console" />
            <AppenderRef ref="errorFile" />
            <AppenderRef ref="infoFile" />
        Root>
        <Logger name="org.sea" level="INFO" />
        <Logger name="org.springframework" level="INFO" />
        <Logger name="zaxxer.hikari" level="INFO" />
        <Logger name="org.thymeleaf" level="INFO" />
        
        <Logger name="com.ibatis" level="INFO"/>
        <Logger name="java.sql" level="INFO" />
    Loggers>
Configuration>

以上配置使用了RollingFile追加器,可以定义多个多个追加器,每个追加器定义不同的级别,在生产环境,我们希望能够按日志级别归类打印文件,同时指定文件的过期策略,防止磁盘被打满,RollingFile追加器能满足我们的需求,根据log4j2的架构,我们知道追加器内部可以添加过滤器、布局。LevelRangeFilter 过滤器能为指定打印日志级别。

<LevelRangeFilter minLevel="ERROR" maxLevel="ERROR" onMatch="ACCEPT" onMismatch="DENY"/>

提示:指定打印的日志级别为ERROR级别,匹配打印,不匹配拒绝打印

 <PatternLayout>
			 <Pattern>%d{yyyy-MM-dd HH:mm:ss} %-5p %m%nPattern>
 PatternLayout>

提示:指定日志输出格式 ,日志输出格式可以根据需要自定义,自定义需要结合MDC使用

<Policies>
	<SizeBasedTriggeringPolicy size="1KB" />
Policies>
<DefaultRolloverStrategy max="2" />

提示:追加器的策略,表示文件大小为1KB ,文件个数最大为2个,size和max都达到最大值的时候会自动刷新已经生成的日志,按照时间顺序自动刷新过期日志数据。

resources/application.yml配置

logging:
  config: classpath:log4j2.xml

java代码使用

package org.sea.spring.cloud.nacos.job;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.sea.spring.cloud.nacos.feign.EchoFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
public class  EchoFeignJob {

	Logger logger =  LogManager.getLogger(this.getClass());
	 
	@Autowired
	EchoFeignClient echoFeignClient;
	 
	 
	@Scheduled(fixedRate = 1000)
	public void  test() {
		String result =echoFeignClient.echo("zhangshan0001");
		logger.info("result:{}",result);
		logger.error("result is error");
	}
}

更多的log4j2的配置详情,请参阅 https://howtodoinjava.com/log4j2/

大数据时代日志数据很重要,因为我们的大部分数据都来源日志数据,而非业务数据。

如果觉得文章有帮助,关注下作者的公众号,赞个人气,不胜感激!
同时可以下载作者整理的工作10多年来阅读过的电子书籍。
公众号: TalkNewClass
Log4j 2.x 在Springboot应用中如何配置_第1张图片

你可能感兴趣的:(日志框架,log4j2)