Question

I have this program as shown below , right now its only printng the stacktrace . my question is that , is it possible to get the stack trace and also a custom field , here in my case i need 1090099

Please tell me if its possible ??

package com;
import org.apache.log4j.Logger;
public class Test {

    private static final Logger logger = Logger.getLogger(Test.class);
    public static void main(String args[]) {
        try {
    String accountid = "1090099";
            String desc = null;
            System.out.println(desc.toUpperCase());
            } 
            catch (Exception t) 
        {
            logger.fatal("Exception inside the Test program  ", t);
        }
    }
}

2013-06-26 21:44:29,723[main] FATAL(Test.java:<main>:16)- Exception inside the Test program
java.lang.NullPointerException
    at com.Test.main(Test.java:12)
Was it helpful?

Solution 3

Yes, you can print the value as long as it's in scope.

String accountid = null;
try {
     accountid = "1090099";
     String desc = null;
     System.out.println(desc.toUpperCase());
 } catch (Exception t) {
     logger.fatal("Exception inside the Test program  " + accountid, t);
 } 

Also, I would suggest using logger.debug instead of system.out.println for your other logging calls...

OTHER TIPS

You have to include it manually in the message you're logging. But it looks to me like what you're really looking for is the MDC (mapped diagnostic context), a way to store values in a thread-local "context" that can then be used to distinguish between log messages relating to different application-level entities.

package com;
import org.apache.log4j.*;
public class Test {

    private static final Logger logger = Logger.getLogger(Test.class);
    public static void main(String args[]) {
        MDC.put("accountid", "1090099");
        try {
            String desc = null;
            System.out.println(desc.toUpperCase());
            } 
            catch (Exception t) 
        {
            logger.fatal("Exception inside the Test program  ", t);
        } finally {
            MDC.remove("accountid");
        }
    }
}

You would then include %X{accountid} somewhere in your appender's layout pattern and it would include the appropriate MDC entry in every log message, including those logged by third-party code that you call.

I would create my own Exception class, with members to hold the additional information, and a suitable toString() method that displays them. Wrap the original Exception in your custom Exception and add the information you want preserved.

You are close to achieving that.

In your case you will have to declare the accountid outside the try block and then you can append the accountid along with your Exception inside the Test program message`

String accountid = ""; 

try {
        accountid = "1090099";
        String desc = null;
        System.out.println(desc.toUpperCase());
    } 
        catch (Exception t) 
    {
        logger.fatal("Exception inside the Test program.\nAccount ID: " + accountid, t);
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top