Log4j2使用

Log4j 2 maven依赖jar,Log4j 2 从2.4版本开始使用JDK7编译

<dependency>
			<groupId>org.apache.logging.log4j</groupId>
			<artifactId>log4j-api</artifactId>
			<version>2.3</version>
		</dependency>
		<dependency>
			<groupId>org.apache.logging.log4j</groupId>
			<artifactId>log4j-core</artifactId>
			<version>2.3</version>
		</dependency>

		<!--使用slf4j封装日志API,如果不使用可以不用引用,log4j2最低要求1.6版本-->
        <dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>1.7.5</version>
		</dependency>
        <dependency>
			<groupId>org.apache.logging.log4j</groupId>
			<artifactId>log4j-slf4j-impl</artifactId>
			<version>2.3</version>
		</dependency>
        <!--异步日志,不使用异步可以不引用-->
		<dependency>
			<groupId>com.lmax</groupId>
			<artifactId>disruptor</artifactId>
			<version>3.0.0</version>
		</dependency>

log4j2.xml配置:

<?xml version="1.0" encoding="UTF-8"?>
<configuration status="error">

	<Properties>
		<Property name="logFilePath">D:\\logs</Property>
		<!-- 一小时一个日志文件 -->
		<Property name="interval">1</Property>
		<!-- 业务日志输出格式 -->
		<Property name="selfPattern">%m%n</Property>
		<!-- 其中%t参数表示当前线程ID -->
		<Property name="ConversionPattern">[oi-service-${sys:dubbo.protocol.port}]%d[%t][%p]
			- %m - [%l]%n</Property>
	</Properties>
	<appenders>
		<!-- 输出到控制台 -->
		<Console name="Console" target="SYSTEM_OUT">
			<ThresholdFilter level="info" onMatch="ACCEPT" />
			<PatternLayout pattern="[oi]%d [%p] - %m [%l] %n" />
		</Console>

		<RollingFile name="debug"
			fileName="${logFilePath}/debug-${sys:dubbo.protocol.port}.log"
			filePattern="${logFilePath}/debug-${sys:dubbo.protocol.port}.log.%d{yyyy-MM-dd-HH}">
			<ThresholdFilter level="debug" onMatch="ACCEPT"
				onMismatch="DENY" />
			<PatternLayout pattern="${ConversionPattern}" />
			<TimeBasedTriggeringPolicy interval="1" />
		</RollingFile>


		<RollingFile name="info"
			fileName="${logFilePath}/info-${sys:dubbo.protocol.port}.log"
			filePattern="${logFilePath}/info-${sys:dubbo.protocol.port}.log.%d{yyyy-MM-dd-HH}">
			<ThresholdFilter level="info" onMatch="ACCEPT"
				onMismatch="DENY" />
			<PatternLayout pattern="${ConversionPattern}" />
			<TimeBasedTriggeringPolicy interval="${interval}" />
		</RollingFile>

		<RollingFile name="warn"
			fileName="${logFilePath}/warn-${sys:dubbo.protocol.port}.log"
			filePattern="${logFilePath}/warn.log-${sys:dubbo.protocol.port}.%d{yyyy-MM-dd-HH}">
			<ThresholdFilter level="warn" onMatch="ACCEPT"
				onMismatch="DENY" />
			<PatternLayout pattern="${ConversionPattern}" />
			<TimeBasedTriggeringPolicy interval="${interval}" />
		</RollingFile>

		<RollingFile name="error"
			fileName="${logFilePath}/error-${sys:dubbo.protocol.port}.log"
			filePattern="${logFilePath}/error-${sys:dubbo.protocol.port}.log.%d{yyyy-MM-dd-HH}">
			<ThresholdFilter level="error" onMatch="ACCEPT"
				onMismatch="DENY" />
			<PatternLayout pattern="${ConversionPattern}" />
			<TimeBasedTriggeringPolicy interval="${interval}" />
		</RollingFile>



		<RollingFile name="inf"
			fileName="${logFilePath}/inf-${sys:dubbo.protocol.port}.log"
			filePattern="${logFilePath}/inf-${sys:dubbo.protocol.port}.log.%d{yyyy-MM-dd-HH}">
			<ThresholdFilter level="info" onMatch="ACCEPT"
				onMismatch="DENY" />
			<PatternLayout pattern="${selfPattern}" />
			<TimeBasedTriggeringPolicy interval="${interval}" />
		</RollingFile>

		<RollingFile name="query"
			fileName="${logFilePath}/query-${sys:dubbo.protocol.port}.log"
			filePattern="${logFilePath}/query-${sys:dubbo.protocol.port}.log.%d{yyyy-MM-dd-HH}">
			<ThresholdFilter level="info" onMatch="ACCEPT"
				onMismatch="DENY" />
			<PatternLayout pattern="${selfPattern}" />
			<TimeBasedTriggeringPolicy interval="${interval}" />
		</RollingFile>
		<Async name="Async">
			<AppenderRef ref="Console" />
		</Async>
	</appenders>

	<loggers>
		<!--自定义日志输出, 使用异步日志,log4j2建议业务日志使用同步,程序猿使用的日志使用异步 -->
		<asyncLogger name="inf" level="info" additivity="false">
			<appender-ref ref="inf" />
		</asyncLogger>

		<logger name="query" level="info" additivity="false">
			<appender-ref ref="query" />
		</logger>

		<logger name="org" level="error" additivity="false">
		</logger>
		<logger name="com.opensymphony.xwork2" level="off" additivity="false">
		</logger>
		<logger name="com.alibaba.dubbo" level="error" additivity="false">
		</logger>
		<asyncRoot level="trace" includeLocation="true">
			<appender-ref ref="info" />
			<appender-ref ref="error" />
			<appender-ref ref="warn" />
			<appenderRef ref="Console" level="error" />
		</asyncRoot>
	</loggers>
</configuration>

 log4j2异步日志和log4j1性能比较,使用ab,并发10000,100000次请求:

log4j2

 Log4j2使用

log4j1

 Log4j2使用

其中log4j2的性能是log4j1的10倍。

相关推荐