简体   繁体   English

如何将异常堆栈跟踪插入日志

[英]how to insert exception stacktrace to log

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是的,我的日志中出现如下错误:2020-12-07 11:27:56 错误 FixProtocolConvert:162 - 修复协议转换线程中的错误: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我也必须尝试使用此代码,但我只会得到这样的结果:2020-12-07 14:09:52 错误 FixProtocolConvert:325 - 修复协议转换线程中的错误:java.lang.NullPointerException Z93F725A07423FE1C8846F448B330ZllD21F446.

how to get exactly position of this error?如何得到这个错误的准确 position? like this example of ex.printstacktrace.像 ex.printstacktrace 的这个例子。 so i need to log the value of ex.printstacktrace to my logger所以我需要将 ex.printstacktrace 的值记录到我的记录器中

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

my question is how to get exactly position of this error?我的问题是如何得到这个错误的确切 position ? because if i use that code only get log like that... thanks all因为如果我使用该代码只会得到这样的日志......谢谢大家

The last parameter passed to any log method ( info , warn , ...) will be interpreted as Exception if it is an appropriate subclass.如果它是适当的子类,则传递给任何日志方法( infowarn ,...)的最后一个参数将被解释为Exception

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 .另外,不要将e.printStackTrace()Logger一起使用。 It is sufficient to use one form of logging.使用一种形式的日志记录就足够了。 Prefer using a Logger instead of System.err , which is used by printStackTrace() .更喜欢使用Logger而不是System.err ,后者由printStackTrace()使用。

Related Questions:相关问题:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM