Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting log level for inner class (in Glassfish)

I'm looking for a way to set the log level for an inner class, let's say I have the following class:

package mypackage;

class Outer {
   Inner inner = new Inner();

   public void logSomething() {
       Logger.getLogger(Outer.class.getName()).log(Level.FINE, "I'm alive!");
       inner.logSomething();
   } 

   class Inner {
       public void logSomething() {
           Logger.getLogger(Inner.class.getName()).log(Level.FINE, "I'm alive too!");
       }
   }
}

I can set the log level of Outer using the name mypackage.Outer. But how do I set it for Inner? I have tested the following without success:

  • mypackage.Outer
  • mypackage.Outer.Inner
  • mypackage.Outer$Inner (gives an error)
  • mypackage.Inner

Does anyone know if there is a way to do this?

like image 482
Petter Avatar asked Aug 10 '12 08:08

Petter


1 Answers

Inner.class.getName() is returning mypackage.Outer$Inner which doesn't follow the expected convention for Logger.getLogger() parameter

name - A name for the logger. This should be a dot-separated name and should normally be based on the package name or class name of the subsystem, such as java.net or javax.swing

You can explicitly use

Logger.getLogger("myPackage.Outer.Inner").log(Level.FINE, "I'm alive too!")

and set the level using myPackage.Outer.Inner

Curiously enough, the message gets formatted by SimpleFormatter() using $ for inner class as it's outputting class name, not logger name

28-feb-2013 19:04:16 mypackage.Outer$Inner logSomething
FINE: I'm alive too!
like image 131
fglez Avatar answered Nov 14 '22 05:11

fglez