Getting calling className in Java.Util.Logging.Logger

G

Gugle

Hi,
I'm using Java 1.5 and I'm using Java's Logger class for logging. I
would like to know how to get the className and methodname of the
calling method for logging(like log4j does). For e.g., if I'm calling
the log method from method a of class B, then the log message should
contain the classname and method name. I don't want to pass the method
name and class name everytime to the log method. Can someone let me
know how this can be done?
 
H

henry

Hi,
properly this helps you ....

StackTraceElement[] stack =
Thread.getAllStackTraces().get(Thread.currentThread());
for (int i = 0; i < stack.length; i++) {
StackTraceElement elem = stack;
System.out.println("Class "+ elem.getClassName() + " Method" +
elem.getMethodName());
}
I think form here you can go on..
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Gugle said:
I'm using Java 1.5 and I'm using Java's Logger class for logging. I
would like to know how to get the className and methodname of the
calling method for logging(like log4j does). For e.g., if I'm calling
the log method from method a of class B, then the log message should
contain the classname and method name. I don't want to pass the method
name and class name everytime to the log method. Can someone let me
know how this can be done?

Class name and method name are part of the default formatter.

But the following example show how you can control the output
yourself:

package december;

import java.util.Date;
import java.util.logging.ConsoleHandler;
import java.util.logging.Formatter;
import java.util.logging.LogRecord;
import java.util.logging.Logger;

public class Logging {
public static void main(String[] args) {
Logger stdlogger = Logger.getLogger("Test 1");
stdlogger.info("This is a test");
Logger modlogger = Logger.getLogger("Test 2");
modlogger.setUseParentHandlers(false);
modlogger.addHandler(new ConsoleHandler());
modlogger.getHandlers()[0].setFormatter(new MyFormat());
modlogger.info("This is a test");
}
}

class MyFormat extends Formatter {
public String format(LogRecord rec) {
return ("class = " + rec.getSourceClassName() + "," +
"method = " + rec.getSourceMethodName() + "," +
"time = " + new Date(rec.getMillis()) + "," +
"message = " + rec.getMessage());
}
}

Arne
 
G

Gugle

Thanks for your help guys..its working now..


Gugle said:
I'm using Java 1.5 and I'm using Java's Logger class for logging. I
would like to know how to get the className and methodname of the
calling method for logging(like log4j does). For e.g., if I'm calling
the log method from method a of class B, then the log message should
contain the classname and method name. I don't want to pass the method
name and class name everytime to the log method. Can someone let me
know how this can be done?

Class name and method name are part of the default formatter.

But the following example show how you can control the output
yourself:

package december;

import java.util.Date;
import java.util.logging.ConsoleHandler;
import java.util.logging.Formatter;
import java.util.logging.LogRecord;
import java.util.logging.Logger;

public class Logging {
public static void main(String[] args) {
Logger stdlogger = Logger.getLogger("Test 1");
stdlogger.info("This is a test");
Logger modlogger = Logger.getLogger("Test 2");
modlogger.setUseParentHandlers(false);
modlogger.addHandler(new ConsoleHandler());
modlogger.getHandlers()[0].setFormatter(new MyFormat());
modlogger.info("This is a test");
}
}

class MyFormat extends Formatter {
public String format(LogRecord rec) {
return ("class = " + rec.getSourceClassName() + "," +
"method = " + rec.getSourceMethodName() + "," +
"time = " + new Date(rec.getMillis()) + "," +
"message = " + rec.getMessage());
}
}

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top