package name;

import org.apache.log4j.*;
import org.apache.commons.logging.*;

/**
 * Result: If we want many classes in a package to share a logger, and
 * thus ALL see the results of a single setLevel() call, all loggers
 * MUST be declared with the SAME string. so THIS works:
 *
 * log = Logger.getLogger( getClass().getPackage().getName() );
 *
 * but this does NOT work (since individual loggers are created for each class
 * and setting one's level does NOT affect any others)
 *
 * log = Logger.getLogger( getClass() );
 */
public class LogLevelsTest extends junit.framework.TestCase {

	static class A {
		A() {

			/*
			  This is what NOT to do if you want all classes to use SAME
			  logger and so all 'see' the effects of a setLevel() call
			*/
			//			log = Logger.getLogger( getClass() );


			/*
			  This IS what to do, since all log creations then use the SAME
			  string
			*/
			log = Logger.getLogger( getClass().getPackage().getName() );

			log.debug( log.getName() );
		}
		protected Logger log;
	}

	static class B extends A {
	}

	static class C extends A {
	}

	static class D {
		D() {
			log = LogFactory.getLog( getClass().getPackage().getName() );
			log.debug( "D" );
		}
		Log log;
	}
	
	public void testNull() {
	}

	public void testSharedLevels() {
		B b = new B();
		C c = new C();
		D d = new D();
		
		System.out.println( b.log.getLevel() );
		System.out.println( c.log.getLevel() );

		System.out.println( b.log.getEffectiveLevel() );
		System.out.println( c.log.getEffectiveLevel() );

		b.log.setLevel( Level.WARN );

		System.out.println( b.log.getLevel() );
		System.out.println( c.log.getLevel() );

		System.out.println( b.log.getEffectiveLevel() );
		System.out.println( c.log.getEffectiveLevel() );

		d.log.debug( "D.debug" );
		d.log.info( "D.info" );
		d.log.warn( "D.warn" );
	}
}

// eof

@@ -146,16 +146,42 @@
 gger
+ in a%0A * log4j logging system
 , and
-%0A *
  thu
@@ -213,16 +213,19 @@
 a single
+%0A *
  setLeve
@@ -245,19 +245,16 @@
  loggers
-%0A *
  MUST be
@@ -280,16 +280,19 @@
 SAME
+%0A *
  string.
  so 
@@ -287,16 +287,105 @@
  string.
+ So as long as all the classes are in the same package,we%0A * use the common package name,
  so THIS
@@ -529,16 +529,19 @@
 ated for
+%0A *
  each cl
@@ -547,16 +547,43 @@
 lass
-%0A *
  and 
+thus have different names, so 
 sett
@@ -597,16 +597,19 @@
 's level
+%0A *
  does NO
@@ -627,17 +627,43 @@
 y others
-)
+, this is how log4j works):
 %0A *%0A * l
@@ -699,16 +699,356 @@
 ss() );%0A
+ *%0A * We fabricate four classes A,B,C,D all in the same package.  D uses%0A * commons logging, just to see if its associated log4j logger impl%0A * follows same rules for level sharing as explicitly declared log4j%0A * loggers.  We cannot test/inspect D' log level directly, so have to%0A * just visually inspect the output of its log messages.%0A *%0A
  */%0Apubl
@@ -1764,16 +1764,258 @@
 ) %7B%0A%09%7D%0A%0A
+%09/**%0A%09 * The test is: create some objects of distincts classes, each%0A%09 * with a logger which is supposedly using package name for logger%0A%09 * name.  We then set the level in one logger and hope it observe%0A%09 * it changed in other loggers.%0A%09 */%0A
 %09public 
@@ -2392,32 +2392,89 @@
 .getLevel() );%0A%0A
+%09%09assertEquals( b.log.getLevel(), c.log.getLevel() );%0A%09%09%0A
 %09%09System.out.pri
@@ -2583,16 +2583,40 @@
 %22D.debug
+. You should NOT see me!
 %22 );%0A%09%09d
@@ -2633,16 +2633,40 @@
  %22D.info
+, You should NOT see me!
 %22 );%0A%09%09d
@@ -2683,16 +2683,37 @@
  %22D.warn
+.  You should see me.
 %22 );%0A%09%7D%0A

add real assertion to log4j level test case, was purely visual