使⽤log4j2分离系统⽇志与业务⽇志
  前⼀篇⽂章介绍了,现在介绍⼀下通过⽇志级别将系统⽇志与业务⽇志分类,要达到这个⽬的很容易,只需要配置⼀下log4j的xml⽂件:
<?xml version="1.0" encoding="UTF-8"?>
<configuration status="OFF">
<Properties>
<property name="log_pattern">%m%n</property>
<property name="basePath">/data/logs/</property>
<property name="system-msg">${basePath}/system.log</property>
<property name="business-a-msg">${basePath}/service_a.log</property>
<property name="business-b-msg">${basePath}/service_b.log</property>
<property name="rollingfile_system-msg">${basePath}/system%d{yyyy-MM-dd}.log</property>
<property name="rollingfile_business-a-msg">${basePath}/service_a%d{yyyy-MM-dd}.log</property>
<property name="rollingfile_business-b-msg">${basePath}/service_b%d{yyyy-MM-dd}.log</property>
</Properties>
<appenders>
<Console name="console" target="SYSTEM_OUT">
<ThresholdFilter level="debug" onMatch="ACCEPT" onMismatch="ACCEPT"/>
<PatternLayout pattern="${log_pattern}"/>
</Console>
<RollingFile name="appender_system-msg" filename="${system-msg}" filePattern="${rollingfile_system-msg}" append="true">
<Filters>
<ThresholdFilter level="business" onMatch="NEUTRAL" onMismatch="ACCEPT"/>
</Filters>
<PatternLayout>
<Pattern>%d{yyyy-MM-dd HH:mm:ss} %-5level %class{36} %L %M - %msg%xEx%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy />
</Policies>
</RollingFile>
<RollingFile name="appender_business-a-msg" filename="${business-a-msg}" filePattern="${rollingfile_business-a-msg}" append="true"> <ThresholdFilter level="business" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout>
<Pattern>${log_pattern}</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy />
</Policies>
</RollingFile>
<RollingFile name="appender_business-b-msg" filename="${business-b-msg}" filePattern="${rollingfile_business-b-msg}" append="true"> <ThresholdFilter level="business" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout>log4j2不打印日志
<Pattern>${log_pattern}</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy />
</Policies>
</RollingFile>
</appenders>
<loggers>
<AsyncLogger name="com.jessezeng" level="info" additivity="true"  includeLocation="true">
<AppenderRef ref="appender_system-msg"/>
</AsyncLogger>
<AsyncLogger name="com.jessezeng.service.a.ServiceAImpl" level="info" additivity="true"  includeLocation="true">
<AppenderRef ref="appender_business-a-msg"/>
</AsyncLogger>
<AsyncLogger name="com.jessezeng.service.b.ServiceBImpl" level="info" additivity="true"  includeLocation="true">
<AppenderRef ref="appender_business-b-msg"/>
</AsyncLogger>
<Root level="info">
<AppenderRef ref="console"/>
</Root>
</loggers>
</configuration>
通过上⾯的配置就可以达到我们想要的⽬的,主要⽤到了log4j2⽇志级别过滤,可参考另⼀篇⽂章。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。