I am trying to run a standalone java program using log4j, but receiving below while debugging (with no log4j related logs on console):
log= {Logger@1343} "java.lang.Class:ERROR in 18b4aac2"
Can someone please suggest what is wrong here?
The code is as below:
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.core.Logger;
import java.io.IOException;
import java.sql.SQLException;
public class log4jExample {
static org.apache.logging.log4j.Logger log = LogManager.getLogger(log4jExample.class.getClass());
public static void main(String[] args)throws IOException,SQLException {
System.out.println("in main...");
log.debug("Hello this is a debug message");
System.out.println("in main...2..");
log.info("Hello this is an info message");
}
}
And the log4j.properties file is as below which is kept at src/main/resources.
log4j.rootLogger=DEBUG, stdout, file
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
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=C:\\test\\log4j-example.log
log4j.appender.file.MaxFileSize=1MB
log4j.appender.file.MaxBackupIndex=2
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
I am running my java program with VM option
-Dlog4j.configurationFile=C:\MyFirstProject\src\main\resources\log4j.properties
It is not an error !
It just says that your logger level is set to ERROR, so you will not see the messages logged to DEBUG/INFO levels.
Maybe you should check if your configuration is compatible with Log4J-2.x:
https://logging.apache.org/log4j/2.x/manual/configuration.html#Properties https://logging.apache.org/log4j/2.x/manual/appenders.html#ConsoleAppender
Use XML format is usually better.
It seems to me like you have mixed up log4j versions. The configuration file you use is using log4j 1.x format. You have to convert it to log4j 2.x format. I tried your example and it works. See details below.
pom.xml file
Re-factored code
log4j.properties file (in log4j2.x format)
Output:
Refer the log4j docs.