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 Laurent Delaforge <ld...@aol.com> on 2003/08/04 09:18:34 UTC

Logger.setLevel()

Hi !

I have this code source :

public class TestLogging
{
     Logger mLoggerRoot = Logger.getRootLogger();
     Logger mClassLogger = Logger.getLogger(TestLogging.class.getName());
     Logger mClassLoggerSoon =
Logger.getLogger(TestLogging.class.getName() + ".soon");

     public void levelInheritance()
     {
         // init
         mClassLogger.setLevel(Level.DEBUG);
         mLoggerRoot.fatal("init : Level of " + mClassLogger.getName() +
" : " + mClassLogger.getEffectiveLevel());
         mLoggerRoot.fatal("init : Level of " +
mClassLoggerSoon.getName() + " : " + mClassLoggerSoon.getEffectiveLevel());

         // mClassLoggerSoon.setLevel(Level.FATAL); // try once with this
line uncommented and once again commented
         mClassLogger.setLevel(Level.FATAL);
         mLoggerRoot.fatal("1. Level of " + mClassLogger.getName() + " :
" + mClassLogger.getEffectiveLevel());
         mLoggerRoot.fatal("Level of " + mClassLoggerSoon.getName() + " :
" + mClassLoggerSoon.getEffectiveLevel());

         mClassLogger.setLevel(Level.DEBUG);
         mLoggerRoot.fatal("2. Level of " + mClassLogger.getName() + " :
" + mClassLogger.getEffectiveLevel());
         mLoggerRoot.fatal("Level of " + mClassLoggerSoon.getName() + " :
" + mClassLoggerSoon.getEffectiveLevel());
     }

     public static void main(String args[])
     {
         TestLogging lTestLogging = new TestLogging();
         lTestLogging.levelInheritance();
     }
}

Then you have :

root.levelInheritance [01 août 2003 15:42:43,587] 0
(TestLogging.java:112) main FATAL : init : Level of TestLogging : DEBUG
root.levelInheritance [01 août 2003 15:42:43,597] 10
(TestLogging.java:113) main FATAL : init : Level of TestLogging.soon : DEBUG
root.levelInheritance [01 août 2003 15:42:43,597] 10
(TestLogging.java:117) main FATAL : 1. Level of TestLogging : FATAL
root.levelInheritance [01 août 2003 15:42:43,597] 10
(TestLogging.java:118) main FATAL : Level of TestLogging.soon : FATAL
root.levelInheritance [01 août 2003 15:42:43,627] 40
(TestLogging.java:121) main FATAL : 2. Level of TestLogging : DEBUG
root.levelInheritance [01 août 2003 15:42:43,627] 40
(TestLogging.java:122) main FATAL : Level of TestLogging.soon : DEBUG <--

1. when we set TestLogging to level FATAL i think it is normal that
TestLogging.soon become FATAL too,
because it is not possible for a logger to have his level 'lower' than
his ancestor.
(So, for this case, it was not possible that level of TestLogging was
FATAL and level of TestLogging.soon was DEBUG)

2. when we set TestLogging to level DEBUG i don't think it is normal
that mClassLoggerSoon become DEBUG too,
because it IS possible for a logger to have his level 'higher' than his
ancestor.
(So, for this case, it was not possible that level of TestLogging was
DEBUG and level of TestLogging.soon was FATAL)

My question :
-----------------
Can you explain to me why in the case 2., level of TestLogging.soon
become DEBUG and doesn't keep its level FATAL.


Notice :
if you uncomment the line "mClassLoggerSoon.setLevel(Level.FATAL);",
there is no problem...

root.levelInheritance [01 août 2003 15:39:30,379] 0
(TestLogging.java:112) main FATAL : init : Level of TestLogging : DEBUG
root.levelInheritance [01 août 2003 15:39:30,389] 10
(TestLogging.java:113) main FATAL : init : Level of TestLogging.soon : DEBUG
root.levelInheritance [01 août 2003 15:39:30,389] 10
(TestLogging.java:118) main FATAL : 1. Level of TestLogging : FATAL
root.levelInheritance [01 août 2003 15:39:30,389] 10
(TestLogging.java:119) main FATAL : Level of TestLogging.soon : FATAL
root.levelInheritance [01 août 2003 15:39:30,449] 70
(TestLogging.java:122) main FATAL : 2. Level of TestLogging : DEBUG
root.levelInheritance [01 août 2003 15:39:30,449] 70
(TestLogging.java:123) main FATAL : Level of TestLogging.soon : FATAL
<-- NOW THIS IS NORMAL

Thank you


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


RE: Logger.setLevel()

Posted by Laurent Delaforge <ld...@aol.com>.
Thank you for your answer which is perfectly clear.
Now I understand the stupidity of my question : i hadn't understand the 
basis of the logger's level in a hierarchy !

Steve Ebersole wrote:

 > Your hierarchy looks like this:
 >
 > root
 >   |-- TestLogging
 >         |-- soon
 >
 > Messages logged against "soon" will be level-tested as follows:
 > 1) The level explicitly set on the "soon" node, if one;
 > 2) If none, the level explicitly set on the "TestLogging" node, if one;
 > 3) If none, the level explicitly set on root, if one, or else root's
 > default
 >
 > The important point is that a logger's level is either the level
 > explicity
 > set on the logger, or the level of the node closest to it in the
 > hierarchy
 > with an explicitly set level.  In your test, you explicitly set a
 > level on
 > "TestLogging", but not on "soon"; thus messages sent to thhe "soon"
 > logger
 > will be checked against that explicitly set level.
 >
 >
 > >> 1. when we set TestLogging to level FATAL i think it is normal that
 > >> TestLogging.soon become FATAL too,
 > As long as you are not explicitly setting a level on TestLogging.soon,
 > this
 > is correct; it will inherit the level of its ancestor.  And this is what
 > happens in your test.
 >
 > >> because it is not possible for a logger to have his level 'lower' than
 > >> his ancestor.
 > >> (So, for this case, it was not possible that level of TestLogging was
 > >> FATAL and level of TestLogging.soon was DEBUG)
 > As your own tests show, this is an incorrect assumption...  Consider a
 > typically use-case.  Say I have a "util" package with a bunch of utility
 > classes.  I do not want to see logging from this package normally, so
 > I set
 > the level on that package to FATAL (or maybe even OFF).  However, a
 > class in
 > that package has recently been giving me trouble (or I want to
 > optimize it,
 > etc), so I want to kick the level for that class alone down to DEBUG.
 > That
 > way all messages from that particular class will log at the DEBUG
 > level (so
 > I can see what's going on internally) but all other classes in that
 > package
 > continue to log at FATAL (so they don't clutter my logs).
 >
 >
 >
 > >> 2. when we set TestLogging to level DEBUG i don't think it is normal
 > >> that mClassLoggerSoon become DEBUG too,
 > >> because it IS possible for a logger to have his level 'higher' than
 > his
 > >> ancestor.
 > >> (So, for this case, it was not possible that level of TestLogging was
 > >> DEBUG and level of TestLogging.soon was FATAL)
 > Again, you have never explicitly set the level of the "soon" node, and
 > so it
 > is simply inheriting the level of its ancestor.
 >
 >
 >
 >
 > Consider a stripped-down paraphrase of you test...
 >
 > As posted:
 >
 >          // init
 >          mClassLogger.setLevel(Level.DEBUG);
 > init : Level of TestLogging : DEBUG
 > init : Level of TestLogging.soon : DEBUG
 >
 >          // mClassLoggerSoon.setLevel(Level.FATAL);
 >          mClassLogger.setLevel(Level.FATAL);
 > Level of TestLogging : FATAL
 > Level of TestLogging.soon : FATAL
 >
 >          mClassLogger.setLevel(Level.DEBUG);
 > Level of TestLogging : DEBUG
 > Level of TestLogging.soon : DEBUG
 >
 >
 >
 > Explicitly setting level on "soon" logger (what should happen):
 >
 >          // init
 >          mClassLogger.setLevel(Level.DEBUG);
 > init : Level of TestLogging : DEBUG
 > init : Level of TestLogging.soon : DEBUG
 >
 >          mClassLoggerSoon.setLevel(Level.FATAL); // <- uncommented
 >          mClassLogger.setLevel(Level.FATAL);
 > Level of TestLogging : FATAL
 > Level of TestLogging.soon : FATAL
 >
 >          mClassLogger.setLevel(Level.DEBUG);
 > Level of TestLogging : DEBUG
 > Level of TestLogging.soon : FATAL
 >
 >
 >
 >
 >
 >
 > -----Original Message-----
 > From: Laurent Delaforge [mailto:ldelaforgefr@aol.com]
 > Sent: Monday, August 04, 2003 2:19 AM
 > To: log4j-user@jakarta.apache.org
 > Subject: Logger.setLevel()
 >
 >
 > Hi !
 >
 > I have this code source :
 >
 > public class TestLogging
 > {
 >      Logger mLoggerRoot = Logger.getRootLogger();
 >      Logger mClassLogger = Logger.getLogger(TestLogging.class.getName());
 >      Logger mClassLoggerSoon =
 > Logger.getLogger(TestLogging.class.getName() + ".soon");
 >
 >      public void levelInheritance()
 >      {
 >          // init
 >          mClassLogger.setLevel(Level.DEBUG);
 >          mLoggerRoot.fatal("init : Level of " + mClassLogger.getName() +
 > " : " + mClassLogger.getEffectiveLevel());
 >          mLoggerRoot.fatal("init : Level of " +
 > mClassLoggerSoon.getName() + " : " +
 > mClassLoggerSoon.getEffectiveLevel());
 >
 >          // mClassLoggerSoon.setLevel(Level.FATAL); // try once with this
 > line uncommented and once again commented
 >          mClassLogger.setLevel(Level.FATAL);
 >          mLoggerRoot.fatal("1. Level of " + mClassLogger.getName() + " :
 > " + mClassLogger.getEffectiveLevel());
 >          mLoggerRoot.fatal("Level of " + mClassLoggerSoon.getName() + " :
 > " + mClassLoggerSoon.getEffectiveLevel());
 >
 >          mClassLogger.setLevel(Level.DEBUG);
 >          mLoggerRoot.fatal("2. Level of " + mClassLogger.getName() + " :
 > " + mClassLogger.getEffectiveLevel());
 >          mLoggerRoot.fatal("Level of " + mClassLoggerSoon.getName() + " :
 > " + mClassLoggerSoon.getEffectiveLevel());
 >      }
 >
 >      public static void main(String args[])
 >      {
 >          TestLogging lTestLogging = new TestLogging();
 >          lTestLogging.levelInheritance();
 >      }
 > }
 >
 > Then you have :
 >
 > root.levelInheritance [01 août 2003 15:42:43,587] 0
 > (TestLogging.java:112) main FATAL : init : Level of TestLogging : DEBUG
 > root.levelInheritance [01 août 2003 15:42:43,597] 10
 > (TestLogging.java:113) main FATAL : init : Level of TestLogging.soon :
 > DEBUG
 > root.levelInheritance [01 août 2003 15:42:43,597] 10
 > (TestLogging.java:117) main FATAL : 1. Level of TestLogging : FATAL
 > root.levelInheritance [01 août 2003 15:42:43,597] 10
 > (TestLogging.java:118) main FATAL : Level of TestLogging.soon : FATAL
 > root.levelInheritance [01 août 2003 15:42:43,627] 40
 > (TestLogging.java:121) main FATAL : 2. Level of TestLogging : DEBUG
 > root.levelInheritance [01 août 2003 15:42:43,627] 40
 > (TestLogging.java:122) main FATAL : Level of TestLogging.soon : DEBUG <--
 >
 > 1. when we set TestLogging to level FATAL i think it is normal that
 > TestLogging.soon become FATAL too,
 > because it is not possible for a logger to have his level 'lower' than
 > his ancestor.
 > (So, for this case, it was not possible that level of TestLogging was
 > FATAL and level of TestLogging.soon was DEBUG)
 >
 > 2. when we set TestLogging to level DEBUG i don't think it is normal
 > that mClassLoggerSoon become DEBUG too,
 > because it IS possible for a logger to have his level 'higher' than his
 > ancestor.
 > (So, for this case, it was not possible that level of TestLogging was
 > DEBUG and level of TestLogging.soon was FATAL)
 >
 > My question :
 > -----------------
 > Can you explain to me why in the case 2., level of TestLogging.soon
 > become DEBUG and doesn't keep its level FATAL.
 >
 >
 > Notice :
 > if you uncomment the line "mClassLoggerSoon.setLevel(Level.FATAL);",
 > there is no problem...
 >
 > root.levelInheritance [01 août 2003 15:39:30,379] 0
 > (TestLogging.java:112) main FATAL : init : Level of TestLogging : DEBUG
 > root.levelInheritance [01 août 2003 15:39:30,389] 10
 > (TestLogging.java:113) main FATAL : init : Level of TestLogging.soon :
 > DEBUG
 > root.levelInheritance [01 août 2003 15:39:30,389] 10
 > (TestLogging.java:118) main FATAL : 1. Level of TestLogging : FATAL
 > root.levelInheritance [01 août 2003 15:39:30,389] 10
 > (TestLogging.java:119) main FATAL : Level of TestLogging.soon : FATAL
 > root.levelInheritance [01 août 2003 15:39:30,449] 70
 > (TestLogging.java:122) main FATAL : 2. Level of TestLogging : DEBUG
 > root.levelInheritance [01 août 2003 15:39:30,449] 70
 > (TestLogging.java:123) main FATAL : Level of TestLogging.soon : FATAL
 > <-- NOW THIS IS NORMAL
 >
 > Thank you
 >
 >
 > ---------------------------------------------------------------------
 > To unsubscribe, e-mail: log4j-user-unsubscribe@jakarta.apache.org
 > For additional commands, e-mail: log4j-user-help@jakarta.apache.org
 >
 >
 > ---------------------------------------------------------------------
 > To unsubscribe, e-mail: log4j-user-unsubscribe@jakarta.apache.org
 > For additional commands, e-mail: log4j-user-help@jakarta.apache.org
 >



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


RE: Logger.setLevel()

Posted by Steve Ebersole <st...@austin.rr.com>.
Your hierarchy looks like this:

root
  |-- TestLogging
        |-- soon

Messages logged against "soon" will be level-tested as follows:
1) The level explicitly set on the "soon" node, if one;
2) If none, the level explicitly set on the "TestLogging" node, if one;
3) If none, the level explicitly set on root, if one, or else root's default

The important point is that a logger's level is either the level explicity
set on the logger, or the level of the node closest to it in the hierarchy
with an explicitly set level.  In your test, you explicitly set a level on
"TestLogging", but not on "soon"; thus messages sent to thhe "soon" logger
will be checked against that explicitly set level.


>> 1. when we set TestLogging to level FATAL i think it is normal that
>> TestLogging.soon become FATAL too,
As long as you are not explicitly setting a level on TestLogging.soon, this
is correct; it will inherit the level of its ancestor.  And this is what
happens in your test.

>> because it is not possible for a logger to have his level 'lower' than
>> his ancestor.
>> (So, for this case, it was not possible that level of TestLogging was
>> FATAL and level of TestLogging.soon was DEBUG)
As your own tests show, this is an incorrect assumption...  Consider a
typically use-case.  Say I have a "util" package with a bunch of utility
classes.  I do not want to see logging from this package normally, so I set
the level on that package to FATAL (or maybe even OFF).  However, a class in
that package has recently been giving me trouble (or I want to optimize it,
etc), so I want to kick the level for that class alone down to DEBUG.  That
way all messages from that particular class will log at the DEBUG level (so
I can see what's going on internally) but all other classes in that package
continue to log at FATAL (so they don't clutter my logs).



>> 2. when we set TestLogging to level DEBUG i don't think it is normal
>> that mClassLoggerSoon become DEBUG too,
>> because it IS possible for a logger to have his level 'higher' than his
>> ancestor.
>> (So, for this case, it was not possible that level of TestLogging was
>> DEBUG and level of TestLogging.soon was FATAL)
Again, you have never explicitly set the level of the "soon" node, and so it
is simply inheriting the level of its ancestor.




Consider a stripped-down paraphrase of you test...

As posted:

         // init
         mClassLogger.setLevel(Level.DEBUG);
init : Level of TestLogging : DEBUG
init : Level of TestLogging.soon : DEBUG

         // mClassLoggerSoon.setLevel(Level.FATAL);
         mClassLogger.setLevel(Level.FATAL);
Level of TestLogging : FATAL
Level of TestLogging.soon : FATAL

         mClassLogger.setLevel(Level.DEBUG);
Level of TestLogging : DEBUG
Level of TestLogging.soon : DEBUG



Explicitly setting level on "soon" logger (what should happen):

         // init
         mClassLogger.setLevel(Level.DEBUG);
init : Level of TestLogging : DEBUG
init : Level of TestLogging.soon : DEBUG

         mClassLoggerSoon.setLevel(Level.FATAL); // <- uncommented
         mClassLogger.setLevel(Level.FATAL);
Level of TestLogging : FATAL
Level of TestLogging.soon : FATAL

         mClassLogger.setLevel(Level.DEBUG);
Level of TestLogging : DEBUG
Level of TestLogging.soon : FATAL






-----Original Message-----
From: Laurent Delaforge [mailto:ldelaforgefr@aol.com]
Sent: Monday, August 04, 2003 2:19 AM
To: log4j-user@jakarta.apache.org
Subject: Logger.setLevel()


Hi !

I have this code source :

public class TestLogging
{
     Logger mLoggerRoot = Logger.getRootLogger();
     Logger mClassLogger = Logger.getLogger(TestLogging.class.getName());
     Logger mClassLoggerSoon =
Logger.getLogger(TestLogging.class.getName() + ".soon");

     public void levelInheritance()
     {
         // init
         mClassLogger.setLevel(Level.DEBUG);
         mLoggerRoot.fatal("init : Level of " + mClassLogger.getName() +
" : " + mClassLogger.getEffectiveLevel());
         mLoggerRoot.fatal("init : Level of " +
mClassLoggerSoon.getName() + " : " + mClassLoggerSoon.getEffectiveLevel());

         // mClassLoggerSoon.setLevel(Level.FATAL); // try once with this
line uncommented and once again commented
         mClassLogger.setLevel(Level.FATAL);
         mLoggerRoot.fatal("1. Level of " + mClassLogger.getName() + " :
" + mClassLogger.getEffectiveLevel());
         mLoggerRoot.fatal("Level of " + mClassLoggerSoon.getName() + " :
" + mClassLoggerSoon.getEffectiveLevel());

         mClassLogger.setLevel(Level.DEBUG);
         mLoggerRoot.fatal("2. Level of " + mClassLogger.getName() + " :
" + mClassLogger.getEffectiveLevel());
         mLoggerRoot.fatal("Level of " + mClassLoggerSoon.getName() + " :
" + mClassLoggerSoon.getEffectiveLevel());
     }

     public static void main(String args[])
     {
         TestLogging lTestLogging = new TestLogging();
         lTestLogging.levelInheritance();
     }
}

Then you have :

root.levelInheritance [01 août 2003 15:42:43,587] 0
(TestLogging.java:112) main FATAL : init : Level of TestLogging : DEBUG
root.levelInheritance [01 août 2003 15:42:43,597] 10
(TestLogging.java:113) main FATAL : init : Level of TestLogging.soon : DEBUG
root.levelInheritance [01 août 2003 15:42:43,597] 10
(TestLogging.java:117) main FATAL : 1. Level of TestLogging : FATAL
root.levelInheritance [01 août 2003 15:42:43,597] 10
(TestLogging.java:118) main FATAL : Level of TestLogging.soon : FATAL
root.levelInheritance [01 août 2003 15:42:43,627] 40
(TestLogging.java:121) main FATAL : 2. Level of TestLogging : DEBUG
root.levelInheritance [01 août 2003 15:42:43,627] 40
(TestLogging.java:122) main FATAL : Level of TestLogging.soon : DEBUG <--

1. when we set TestLogging to level FATAL i think it is normal that
TestLogging.soon become FATAL too,
because it is not possible for a logger to have his level 'lower' than
his ancestor.
(So, for this case, it was not possible that level of TestLogging was
FATAL and level of TestLogging.soon was DEBUG)

2. when we set TestLogging to level DEBUG i don't think it is normal
that mClassLoggerSoon become DEBUG too,
because it IS possible for a logger to have his level 'higher' than his
ancestor.
(So, for this case, it was not possible that level of TestLogging was
DEBUG and level of TestLogging.soon was FATAL)

My question :
-----------------
Can you explain to me why in the case 2., level of TestLogging.soon
become DEBUG and doesn't keep its level FATAL.


Notice :
if you uncomment the line "mClassLoggerSoon.setLevel(Level.FATAL);",
there is no problem...

root.levelInheritance [01 août 2003 15:39:30,379] 0
(TestLogging.java:112) main FATAL : init : Level of TestLogging : DEBUG
root.levelInheritance [01 août 2003 15:39:30,389] 10
(TestLogging.java:113) main FATAL : init : Level of TestLogging.soon : DEBUG
root.levelInheritance [01 août 2003 15:39:30,389] 10
(TestLogging.java:118) main FATAL : 1. Level of TestLogging : FATAL
root.levelInheritance [01 août 2003 15:39:30,389] 10
(TestLogging.java:119) main FATAL : Level of TestLogging.soon : FATAL
root.levelInheritance [01 août 2003 15:39:30,449] 70
(TestLogging.java:122) main FATAL : 2. Level of TestLogging : DEBUG
root.levelInheritance [01 août 2003 15:39:30,449] 70
(TestLogging.java:123) main FATAL : Level of TestLogging.soon : FATAL
<-- NOW THIS IS NORMAL

Thank you


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


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