log4j升级到log4j2
1、导入依赖
log4j2应尽量使用同一版本,否则可能出现不兼容的情况
<!-- log4j2 start --> <!-- log4j-1.2-api必须放在最前面,否则出错 --> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-1.2-api</artifactId> <version>2.9.1</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-web</artifactId> <version>2.9.1</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core --> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.9.1</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-api --> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> <version>2.9.1</version> </dependency> <!-- log4j2 end -->
2、log4j.properties升级为log4j2.properties
log4j2支持log4j2.properties配置文件
status = warn
name = MyApp
#指定输出源‘类型’为控制台
appender.console.type = Console
appender.console.name = consoleLogDemo
appender.console.filter.threshold.type = ThresholdFilter
appender.console.filter.threshold.level = debug
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = [%-5p] [%d{yyyy-MM-dd HH:mm:ss}] [%C{1}:%M:%L] %m%n
appender.console.target = System_out
appender.rolling.type = RollingFile
appender.rolling.name = fileLogDemo
appender.rolling.filter.threshold.type = ThresholdFilter
appender.rolling.filter.threshold.level = info
appender.rolling.layout.type = PatternLayout
appender.rolling.layout.pattern = [%-5p] [%d{yyyy-MM-dd HH:mm:ss}] [%C{1}:%M:%L] %m%n
appender.rolling.append = true
appender.rolling.fileName =/data/logs/workflow/web.log
appender.rolling.filePattern=/data/logs/wokflow/web.log.%d{yyyy-MM-dd}
appender.rolling.policies.type = Policies
appender.rolling.policies.time.type = TimeBasedTriggeringPolicy
appender.rolling.policies.time.interval = 2
appender.rolling.policies.time.modulate = true
appender.rolling.policies.size.type = SizeBasedTriggeringPolicy
appender.rolling.policies.size.size=100MB
appender.rolling.strategy.type = DefaultRolloverStrategy
appender.rolling.strategy.max = 5
logger.rolling.name = com.iqiyi
logger.rolling.level = info
logger.rolling.additivity = false
logger.rolling.appenderRef.rolling.ref = fileLogDemo
#指明根级日志的级别
rootLogger.level = info
#指定哪些appender输出源是根级日志的输出级别
rootLogger.appenderRef.consolelogdemo.ref = consoleLogDemo
rootLogger.appenderRef.filelogdemo.ref = fileLogDemo
参考文档:
[log4j2中的properties配置文件] http://www.360doc.com/content/18/0628/10/339365_766029208.shtml
[log4j 2.x 版本的 properties 配置] https://www.cnblogs.com/chensuqian/p/10419218.html
[升级log4j 2.x版本 缓存异步化配置解读] https://www.iteye.com/blog/zl378837964-2359382
[旧web项目log4j日志升级至log4j2【从servlet2.x版本升级】] https://blog.csdn.net/leixingbang1989/article/details/86518061
3、log4j2.xml
<?xml version="1.0" encoding="UTF-8"?> <configuration status="error" monitorInterval="30"> <Properties> <Property name="baseDir">log</Property> </Properties> <!--先定义所有的appender--> <appenders> <!--这个输出控制台的配置--> <Console name="Console" target="SYSTEM_OUT"> <!--控制台只输出level及以上级别的信息(onMatch),其他的直接拒绝(onMismatch)--> <ThresholdFilter level="trace" onMatch="ACCEPT" onMismatch="DENY"/> <PatternLayout pattern="%d %5p %c:%L - %m %throwable{}%n"/> </Console> <!--异步日志配置,指向配置引用AppenderRef--> <Async name="ASYNC" bufferSize="262144" includeLocation="true"> <AppenderRef ref="RollingFile"/> </Async> <!--日志文件配置,filePattern为日志文件名称的格式--> <RollingFile name="RollingFile" fileName="${baseDir}/info.log" filePattern="${baseDir}/info.log.%d{yyyy-MM-dd}"> <!--日志内容格式--> <PatternLayout pattern="%d %5p %c:%L - %m %throwable{separator( --> )}%n"/> <!--依据时间创建新的日志文件:1d--> <TimeBasedTriggeringPolicy interval="1"/> <DefaultRolloverStrategy> <!-- 在轮转时,删除7天之前的,命名符合规则的文件 --> <Delete basePath="${baseDir}"> <IfFileName glob="info.log.*"/> <IfLastModified age="7d"/> </Delete> </DefaultRolloverStrategy> </RollingFile> </appenders> <!--然后定义logger,只有定义了logger并引入的appender,appender才会生效--> <loggers> <root level="INFO"> <appender-ref ref="Console"/> <appender-ref ref="RollingFile"/> </root> <!--这里配置 过滤日志 --> <logger name="org.hibernate.validator" level="ERROR"/> </loggers> </configuration>
4、修改web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <!--日志升级开始--> <listener> <listener-class>org.apache.logging.log4j.web.Log4jServletContextListener</listener-class> </listener> <filter> <filter-name>log4jServletFilter</filter-name> <filter-class>org.apache.logging.log4j.web.Log4jServletFilter</filter-class> </filter> <filter-mapping> <filter-name>log4jServletFilter</filter-name> <url-pattern>/*</url-pattern> <dispatcher>REQUEST</dispatcher> <dispatcher>FORWARD</dispatcher> <dispatcher>INCLUDE</dispatcher> <dispatcher>ERROR</dispatcher> </filter-mapping> <context-param> <param-name>log4jContextName</param-name> <param-value>myApplication</param-value> </context-param> <context-param> <param-name>log4jConfiguration</param-name> <param-value>classpath:log4j2.properties</param-value> </context-param> <!--日志升级结束-->
在web.xml中移除旧的log4j.property的配置
<context-param> <param-name>log4jConfigLocation</param-name> <param-value>classpath:log4j.properties</param-value> </context-param>
5、log4j2测试
使用的LogManager.getLogger(Object.class)方式获取对象
加入测试类,并运行。如在项目磁盘目录下正常输出日志内容则说明log4j2添加成功:
public class TestLogger { private static final Logger log = LoggerFactory.getLogger(TestLogger.class); public static void main(String[] args) { int number=10; log.info("AAAAAAAAAAInfo : number is " + number); log.warn("AAAAAAAAAAAAAAAWarning : number is " + number); log.debug("AAAAAAAAADebug : number is " + number); log.error("AAAAAAAAAAAError : number is " + number); } }
Windy心梦无痕