Jagadeesh
Jagadeesh

Reputation: 852

Log4j Logs are not coming in the pattern I have specified in log4j.properties

I am trying to log messages to console using Log4j.

I have a class like below

public class Demo {
private static Employee employee=null;
private static Logger logger=Logger.getRootLogger();

public static void main(String[] args) {
    PropertyConfigurator.configure("log4j.properties");
    if(employee!=null){
        System.out.println(employee);
    }else{
        logger.warn("Employee object is null in Demo class main method in if(){} statement");
    }
}
}

I have log4j.properties file in the Project folder. If I keep log4j.properties file in classpath I am getting file not found exception , so I kept it in Project folder.My project in eclipse is like below

enter image description here

log4j.properties file is as below

log4j.rootLogger=DEBUG, lntLogger
log4j.appender.lntLogger=org.apache.log4j.ConsoleAppender
log4j.appender.lntLogger.layout=org.apache.log4j.PatternLayout
log4j.appender.CA.layout.ConversionPattern=%-4r [%t] %-5p %c - %m  --date %d{dd MMM yyyy} %n

Now I am expecting output in the format %-4r [%t] %-5p %c - %m --date %d{dd MMM yyyy} %n

But I am getting Only my message in the console like

Employee object is null in Demo class main method in if(){} statement

Now my question is why my log is not coming in the format what I have specified in properties file.

Upvotes: 1

Views: 156

Answers (1)

user3741056
user3741056

Reputation:

In log4j.properties file change 4th line to

log4j.appender.lntLogger.layout.ConversionPattern=%-4r [%t] %-5p %c - %m  --date %d{dd MMM yyyy} %n

Upvotes: 1

Related Questions