psg
psg

Reputation: 141

java.lang.Class:ERROR while initializing log4j properties file in java program

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

Upvotes: 2

Views: 2993

Answers (2)

Rajind Ruparathna
Rajind Ruparathna

Reputation: 2255

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

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.bft.</groupId>
    <artifactId>test</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.7</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.7</version>
        </dependency>
    </dependencies>
</project>

Re-factored code

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class log4jExample {

    static final Logger logger = LogManager.getLogger(log4jExample.class);

    public static void main(String[] args) {
        System.out.println("in main...");

        logger.info("Hello this is a debug message");
        System.out.println("in main...2..");
        logger.info("Hello this is an info message");
    }
}

log4j.properties file (in log4j2.x format)

status = error
dest = err
name = PropertiesConfig

filter.threshold.type = ThresholdFilter
filter.threshold.level = debug

appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = %m%n

rootLogger.level = info
rootLogger.appenderRef.stdout.ref = STDOUT

Output:

in main...
Hello this is a debug message
in main...2..
Hello this is an info message

Process finished with exit code 0

Refer the log4j docs.

Upvotes: 3

Donatello
Donatello

Reputation: 3745

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.

Upvotes: 1

Related Questions