Reputation: 263
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
Upvotes: 1
Views: 1311
Reputation: 9151
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.
Upvotes: 1