0

I am trying to implement Log4J API in my java project. To do that, I have created a properties as shown in the image below (highlighted in yellow): Project Structure Image

These are the properties I set in the file:

# TRACE < DEBUG < INFO < WARN < FATAL
log4j.rootLogger=TRACE, DEBUG, INFO, file

# Console
# log4j.appender.toConsole=org.apache.log4j.ConsoleAppender
# log4j.appender.toConsole.layout=org.apache.log4j.PatternLayout
# log4j.appender.toConsole.layout.ConversionPatter=%d{HH:mm:ss} %5p [%t] - $c.%M - %m%n

# Redirecting log messages to 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

I declared the LOGGER object in my class as following:

import org.apache.log4j.Level;
import org.apache.log4j.Logger;
public class Credits{

    Logger LOGGER = null;
    public Credits(String sources) {
        LOGGER = Logger.getLogger(ChunkRecon.class.getName());
        LOGGER.setLevel(Level.DEBUG);
        String  creditNames = "select tablename, creditNumbers, credit_type from schema.table where credit_type in ("sources")";
        LOGGER.debug("This is a debug message");
        system.out.println("This message is from println");
    }
}

In the output, I see the message from sysout: This message is from println but not the debug message.

Could anyone let me know what is the mistake I am doing here ?

Torque
  • 99
  • 3
  • 16

2 Answers2

0

Try to rename your loggerproperties to log4j.properties and check it is in classpath. Another problem with rootLogger, see explanation here

Also Logger is usually used as static variable. For example:

public class Credits {

    private static final Logger logger = Logger.getLogger(Credits.class);

    public Credits(String sources) {
        logger.setLevel(Level.DEBUG);
        logger.debug("This is a debug message");

        System.out.println("This message is from println");
    }
}

log4j.properties

log4j.rootLogger=debug, stdout

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
ilinykhma
  • 980
  • 1
  • 8
  • 14
  • If I make it static or private static, the compiler says: Illegal modifier for parameter LOGGER; only final is permitted. Also changing file name wouldn't have any impact. – Torque Feb 11 '19 at 11:21
0

Your log4j.rootLogger declaration seems incorrect. Try as -

log4j.rootLogger=TRACE,stdout,file
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.file=org.apache.log4j.RollingFileAppender

If you only want the logs on console then remove file logging fully.

log4j.rootLogger=TRACE,stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender

Refer section Configuring loggers from JavaDoc.
The syntax for configuring the root logger is:

log4j.rootLogger=[level], appenderName, appenderName, ...
Haripriya
  • 822
  • 1
  • 14
  • 27