RE: [SPAM (Bayesain Analysis)] - RE: static log4j configuration statement - Bayesian Filter detected spam

2008-09-11 Thread Bender Heri
Just put your property file into the classpath and you do not have to
worry about initialization of log4j framework. It does it automagically
when you fetch the first logger.
Heri 

 -Original Message-
 From: Michael Erskine [mailto:[EMAIL PROTECTED] 
 Sent: Wednesday, September 10, 2008 4:20 PM
 To: Log4J Users List
 Subject: [SPAM (Bayesain Analysis)] - RE: static log4j 
 configuration statement - Bayesian Filter detected spam
 
  I use standard static code to include the log4j configuration:
 
  static {
  PropertyConfigurator.configure(file));
  }
 
  But this code is not always executed, depending on if the class in 
  which this is included particpates in execution. But if I add this 
  statment to other classes too, I sooner or later get 
 double-messages 
  as two classes with this statement are used. Then I need to 
 manually 
  remove the code in one of them to achieve proper logging.
 
  How this is done right? I just want to have this 
 configuration stated 
  once and applicable to the whole project.
 
 Hi Sebastian,
 
 What I tend to do is have a class to do something similar but 
 first check whether already configured: my class is called 
 LogConfigureCheck and is used in a static block for all my 
 JUnit tests thus...
 
 static {
 LogConfigureCheck.check();
 }
 
 ...and here follows a version of the class suitable for 
 redistribution.
 
 Enjoy,
 Michael Erskine
 
 
 import org.apache.log4j.ConsoleAppender; import 
 org.apache.log4j.Logger; import org.apache.log4j.PatternLayout;
 
 /**
  * Occasionally we find a JUnit test suite or test case class 
 that tests classes
  * that use log4j but for whatever reason log4j is not 
 configured. We want to
  * avoid log4j being configured multiple times as this adds 
 its own problems!
  * The aim here is to enforce that log4j is configured once 
 and only once with
  * typical but useful features.
  *
  * @author Michael Erskine (msemtd)
  */
 public class LogConfigureCheck {
 public static void check() {
 // One indicator of log4j not being configured is the 
 lack of appenders
 // for the root logger. So...
 if 
 (!Logger.getRootLogger().getAllAppenders().hasMoreElements()) {
 // Here we could just use BasicConfigurator.configure()
 // but the layout is not as useful as it could be 
 - here we do
 // essentially the same but with a better layout...
 Logger.getRootLogger().addAppender(
 new ConsoleAppender(new PatternLayout(
 %-5p %d{HH:mm:ss.SSS} %c [%t] %m%n)));
 }
 }
 }
 
 
 -
 To unsubscribe, e-mail: [EMAIL PROTECTED]
 For additional commands, e-mail: [EMAIL PROTECTED]
 
 

-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



RE: static log4j configuration statement

2008-09-11 Thread Michael Erskine
Bender Heri [mailto:[EMAIL PROTECTED] wrote:

 Just put your property file into the classpath and you do not have to
 worry about initialization of log4j framework. It does it automagically
 when you fetch the first logger.
 Heri

Whilst that might be perfectly correct it is not what I'm attempting to achieve 
here; when I run any of my JUnit test suites the normal initialisation is 
suppressed and the first test will bootstrap a pleasing ConsoleAppender so the 
test results can be recorded. This technique can be used in any scenario where 
we want a basic but useful log4j configuration created if one has not already 
been configured explicitly (or, perhaps, automagically).

Regards,
Michael Erskine.


-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



RE: static log4j configuration statement

2008-09-10 Thread Michael Erskine
 I use standard static code to include the log4j configuration:

 static {
 PropertyConfigurator.configure(file));
 }

 But this code is not always executed, depending on if the class in which
 this is included particpates in execution. But if I add this statment to
 other classes too,
 I sooner or later get double-messages as two classes with this statement
 are used. Then I need to manually remove the code in one of them to
 achieve proper logging.

 How this is done right? I just want to have this configuration stated
 once and applicable to the whole project.

Hi Sebastian,

What I tend to do is have a class to do something similar but first check 
whether already configured: my class is called LogConfigureCheck and is used in 
a static block for all my JUnit tests thus...

static {
LogConfigureCheck.check();
}

...and here follows a version of the class suitable for redistribution.

Enjoy,
Michael Erskine


import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;

/**
 * Occasionally we find a JUnit test suite or test case class that tests classes
 * that use log4j but for whatever reason log4j is not configured. We want to
 * avoid log4j being configured multiple times as this adds its own problems!
 * The aim here is to enforce that log4j is configured once and only once with
 * typical but useful features.
 *
 * @author Michael Erskine (msemtd)
 */
public class LogConfigureCheck {
public static void check() {
// One indicator of log4j not being configured is the lack of appenders
// for the root logger. So...
if (!Logger.getRootLogger().getAllAppenders().hasMoreElements()) {
// Here we could just use BasicConfigurator.configure()
// but the layout is not as useful as it could be - here we do
// essentially the same but with a better layout...
Logger.getRootLogger().addAppender(
new ConsoleAppender(new PatternLayout(
%-5p %d{HH:mm:ss.SSS} %c [%t] %m%n)));
}
}
}


-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]