Reputation: 14988
I put a dependency of log4j in pom.xml file:
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.10.0</version>
</dependency>
wrote the following log4j.properties file:
log4j.rootLogger=INFO, stdout, console log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target=System.out log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
and put this file under 'resources' folder.
Lastly, I put environment variable LOG4J_log4j.configurationFile = log4j.properties
Still, when I run my application, the line:
logger.info("Hello");
does not write anything to console. Do you know what can be the problem?
Upvotes: 4
Views: 20299
Reputation: 7938
You are using log4j2, your properties file name should be log4j2.properties
. And you can configure it like below. You don't have to put any enviromental variable.
appenders = console
appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = [%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
rootLogger.level = debug
rootLogger.appenderRefs = stdout
rootLogger.appenderRef.stdout.ref = STDOUT
source = https://springframework.guru/log4j-2-configuration-using-properties-file/
Upvotes: 11
Reputation: 99
I suggest that you start with a working example first, then add the specifics of your own configuration step by step. Then you see where it breaks.
In your above code I see the following potential issues:
-Dlog4j.configuration=file:log4j.properties
when starting the JVM.Upvotes: 1