Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Log4j doesn't print to console

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?

like image 431
CrazySynthax Avatar asked Sep 08 '25 14:09

CrazySynthax


1 Answers

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/

like image 186
miskender Avatar answered Sep 10 '25 06:09

miskender