Log file is created but File is empty

12,351

Solution 1

Replace

log4j.rootLogger=off

with

log4j.rootLogger=DEBUG,stdout,Rollfile

Solution 2

There was no problem in your earlier code but the newer code is better as your code is not tied to log4j API.

Are you sure your application is loading this log file?

The rollfile will get log messages only from classes under package com.gridsense.server.automode. Are you sure there are some log messages executing?

Typically I have always used

log4j.rootLogger=DEBUG, stdout,Rollfile

instead of below so not sure if it has any conflicts.

log4j.logger.org.apache.axis.enterprise=DEBUG, stdout,Rollfile
log4j.rootLogger=off
Share:
12,351
prachi
Author by

prachi

Updated on June 04, 2022

Comments

  • prachi
    prachi almost 2 years

    I am using log4j.properties file for generating logs in my java project. I want only custom log statements and log file corresponding to that statements. My log file is like this:

    log4j.logger.org.apache.axis.enterprise=DEBUG, stdout,Rollfile
    log4j.rootLogger=off
    log4j.logger.com.gridsense.server.automode=Rollfile,stdout
    log4j.appender.Rollfile=org.apache.log4j.RollingFileAppender
    log4j.appender.Rollfile.Threshold=DEBUG
    log4j.appender.Rollfile.File=D:/javaProjects/AutomodeGS_Prachi/AutoGS.log
    log4j.appender.Rollfile.MaxFileSize=2MB
    log4j.appender.Rollfile.layout=org.apache.log4j.PatternLayout
    log4j.appender.Rollfile.layout.ConversionPattern=[%t] %-5p %c %d{dd/MM/yyyy HH:mm:ss}  – %m%n
    
    log4j.appender.stdout=org.apache.log4j.ConsoleAppender
    log4j.appender.stdout.Threshold=DEBUG
    log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
    log4j.appender.stdout.layout.ConversionPattern=%-4r [%t] %-5p %c %x – %m%n
    

    But when i run the program the log statements are printed on console and empty log file is created. I don't understand what is problem.Why my log file is empty? In my class i have written the statement

    private static  Logger logger = Logger.getLogger(Driver.class);
    

    I am importing

    import org.apache.log4j.Logger;
    

    Actually I read your solution given to a question related to "Log4J file Empty" and made some changes in my code. I changed the import statement to

    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    

    and in my class :

    static Log   logger = LogFactory.getLog(Driver.class);
    

    Still i encounter the same problem. Any suggestion will be helpful.