Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

log4j appender issue - unable to print debug, error levels

Tags:

java

log4j

I am using a log4j for logging.

This is how my log4j.properties looks like

# Root logger option
log4j.rootLogger=info, debug, error file


# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender


#Redirect to Tomcat logs folder
#log4j.appender.file.File=${catalina.home}/logs/logging.log

log4j.appender.file.File=C:\\Users\\raj_sanpui\\Desktop\\Automation\\test.log
log4j.appender.file.MaxFileSize=10MB
log4j.appender.file.MaxBackupIndex=10
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

Each of my Java files constructor has this call:

public class IMGOperations {

    private org.apache.log4j.Logger log;
    private String hostname;
    private String sysid;
    private String dicomfilepath;

    public IMGOperations(String hostname, String sysid, String dicomfilepath)
    {
        this.hostname=hostname;
        this.sysid=sysid;
        this.dicomfilepath=dicomfilepath;

        PropertyConfigurator.configure(mainConfig.LOG4JCONFPATH);
        log = Logger.getLogger(mainConfig.class);
    }

I am getting this error on running my Java program:

log4j:ERROR Could not find value for key log4j.appender.debug
log4j:ERROR Could not instantiate appender named "debug".
log4j:ERROR Could not find value for key log4j.appender.error file
log4j:ERROR Could not instantiate appender named "error file".
log4j:ERROR Could not find value for key log4j.appender.debug

I am basically a C/C++ folk, who knows Core Java, and pretty much a noob in this stuff. So please pardon me, if you find it too basic.

like image 222
RajSanpui Avatar asked Jan 10 '23 02:01

RajSanpui


2 Answers

The problem is in your second line, it should be:

# Root logger option
log4j.rootLogger=INFO, stdout, file

This means you will log at INFO level, and instantiate log4j appenders for stdout and files. Needless to say, INFO can be switched for any other logging level (TRACE, DEBUG, INFO, WARN, ERROR, FATAL).

EDIT:

The log4j.rootLogger takes 2 or more arguments. The first argument, is the logging level, and any following are the names of log4j appenders. You are seeing the error you have presented in your question because there are no appenders by the names of "debug" or "error file", as described by the line:

log4j:ERROR Could not find value for key log4j.appender.debug
log4j:ERROR Could not find value for key log4j.appender.error file

EDIT 2:

Given the below config file:

# Root logger option
log4j.rootLogger=info, debug, error file


# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender


#Redirect to Tomcat logs folder
#log4j.appender.file.File=${catalina.home}/logs/logging.log

log4j.appender.file.File=C:\\Users\\raj_sanpui\\Desktop\\Automation\\test.log
log4j.appender.file.MaxFileSize=10MB
log4j.appender.file.MaxBackupIndex=10
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

Log4j expects appenders to be defined for "debug" and "error file", however, you have only defined an appender for "file". You have defined your appender like so:

log4j.rootLogger=info, file
 log4j.appender.file=org.apache.log4j.RollingFileAppender

To use an appender named "debug", you would need another similar line:

log4j.rootLogger=info, debug
log4j.appender.debug=org.apache.log4j.RollingFileAppender

There should be a one to one mapping between the appenders you declare in the log4j.rootLogger property, and your appender definitions.

It might help if you think of each of the properties like this:

log4j.appender.file => create new file appender object
log4j.appender.file.File => set the file property of the file appender
log4j.appender.file.MaxFileSize => set the max file size of the file appender
log4j.appender.file.MaxBackupIndex => set the max backup index property of the file appender
log4j.appender.file.layout => set the layout of the file appender
etc...
like image 182
ConMan Avatar answered Jan 18 '23 21:01

ConMan


You having problem in the rootLogger, you should have only one logging level e.g DEBUG, INFO, ERROR etc.. in your case you having multiple logging levels and second thing you didn't declare file appender in your rootLogger but you used it in rest of the snippets.

# Root logger option log4j.rootLogger=info, debug, error file

The above snippet should be like this log4j.rootLogger= INFO, file

Have a look at this link

like image 24
Sivakumar Avatar answered Jan 18 '23 19:01

Sivakumar