1.
If you don't set the level for a logger, then it inherits from the
parent logger.
import java.io.IOException; import java.util.logging.*; public class LogHierarchy { static final Logger lev1 = Logger.getLogger("Level1"); static final Logger lev2 = Logger.getLogger("Level1.Level2"); static final Logger lev3 = Logger.getLogger("Level1.Level2.Level3"); static Handler handler1, handler2, handler3; static void processData(){ lev3.info("Processing started"); /*Do Some processing here*/ lev3.log(Level.SEVERE, "Severe Error Occured"); lev3.info("Processing completed"); } public static void main(String args[]) throws IOException{ handler1 = new FileHandler("file1.out"); handler2 = new FileHandler("file2.out"); handler3 = new FileHandler("file3.out"); lev1.addHandler(handler1); lev2.addHandler(handler2); lev3.addHandler(handler3); lev1.setLevel(Level.SEVERE); processData(); } }
Run
the application, it creates 3 files 'file1.out', 'file2.out' and
'file3.out' and logs only severe messages.
2. If
the logger set the level 'X', then the messages at level 'X' and
higher only logged to higher levels of hierarchy.
import java.io.IOException; import java.util.logging.*; public class LogHierarchy { static final Logger lev1 = Logger.getLogger("Level1"); static final Logger lev2 = Logger.getLogger("Level1.Level2"); static final Logger lev3 = Logger.getLogger("Level1.Level2.Level3"); static Handler handler1, handler2, handler3; static void processData(){ lev3.log(Level.FINE, "Fine Message"); lev3.log(Level.INFO, "Information Message"); lev3.log(Level.WARNING, "Warning Message"); lev3.log(Level.SEVERE, "Severe Message"); } public static void main(String args[]) throws IOException{ handler1 = new FileHandler("file1.out"); handler2 = new FileHandler("file2.out"); handler3 = new FileHandler("file3.out"); lev1.addHandler(handler1); lev2.addHandler(handler2); lev3.addHandler(handler3); lev1.setLevel(Level.INFO); lev2.setLevel(Level.WARNING); lev3.setLevel(Level.SEVERE); processData(); } }
Even
though loggers lev1 and lev2 set at levels INFO, WARNING, they don't
log the FINE, WARNING and INFO messages. They log only SEVERE
messages.
No comments:
Post a Comment