Normally I would have answered to Stephen only. But this is a question you often find
and so it might be of interest to more users.
(1) for java 1.3 or less you might use
public static String getClassName() {
CurrentClassGetter ccg = new CurrentClassGetter() ;
String sReturn = ccg.getClassName() ;
return sReturn ;
}
public static class CurrentClassGetter extends SecurityManager {
public String getClassName() {
return getClassContext()[1].getName();
}
}
(2) In java 1.4 or higher the following should do the trick
public static String getClassName() {
String sReturn = new Exception().getStackTrace()[0].getClassName() ;
return sReturn ;
}
Since I don't have 1.4 here I can't verify (2). (1) I found at http://www.rgagnon.com/
Regards
Gyoergy
-----Urspr�ngliche Nachricht-----
Von: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]
Gesendet: Montag, 1. M�rz 2004 04:51
An: [EMAIL PROTECTED]
Betreff: Getting the current class name in static code
From: [EMAIL PROTECTED]
To: [EMAIL PROTECTED]
Date: Mon, 1 Mar 2004 16:51:19 +1300
Subject: Getting the current class name in static code
In log4j a popular way to name loggers is to name them by software
component. This can be performed by statically instantiating a logger in
each class, as follows:
package com.foo;
import org.apache.log4j.Logger;
public class Bar {
static Logger logger = Logger.getLogger(Bar.class);
...
Can anyone think of a generic way to pass the name of the current class to
the getLogger method, without having to name the actual class in the code?
Doing so would enable developers to simply copy and paste that one line of
code into new source files without having to manually modify it to pass in
the new class name. (Ideally Java needs a static equivalent of "this" to
refer to the current class.)
Cheers
Steve
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]