标签:style blog http color io ar sp 文件 div
一个多月前,我写了篇关于log4j.xml配置的文章,点击此处查看:http://www.cnblogs.com/guogangj/p/3931397.html
最近,我把自己的log4j升级到2.0之后发现之前的配置文件不能用了,于是把旧的配置挪到新的来,挪的过程中遇到了些问题,但一番搜索测试后基本上趋于完美了。老样子,先上代码:
<?xml version="1.0" encoding="UTF-8"?> <!--设置log4j2的自身log级别为warn--> <configuration status="warn"> <appenders> <console name="Console" target="SYSTEM_OUT"> <PatternLayout pattern="[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n"/> </console> <RollingFile name="RollingFileInfo" fileName="${sys:user.home}/logs/hpaasvc/info.log" filePattern="${sys:user.home}/logs/hpaasvc/$${date:yyyy-MM}/info-%d{yyyy-MM-dd}-%i.log"> <Filters> <ThresholdFilter level="INFO"/> <ThresholdFilter level="WARN" onMatch="DENY" onMismatch="NEUTRAL"/> </Filters> <PatternLayout pattern="[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n"/> <Policies> <TimeBasedTriggeringPolicy/> <SizeBasedTriggeringPolicy size="100 MB"/> </Policies> </RollingFile> <RollingFile name="RollingFileWarn" fileName="${sys:user.home}/logs/hpaasvc/warn.log" filePattern="${sys:user.home}/logs/hpaasvc/$${date:yyyy-MM}/warn-%d{yyyy-MM-dd}-%i.log"> <Filters> <ThresholdFilter level="WARN"/> <ThresholdFilter level="ERROR" onMatch="DENY" onMismatch="NEUTRAL"/> </Filters> <PatternLayout pattern="[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n"/> <Policies> <TimeBasedTriggeringPolicy/> <SizeBasedTriggeringPolicy size="100 MB"/> </Policies> </RollingFile> <RollingFile name="RollingFileError" fileName="${sys:user.home}/logs/hpaasvc/error.log" filePattern="${sys:user.home}/logs/hpaasvc/$${date:yyyy-MM}/error-%d{yyyy-MM-dd}-%i.log"> <ThresholdFilter level="ERROR"/> <PatternLayout pattern="[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n"/> <Policies> <TimeBasedTriggeringPolicy/> <SizeBasedTriggeringPolicy size="100 MB"/> </Policies> </RollingFile> </appenders> <loggers> <!--过滤掉spring和hibernate的一些无用的debug信息--> <logger name="org.springframework" level="INFO"> </logger> <logger name="org.hibernate" level="INFO"> </logger> <root level="all"> <appender-ref ref="Console"/> <appender-ref ref="RollingFileInfo"/> <appender-ref ref="RollingFileWarn"/> <appender-ref ref="RollingFileError"/> </root> </loggers> </configuration>
这个配置就能达成跟之前一样的功能,在试验的过程中发现log4j2比log4j强大了很多,不过这不在本文讨论之列。
从配置上看,和log4j 1.x类似,log4j2也有logger,appender等概念,下面重点说说几个差别:
生成的log文件大致如下:
标签:style blog http color io ar sp 文件 div
原文地址:http://www.cnblogs.com/guogangj/p/4031571.html