简体   繁体   English

如何正确配置 Log4J?

[英]How do I configure Log4J properly?

I'm having trouble using Log4J (version 2.13.0), with the following problems:我在使用 Log4J(版本 2.13.0)时遇到问题,有以下问题:

  • I have no idea if my log4j.properties file is configured properly, or even recognized for that matter.我不知道我的log4j.properties文件是否配置正确,甚至不知道是否因此被识别。
  • Log4J is not using the pattern I've specified nor allowing me to log any trace/debug/info/warn messages. Log4J 没有使用我指定的模式,也没有允许我记录任何跟踪/调试/信息/警告消息。

This is my log4j.properties file:这是我的log4j.properties文件:

status = trace
dest = out
name = PropertyConfiguration

property.filename = logs/log%d{yyyy-MM-dd HH:mm:ss}.log
property.pattern = %d{yyyy-MM-dd HH:mm:ss} | [%-5p] %c{1}:%L: %m%n

appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = ${pattern}

appender.rolling.type = RollingFile
appender.rolling.name = RollingFile
appender.fileName = ${filename}
appender.rolling.filePattern = logs/log-%d{MM-dd-yyyy-HH-mm-ss}.log
appender.rolling.layout.type = PatternLayout
appender.rolling.layout.pattern = ${pattern}
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.strategy.type = DefaultRolloverStrategy
appender.rolling.strategy.max = 5

logger.rolling.name = me.tecc
logger.rolling.level = trace
logger.rolling.additivity = false
logger.rolling.appenderRef.rolling.ref = RollingFile

rootLogger.level = trace
rootLogger.appenderRef.stdout.ref = STDOUT

Any help would be appreciated.任何帮助,将不胜感激。 Thanks!谢谢!

Log4J is not using the pattern I've specified nor allowing me to log any trace/debug/info/warn messages. Log4J 没有使用我指定的模式,也没有允许我记录任何跟踪/调试/信息/警告消息。

Unlike its predecessor Log4J 1.x, Log4J 2 did not support configuration through the properties file when it was initially released.与其前身 Log4J 1.x 不同,Log4J 2 在最初发布时不支持通过属性文件进行配置。 It was from Log4J 2.4 that support for the properties file was again added, but with a completely different syntax Log4J 2.4再次添加了对属性文件的支持,但使用了完全不同的语法

I see that your Log4j version is 2.13 which falls below 2.4 , so it wont work.我看到你的 Log4j 版本是 2.13 低于 2.4 ,所以它不会工作。

Kindly pay attention:请注意:

The PropertyConfigurator (log4j.properties) does not handle the advanced configuration features supported by the DOMConfigurator (log4j.xml) such as support custom ErrorHandlers, nested appenders such as the AsyncAppender, etc. PropertyConfigurator (log4j.properties) 不处理DOMConfigurator (log4j.xml) 支持的高级配置功能,例如支持自定义 ErrorHandlers,嵌套 appender,例如 AsyncAppender 等。

appender.fileName = ${filename} appender.fileName = ${filename}

you miss the "rolling" part in this config.你错过了这个配置中的“滚动”部分。 try again after change it as follows更改后重试如下

appender.rolling.fileName = ${filename}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM