Java Logger entering() and exiting() methods

11,254

Solution 1

Entering, exiting, and throwing are logged at level FINER. You'll have to set your logger to FINER or lower and set your FileHandler to FINER or lower.

thisLogger.addHandler(logFileHandler);
thisLogger.setLevel(Level.FINER);
logFileHandler.setLevel(Level.ALL);

As far as style goes, you should try creating a static final for class name because you'll use it as the name of the logger and reference it for entering, exiting, throwing, and log precise tracing:

private static final String CLASS_NAME = MyClass.class.getName();

You always want to use a class literal to fetch the name (as above) because getClass().getName() can return a subclass name which can falsify your tracing.

For method names don't include () in the name, just use the entering/exiting method that matches the number of arguments.

For constructor method names use "<init>" and for static init blocks use "<clinit>" as the method name since that is what would show up in a stacktrace.

Solution 2

As @jmehrens said, these "convenience" methods are logged at finer and your Logger and Handler objects are likely configured at a much higher level by default.

Here are more convenient entering and exiting replacements for use within the logged method and which you can simply change the level on:

logger.finer("ENTRY"); // compare to .entering(MyClass.class.getName(),"MyMethodName")
...
logger.finer("RETURN"); // less typing than .exiting(CLASS_NAME,"MyMethodName")

Same output as entering and exiting as long as you're providing the real class and method name. Simple enough to change .finer to .info.

To handle the parameterized versions use .log:

logger.log(Level.FINER,"ENTRY {0}", param1);
...
logger.log(Level.FINER,"RETURN {0}", result);

I think the only place entering/exiting makes sense is when you're logging from the caller's side:

logger.entering(thing.getClass().getName(),"DoStuff");
thing.DoStuff();
logger.exiting(thing.getClass().getName(),"DoStuff");

You could do the same with logp have control over what level it logs at, but it is more verbose:

logger.logp(Level.FINER,thing.getClass().getName(),"DoStuff","ENTRY");
thing.DoStuff();
logger.logp(Level.FINER,thing.getClass().getName(),"DoStuff","RETURN");
Share:
11,254

Related videos on Youtube

5YrsLaterDBA
Author by

5YrsLaterDBA

Updated on September 15, 2022

Comments

  • 5YrsLaterDBA
    5YrsLaterDBA about 1 year

    I just started to use Java Logger. I tried to use its entering() and exiting() methods with hard coded string for class name and method. They both didn't work for me. Just no log entry for them. But other log statements within that methods were logged properly. My logger level is set to CONFIG. I have tried to set to ALL but still cannot see log entry from them.

    I found for each entry, there already has a line with class name and method being logged. It seems these two methods are not necessary. But I am still want to know how to make them work for me.

    EDIT:

    My code followed: these entering() and exiting() are not create an entry in the log file

    //class variables
    private final static Logger logger = Logger.getLogger(MyClass.class.getName());
    private static FileHandler logFileHandler = null;
    
    //within the main() method
    Logger thisLogger = Logger.getLogger("");
    logFileHandler =  new FileHandler(logFileNameStr, false);
    logFileHandler.setFormatter(new SimpleFormatter());
    thisLogger.addHandler(logFileHandler);
    thisLogger.setLevel(Level.CONFIG);
    logger.log(Level.INFO, "Logfile Directory = " + logFileNameStr);
    
    
    //within a constructor of MyClass
    logger.entering("MyClass", "MyClass()");
    ....
    logger.info(initMsg);
    ....
    logger.exiting(this.getClass().getSimpleName(), "MyClass()");