Getting calling className in Java.Util.Logging.Logger

Discussion in 'Java' started by Gugle, Nov 28, 2006.

  1. Gugle

    Gugle Guest

    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?
    Gugle, Nov 28, 2006
    #1
    1. Advertising

  2. Gugle

    henry Guest

    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..

    Gugle schrieb:

    > 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?
    henry, Nov 28, 2006
    #2
    1. Advertising

  3. Gugle wrote:
    > 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
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=, Dec 9, 2006
    #3
  4. Gugle

    Gugle Guest

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



    Arne Vajhøj wrote:
    > Gugle wrote:
    > > 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
    Gugle, Dec 12, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. CD1
    Replies:
    3
    Views:
    18,493
  2. Oliver S.

    classname::classname a type ?

    Oliver S., Sep 11, 2003, in forum: C++
    Replies:
    1
    Views:
    494
    White Wolf
    Sep 11, 2003
  3. Hongzheng Wang
    Replies:
    32
    Views:
    859
  4. Royan
    Replies:
    1
    Views:
    1,415
    Andrea Francia
    Apr 1, 2008
  5. cteb
    Replies:
    5
    Views:
    7,264
    Arne Vajhøj
    Oct 5, 2008
Loading...

Share This Page