0

i have code like this :

catch (Exception ex) {
            // Log the error
            ex.printStackTrace();
            logger.error("Error in Best Quote Thread: " +  ex); 
        }

yes i got the error in my log like this : 2020-12-07 11:27:56 ERROR FixProtocolConvert:162 - Error in Fix Protocol Convert Thread: java.lang.NullPointerException

}catch (Exception ex) {
            // Log the error
            ex.printStackTrace();
            logger.error("Error in FixProtocolConvert: " ,  ex); 
        }

i also have to try use this code but i only get like this : 2020-12-07 14:09:52 ERROR FixProtocolConvert:325 - Error in Fix Protocol Convert Thread: java.lang.NullPointerException java.lang.NullPointerException

how to get exactly position of this error ? like this example of ex.printstacktrace. so i need to log the value of ex.printstacktrace to my logger

com.abcd.core.datafeed.fixProtocol(FixProtocolConvert.java:121)
        at 

my question is how to get exactly position of this error ? because if i use that code only get log like that... thanks all

1 Answers1

1

The last parameter passed to any log method (info, warn, ...) will be interpreted as Exception if it is an appropriate subclass.

try {
    operationThatCanFail();
catch (Exception e) {
    LOG.error("Operation failed", e); // <-- exception passed as last param 
}

If you need to use additional parameters:

try {
    operationThatCanFail();
catch (Exception e) {
    LOG.error("First {} Third {}", "Second", "Fourth", e);
}

Also, don´t use e.printStackTrace() together with a Logger. It is sufficient to use one form of logging. Prefer using a Logger instead of System.err, which is used by printStackTrace().

Related Questions:

Glains
  • 2,773
  • 3
  • 16
  • 30
  • For your example `"First {} Thrid {}", "Second", "Fourth"`, what parameters are expanded into that string, and how? – CryptoFool Dec 07 '20 at 07:19
  • `{}` acts as a placeholder, therefore it resolves to something like _ERROR [...] - First Second Third Fourth java.lang.NullPointerException [...]_. – Glains Dec 07 '20 at 07:26
  • printStackTrace() uses System.err, not System.out. – VGR Dec 07 '20 at 12:28
  • @VGR You are correct, i have edited the related part. Next time, feel free to suggest an edit yourself. – Glains Dec 07 '20 at 15:15
  • @Glains - Makes sense. I misread your example the first time...thought all of "First, Second, Third, Fourth" were placeholders. – CryptoFool Dec 07 '20 at 15:40