You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-user@logging.apache.org by DanyLux <da...@gmail.com> on 2009/11/04 13:21:43 UTC

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
structure:

package
   ----underpackage
              ----categories
                            ----one
                                    ----class1.java
                                    ----class2.java
                            ----two
                                    ----class3.java
                                    ----class4.java   <-- Applet
                                    ----class5.java
                            ----three
                                    ----LogFile.java

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 );
			logger.setLevel(Level.INFO);
		} catch( Exception ex ) {
			System.err.println( ex );
		}
	}

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


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,
Dany






-- 
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


RE: Log4j over several java classes

Posted by Michael Erskine <mi...@ketech.com>.
> From: DanyLux [mailto:dany.gilgomes@gmail.com]
> ... So I don't need all time to rewrite 3
> lines or more everytime I want to log something.

You might want to consider the merits or otherwise of making 3 logging calls (hints: performance, concurrency) when you are clearly logging only one event. If you want more information or decoration in your log entries then you can use a more elaborate Layout.

Regards,
Michael Erskine.


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


RE: Log4j over several java classes

Posted by DanyLux <da...@gmail.com>.
Hi Matt,
Well what I wanted to to is to have a class like my LogFile.java file where
I define everything I need for the logfile. And as you can see I also have
done a method called logError(String message) which I call and make 2 info
logging and then an error logging. So I don't need all time to rewrite 3
lines or more everytime I want to log something.

Thanks in advance for your help.
Best regards,
Dany


Matt Brown wrote:
> 
> 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
> 
> http://logging.apache.org/log4j/1.2/manual.html
> 
> 
> 
> -----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
> structure:
> 
> package
>    ----underpackage
>               ----categories
>                             ----one
>                                     ----class1.java
>                                     ----class2.java
>                             ----two
>                                     ----class3.java
>                                     ----class4.java   <-- Applet
>                                     ----class5.java
>                             ----three
>                                     ----LogFile.java
> 
> 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 );
> 			logger.setLevel(Level.INFO);
> 		} catch( Exception ex ) {
> 			System.err.println( ex );
> 		}
> 	}
> 
> 	public void logError(String message){
> 		logger.info("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+");
>                 logger.info("Write several more information");
> 		logger.error(message);
> 	}
> }
> 
> 
> 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,
> Dany
> 
> 
> 
> 
> 
> 
> --
> 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 this message in context: http://old.nabble.com/Log4j-over-several-java-classes-tp26195319p26196281.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


RE: Log4j over several java classes

Posted by Matt Brown <Ma...@citrixOnline.com>.
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

http://logging.apache.org/log4j/1.2/manual.html



-----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
structure:

package
   ----underpackage
              ----categories
                            ----one
                                    ----class1.java
                                    ----class2.java
                            ----two
                                    ----class3.java
                                    ----class4.java   <-- Applet
                                    ----class5.java
                            ----three
                                    ----LogFile.java

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 );
			logger.setLevel(Level.INFO);
		} catch( Exception ex ) {
			System.err.println( ex );
		}
	}

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


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,
Dany






--
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