Search code examples
javaeclipselogginglog4j

log File not generating in log4j


I am new to log4j. I have created a sample java program implementing log4j in it.

Below is the java program:

package logging;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;
public class Logging {
    /**
     * @param args the command line arguments
     */
    private static Logger logger = Logger.getLogger(Logging.class);
    public static void main(String[] args) {
        BasicConfigurator.configure();

         logger.trace("This is a Trace");
         logger.debug("This is a Debug");
         logger.info("This is an Info");
         logger.warn("This is a Warn");
         logger.error("This is an Error");
         logger.fatal("This is a Fatal");

    }

}

I am getting the output in the console screen.But the log file is not getting generated. I have also configured my project in the Eclipse neon using the following link: Configuration

I have done everything good. But the log file is not generating.When I implement log4j programmatically the file is getting generated.The following is my properties file:

#root

log4j.logger.com.apress.logging.log4j=debug,dest

log4j.additivity.com.apress.logging.log4j=false

#define the appender
log4j.appender.dest = org.apache.log4j.DailyRollingFileAppender

#set the name of the file 
log4j.appender.dest.File=${user.home}/log.out

#setting the immediate flush to true (default) 
log4j.appender.dest.ImmediateFlush=true

#setting the threshold
log4j.appender.dest.Threshold=ERROR

#setting the append to false, overwrite 
log4j.appender.dest.Append=true

#set the DatePattern 
log4j.appender.dest.DatePattern='.' yyyy-MM-dd

What do I need to do to have Log4J write to the log file?


Solution

  • Ensure log4j.properties is in default package

    # Root logger option
        log4j.rootLogger=DEBUG, stdout, file
        log4j.logger.infoLogger=DEBUG, infoLogger
    
        log4j.additivity.infoLogger = false
        # Redirect 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
    
        # Redirect log messages to a log file, support file rolling.
        log4j.appender.file=org.apache.log4j.RollingFileAppender
        log4j.appender.file.File=E:\\LOG\\ConvertorLogger.log
        log4j.appender.file.MaxFileSize=5MB
        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
    
        # Redirect log messages to a log file, support file rolling.
        log4j.appender.infoLogger=org.apache.log4j.RollingFileAppender
        log4j.appender.infoLogger.File=E:\\LOG\\ConvertorInfoLogger.log
        log4j.appender.infoLogger.MaxFileSize=5MB
        log4j.appender.infoLogger.MaxBackupIndex=10
        log4j.appender.infoLogger.layout=org.apache.log4j.PatternLayout
        log4j.appender.infoLogger.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n