SpringCloud整合log4j2总结

SpringCloud整合log4j2

  • 常用日志框架
  • 日志门面slf4j
  • 为什么要选用log4j2
  • 整合步骤

常用日志框架

  • java.util.logging:是JDK在1.4版本中引入的Java原生日志框架
  • Log4j:Apache的一个开源项目,可以控制日志信息输送的目的地是控制台、文件、GUI组件等,可以控制每一条日志的输出格式,这些可以通过一个配置文件来灵活地进行配置,而不需要修改应用的代码。虽然已经停止维护了,但目前绝大部分企业都是用的log4j。
  • LogBack:是Log4j的一个改良版本
  • Log4j2:Log4j2已经不仅仅是Log4j的一个升级版本了,它从头到尾都被重写了

日志门面slf4j

上述介绍的是一些日志框架的实现,这里我们需要用日志门面来解决系统与日志实现框架的耦合性。SLF4J,即简单日志门面(Simple Logging Facade for Java),它不是一个真正的日志实现,而是一个抽象层( abstraction layer),它允许你在后台使用任意一个日志实现。

SpringCloud整合log4j2总结_第1张图片
前面介绍的几种日志框架一样,每一种日志框架都有自己单独的API,要使用对应的框架就要使用其对应的API,这就大大的增加应用程序代码对于日志框架的耦合性。

使用了slf4j后,对于应用程序来说,无论底层的日志框架如何变,应用程序不需要修改任意一行代码,就可以直接上线了。

为什么要选用log4j2

相比与其他的日志系统,log4j2丢数据这种情况少;disruptor技术,在多线程环境下,性能高于logback等10倍以上;利用jdk1.5并发的特性,减少了死锁的发生;

贴上几张网上的日志框架性能测评图:
SpringCloud整合log4j2总结_第2张图片

  • 可以看到在同步日志模式下, Logback的性能是最糟糕的.
  • log4j2的性能无论在同步日志模式还是异步日志模式下都是最佳的.
    SpringCloud整合log4j2总结_第3张图片
    log4j2优越的性能其原因在于log4j2使用了LMAX,一个无锁的线程间通信库代替了,logback和log4j之前的队列. 并发性能大大提升。

整合步骤

  1. 引入jar包

springboot默认是用logback的日志框架的,所以需要排除logback,不然会出现jar依赖冲突的报错。

<dependency>
	<groupId>org.springframework.bootgroupId>
	<artifactId>spring-boot-starter-webartifactId>
	<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>

<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-actuatorartifactId>
	<exclusions>
		<exclusion>
			<groupId>org.springframework.bootgroupId>
			<artifactId>spring-boot-starter-loggingartifactId>
		exclusion>
	exclusions>
dependency>

  1. 配置文件
    如果自定义了文件名,需要在application.yml中配置
    默认名log4j2-spring.xml,就省下了在application.yml中配置
logging.config = classpath:log4j2-dev.xml
  1. 配置文件模板

log4j是通过一个.properties的文件作为主配置文件的,而现在的log4j2则已经弃用了这种方式,采用的是.xml,.json或者.jsn这种方式来做,可能这也是技术发展的一个必然性,因为properties文件的可阅读性真的是有点差。这里给出一个模版,供大家参考。





<configuration status="INFO" monitorInterval="30">
	
	<properties>
		
		
		
		<Property name="log_path">${sys:user.home}/logsProperty>

		
		<property name="KEEP_LOG_DAY">60Dproperty>
		
		<property name="EVERY_FILE_SIZE">5Mproperty>
	properties>
	
	<appenders>
		<console name="Console" target="SYSTEM_OUT">
			
			<PatternLayout  charset="UTF-8" pattern="[%d][%t][%p][%c:%L] %m%n"/>
			<ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY" />
		console>
		
		
		
		
		

		
		<RollingFile name="RollingFileInfo" fileName="${log_path}/info-latest.log" filePattern="${log_path}/$${date:yyyy-MM-dd}/info-%d{yyyy-MM-dd}-%i.log">
			
			<ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY" />
			
			
			
			
			
			<PatternLayout  charset="UTF-8" pattern="[%d][%t][%p][%c:%L] %m%n"/>
			<Policies>
				
				
				<TimeBasedTriggeringPolicy />
				
				<SizeBasedTriggeringPolicy size="${EVERY_FILE_SIZE}"/>
			Policies>
			 
			<DefaultRolloverStrategy  max="256">
				<Delete basePath="${log_path}/" maxDepth="3">
					<IfFileName glob="*/*info*.log"/>
					<IfLastModified age="${KEEP_LOG_DAY}"/>
				Delete>
			DefaultRolloverStrategy>
		RollingFile>


		<RollingFile name="RollingFileWarn" fileName="${log_path}/warn-latest.log" filePattern="${log_path}/$${date:yyyy-MM-dd}/warn-%d{yyyy-MM-dd}-%i.log">
			<ThresholdFilter level="warn" onMatch="ACCEPT" onMismatch="DENY" />
			
			<PatternLayout  charset="UTF-8" pattern="[%d][%t][%p][%c:%L] %m%n"/>
			<Policies>
				<TimeBasedTriggeringPolicy />
				<SizeBasedTriggeringPolicy size="${EVERY_FILE_SIZE}" />
			Policies>
			
			<DefaultRolloverStrategy  max="256">
				<Delete basePath="${log_path}/" maxDepth="3">
					<IfFileName glob="*/*warn*.log"/>
					<IfLastModified age="${KEEP_LOG_DAY}"/>
				Delete>
			DefaultRolloverStrategy>
		RollingFile>
		<RollingFile name="RollingFileError" fileName="${log_path}/error-latest.log" filePattern="${log_path}/$${date:yyyy-MM-dd}/error-%d{yyyy-MM-dd}-%i.log">
			<ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="DENY" />
			
			<PatternLayout  charset="UTF-8" pattern="[%d][%t][%p][%c:%L] %m%n"/>
			<Policies>
				<TimeBasedTriggeringPolicy />
				<SizeBasedTriggeringPolicy size="1${EVERY_FILE_SIZE}" />
			Policies>
			
			<DefaultRolloverStrategy  max="256">
				<Delete basePath="${log_path}/" maxDepth="3">
					<IfFileName glob="*/*error*.log"/>
					<IfLastModified age="180D"/>
				Delete>
			DefaultRolloverStrategy>
		RollingFile>
	appenders>
	
	<loggers>
		
		<logger name="org.springframework" level="INFO">logger>
		<logger name="org.mybatis" level="DEBUG">logger>
		
		<root level="ALL">
			<appender-ref ref="Console" />
			<appender-ref ref="RollingFileInfo" />
			<appender-ref ref="RollingFileWarn" />
			<appender-ref ref="RollingFileError" />
		root>
		<logger name="cn.timebusker.util" level="INFO">
			<appender-ref ref="RollingFileInfo" />
		logger>
	loggers>
configuration>

参考链接:Springboot整合log4j2日志全解

你可能感兴趣的:(微服务)