Reputation: 37
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.
Upvotes: 2
Views: 15059
Reputation: 319
The solution is very simple: move the "Log4j.properties" file in the main Project directory.
Upvotes: -1
Reputation: 10987
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
Upvotes: 1
Reputation: 2628
Replace
log4j.rootLogger=off
with
log4j.rootLogger=DEBUG,stdout,Rollfile
Upvotes: 1