Results 1 to 7 of 7
  1. #1
    Sagars is offline Member
    Join Date
    Aug 2011
    Posts
    6
    Rep Power
    0

    Default JavaLogger - putting e.printStackTrace() in logger.

    Does anybody know how to put e.printStackTrace() in logger methods.

    try{
    // exception code here
    }catch(Exception e){
    e.printStackTrace(); ==> I want to put this stack trace in Java Logger.

    /*
    Logger methods
    logger.debug(e.getMessage()) - my requirement is to put whole log message inside logger methods.
    */

    }

    Can someone help if they worked on such requirements?

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    786
    Rep Power
    5

    Default

    Which Logger class is that ? Is it from log4j ?
    Check out the javadoc: Logger (Apache Log4j 1.2.16 API)
    --> there is another debug method which you can pass a throwable : Category (Apache Log4j 1.2.16 API)
    -->
    logger.debug(e.getMessage(), e);

  3. #3
    Sagars is offline Member
    Join Date
    Aug 2011
    Posts
    6
    Rep Power
    0

    Default

    It's not the Log4j ...I am using the JavaLogger which using for e.g logError,logDebug and logWarning

    logDebug(Object ob,String Msg){

    Msg.append(ob.getClass().getName()); -- append with help of stringbuffer .. ==> Problem here we can't get a printStrackTrace()

    }

  4. #4
    Sagars is offline Member
    Join Date
    Aug 2011
    Posts
    6
    Rep Power
    0

    Default

    I know one workaround/easy way - get the PrintStackTrace in String.
    Then pass that to logDebug there is one more method - logDebug(String Msg){
    // Logging Code here.
    }

    Please suggest is there anything we can pass directly the exception object to JavaLogger methods?

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default

    What's the JavaLogger?
    The java.util.logging.Logger that comes with the JRE (since 1.4) uses an overloaded log() method:
    Java Code:
    public class Scratch {
        public static void main(String[] args) {
            Logger log = Logger.getLogger(Scratch.class.getName());
            try {
                throw new Exception("Test");
            } catch (Exception ex) {
                log.log(Level.SEVERE, ex.getMessage(), ex);
            }
        }
    }
    Which will print out the stack trace.

    If it's a different logging framework then you'll have to tell us exactly which one it is.

  6. #6
    Sagars is offline Member
    Join Date
    Aug 2011
    Posts
    6
    Rep Power
    0

    Default

    Yes I am talking about the java.util.logging.Logger but I am not sure it will work other way...can you put a code for log.log(Level.SEVERE, ex.getMessage(), ex);
    I used this method but finally to get the stack trace we should have to put a logic to get that in a string and then append everything whatever required right?

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default

    No.
    The code I posted above will print the stack trace to wherever you are logging.
    That's its job.

Similar Threads

  1. Want to know line number from printstacktrace
    By grashmi13 in forum New To Java
    Replies: 19
    Last Post: 10-18-2010, 11:39 AM
  2. A better Java logger
    By tony-walker in forum Reviews / Advertising
    Replies: 0
    Last Post: 09-09-2010, 01:32 PM
  3. printStackTrace()
    By simorgh in forum New To Java
    Replies: 2
    Last Post: 08-07-2010, 03:48 PM
  4. Help with key Logger in java
    By paul in forum Advanced Java
    Replies: 1
    Last Post: 08-07-2007, 05:14 AM
  5. printStackTrace+error
    By Freddie in forum New To Java
    Replies: 1
    Last Post: 05-31-2007, 05:43 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •