-
Enhancement
-
Resolution: Future Project
-
P4
-
None
-
5.0
-
None
-
generic
-
generic
It's a very common practice to obtain a logger for each class, like this:
class Foo {
static Logger logger = Logger.getLogger(Foo.class.getName());
}
In fact in many logging libraries, there's the form of the getLogger method that takes a Class object,
so that you can write it as:
class Foo {
static Logger logger = Logger.getLogger(Foo.class);
}
The java.util.logging package should support this.
We can actually go one step further and allow applications to be written like this:
class Foo {
static Logger logger = Logger.getClassLogger();
}
This can be implemented as:
public static Logger getClassLogger() {
StackTraceElement[] trace = Thread.currentThread().getStackTrace();
return Logger.getLogger(trace[1].getClassName());
}
(In JDK, either this needs to be done in the priviledged block to avoid possible SecurityException)
In this way, there's less characters to type, and more importantly, this line can be cut&pasted from one class to another.
class Foo {
static Logger logger = Logger.getLogger(Foo.class.getName());
}
In fact in many logging libraries, there's the form of the getLogger method that takes a Class object,
so that you can write it as:
class Foo {
static Logger logger = Logger.getLogger(Foo.class);
}
The java.util.logging package should support this.
We can actually go one step further and allow applications to be written like this:
class Foo {
static Logger logger = Logger.getClassLogger();
}
This can be implemented as:
public static Logger getClassLogger() {
StackTraceElement[] trace = Thread.currentThread().getStackTrace();
return Logger.getLogger(trace[1].getClassName());
}
(In JDK, either this needs to be done in the priviledged block to avoid possible SecurityException)
In this way, there's less characters to type, and more importantly, this line can be cut&pasted from one class to another.