如何修复log4j2不记录到文件?

huangapple go评论64阅读模式
英文:

How to fix log4j2 not logging to file?

问题

I am fixing my old log4j properties file to change over to the new log4j2. However, I noticed that when I run my code, it creates a folder structure for the .log file but nothing is being logged. When I read the console, I saw an error that said:

Error:

log4j:WARN No appenders could be found for logger (org.springframework.web.context.ContextLoader).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

When I looked it up online, other people have said it is because the java class can't find the properties file. So I added the -Dlog4j.configuration="Z:/Projects/properties_file/log4j.properties" and the line PropertyConfigurator.configure("Z:/Projects/properties_file/log4j.properties"). Though even adding those lines still did not fix the issue.

Code:

status = TRACE
name= properties_configuration

rootLogger.level = debug,info
rootLogger.additivity = false
rootLogger.appenderRef.rolling.ref = fileLogger
rootLogger.appenderRef.console.ref = consoleLogger

logger.UserLogger.name = UserLogger
logger.UserLogger.level = debug
logger.UserLogger.additivity = false
logger.UserLogger.appenderRef.rolling.ref = fileLogger
logger.UserLogger.appenderRef.console.ref = consoleLogger

appender.console.layout.pattern = %d{dd-MM-yyyy HH:mm:ss.SSS}.-%t-%x-%-3p-%-5c:%m%n

appender.rolling.type = RollingFile
appender.rolling.name = fileLogger
appender.rolling.fileName= C:/projects/properties/logs/my_log.txt
appender.rolling.filePattern= C:/projects/properties/logs/my_log_%d{dd-MM-yyyy}.zip
appender.rolling.layout.type = PatternLayout
appender.rolling.layout.pattern = %d{dd-MM-yyyy HH:mm:ss.SSS}.-%t-%x-%-5p-%-10c:%m%n
appender.rolling.policies.type = Policies

appender.console.type = Console
appender.console.name = consoleLogger
appender.console.target = SYSTEM_OUT
appender.console.layout.type = PatternLayout
英文:

I am fixing my old log4j properties file to change over to the new log4j2. However, I noticed that when I run my code, it creates a folder structure for the .log file but nothing is being logged. When I read the console, I saw an error that said:

Error:

log4j:WARN No appenders could be found for logger (org.springframework.web.context.ContextLoader).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

When I looked it up online, other people have said it is because the java class can't find the properties file. So I added the -Dlog4j.configuration="Z:/Projects/properties_file/log4j.properties and the line PropertyConfigurator.configure("Z:/Projects/properties_file/log4j.properties"). Though even adding those lines still did not fix the issue.

Code:


status = TRACE
name= properties_configuration

rootLogger.level = debug,info
rootLogger.additivity = false
rootLogger.appenderRef.rolling.ref = fileLogger
rootLogger.appenderRef.console.ref = consoleLogger

logger.UserLogger.name = UserLogger
logger.UserLogger.level = debug
logger.UserLogger.additivity = false
logger.UserLogger.appenderRef.rolling.ref = fileLogger
logger.UserLogger.appenderRef.console.ref = consoleLogger

appender.console.layout.pattern = %d{dd-MM-yyyy HH:mm:ss.SSS}.-%t-%x-%-3p-%-5c:%m%n

appender.rolling.type = RollingFile
appender.rolling.name = fileLogger
appender.rolling.fileName= C:/projects/properties/logs/my_log.txt
appender.rolling.filePattern= C:/projects/properties/logs/my_log_%d{dd-MM-yyyy}.zip
appender.rolling.layout.type = PatternLayout
appender.rolling.layout.pattern = %d{dd-MM-yyyy HH:mm:ss.SSS}.-%t-%x-%-5p-%-10c:%m%n
appender.rolling.policies.type = Policies


appender.console.type = Console
appender.console.name = consoleLogger
appender.console.target = SYSTEM_OUT
appender.console.layout.type = PatternLayout
      

答案1

得分: 1

请仔细查看您收到的错误消息。

log4j:WARN 未找到适用于记录器(org.springframework.web.context.ContextLoader)的附加器。
log4j:WARN 请正确初始化log4j系统。
log4j:WARN 有关更多信息,请查看http://logging.apache.org/log4j/1.2/faq.html#noconfig。

请注意,它建议查看log4j 1.2常见问题解答。这意味着您的应用程序仍在使用Log4j 1,而不是Log4j 2。您需要确保log4j 1.x jar文件未包含在您的项目中。此外,log4j.configuration属性用于指定Log4j 1的配置文件,而不是Log4j 2。它使用log4j2.configurationFile。Log4j 2也不使用PropertyConfigurator.configure()来配置自身,而这种方法会与提供log4j2.configurationFile属性发生冲突。

英文:

Look closer at the error message you are getting.

log4j:WARN No appenders could be found for logger (org.springframework.web.context.ContextLoader).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

Notice it says to look at the log4j 1.2 faq. This means your application is still using Log4j 1, not Log4j 2. You need to make sure the log4j 1.x jar is not being included in your project. Also, the log4j.configuration property is how you specify the configuration file for Log4j 1, not Log4j 2. It uses log4j2.configurationFile. Log4j 2 also does not use PropertyConfigurator.configure() to configure itself and that method would conflict with you providing the log4j2.configurationFile property.

huangapple
  • 本文由 发表于 2020年8月11日 21:32:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/63359264.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定