logging-log4j-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Matt Brown <Matt.Br...@citrixOnline.com>
Subject RE: Log4j over several java classes
Date Wed, 04 Nov 2009 13:16:39 GMT
If each of your classes create a new LogFile instance, and the constructor of the LogFile class
creates a new appender and adds it to the root logger, then this means that you've attached
N copies of the appender to the root logger. This explains why you are seeing the message
duplicated in the file, since each appender is writing to the same file.

Is there any reason why you don't have each class get a Logger instance with the same name
of the class? This is the standard usage pattern and is very simple and works well: http://logging.apache.org/log4j/1.2/faq.html#2.4


-----Original Message-----
From: DanyLux [mailto:dany.gilgomes@gmail.com] 
Sent: Wednesday, November 04, 2009 7:22 AM
To: log4j-user@logging.apache.org
Subject: Log4j over several java classes

Hello to all users,

I am new on Log4j.
I am doing a project in java with eclipse and my project has the following

                                    ----class4.java   <-- Applet

And some other information:
class4 calls class3 and class5
class2 is called by class1 and class1 called by class5.

In LogFile i have written the following:
public class LogFile {

	private static final String dirname  = "./log/"; //location of the log files
	private static  Logger logger   = Logger.getRootLogger();

	 * This is the default constructor
	public LogFile() {
		try {
			PatternLayout layout = new PatternLayout( "%d (%c) [%t] %-5p %m%n" );
			DailyRollingFileAppender fileAppender =
				new DailyRollingFileAppender( layout, dirname + "Log.log", "'.'yyyy-MM-dd" );
			logger.addAppender( fileAppender );
		} catch( Exception ex ) {
			System.err.println( ex );

	public void logError(String message){
                logger.info("Write several more information");

And so I called it on the several classes:
private static LogFile lfLogging = new LogFile(); .....
lfLogging.logError("Error Message is written here!");

Well I entered it to all the class and the Logfile is written, the only problem is when the
method is called to write to logfile then it is not just written once, but it is written 4
times the same. I commented in all class just not in one the lines of logfile calling and
then it just write once in the LogFile.

Can somebody help me how I have to declare that my LogFile works correctly.

Many thanks in advance for your help.

Best regards,

View this message in context: http://old.nabble.com/Log4j-over-several-java-classes-tp26195319p26195319.html
Sent from the Log4j - Users mailing list archive at Nabble.com.

To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org

To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org

View raw message