2015-04-23 122 views
0

我想在我的日誌文件中打印整個堆棧跟蹤。以下是我爲各類寫的示例代碼。日誌文件的輸出也被給出。我希望整個堆棧跟蹤以日誌形式打印。日誌不打印完整的堆棧跟蹤

在其中產生異常的第一個採樣DAO:

public String getPasswordByEmail(String email) throws UserException { 
    try { 
     beginNewTransaction(); 
     PreparedStatement stmt = getConnection().prepareStatement(GET_PASSWORD_BY_EMAIL); 
     stmt.setString(1, email); 

     log.debug("PrepareStatement for selecting user password by email " + stmt.toString()); 

     ResultSet rs = stmt.executeQuery(); 
     if(rs != null) { 
      rs.next(); 
     } 
     return rs.getString("password"); 
    } 
    catch(SQLException sqe) { 
     throw new UserException("Could not retrieve user details",sqe); 
    } catch (JiffieTransactionException jte) { 
     throw new UserException("Error while securing the database connection", jte); 
    } 
} 

示例服務類調用DAO:

public String getPasswordByEmail(String email) throws UserException{ 
    UserDAO userDao = new UserDAO(); 
    return userDao.getPasswordByEmail(email); 
} 

樣動作類,終於捕捉和記錄異常:

public String sendPasswordToEmail() { 
    UserService newService = new UserService(); 
    String password; 
    try { 
     password = newService.getPasswordByEmail(email); 
    } catch (UserException e) { 

     log.error("Error in RetrievePasswordAction : "+e); 
     addActionError("Email Id not registered"); 
     return "failure"; 
    } /* more codes */ 

日誌文件的輸出:

23 Apr 2015 11:40:44,755 [http-bio-9999-exec-3] ERROR RetrievePasswordAction - Error in RetrievePasswordAction : com.markEffy.aggregator.UserNotFoundException: Email id does not exist 

這裏是我的我的log4j.xml

<?xml version="1.0" encoding="UTF-8"?> 
<Configuration status="ERROR" DLog4jContextSelector="org.apache.logging.log4j.core.async.AsyncLoggerContext Selector"> 
<Appenders> 
<File name="myFile" fileName="${sys:catalina.home}/logs/Aggregator.log"> 
    <PatternLayout pattern= "%d{dd MMM yyyy HH:mm:ss,SSS} [%t] %-5p %c{1} %X{user} - %m %n %throwable{full} %n"/> 
    </File> 
    </Appenders> 
    <!-- <Policies> 
    <SizeBasedTriggeringPolicy size="5 KB"/> 
    </Policies> --> 
<!-- <DefaultRolloverStrategy max="0"/>  
</RollingRandomAccessFile> 
<Async name="AsyncRollingFile"> 
    <AppenderRef ref="RollingFile"/> 
</Async> --> 
<Console name="STDOUT" target="SYSTEM_OUT"> 
    <PatternLayout pattern="%d %-5p [%t] %C{2} (%F:%L) - %m%n%n"/> 
</Console> 

<Loggers> 
<Logger name="org.apache.log4j.xml" level="debug"> 
    <AppenderRef ref="myFile"/> 
</Logger> 
<Root level="debug"> 
    <AppenderRef ref="myFile"/> 
</Root> 
</Loggers> 

我想整個堆棧跟蹤來在日誌文件中。任何幫助將深受讚賞。

+0

您是否嘗試過使用e.getStacktrace? –

+0

是的,先生..我也試過,但仍然沒有打印堆棧跟蹤。 – user3681970

+0

在執行e.getStackTrace時,它打印出如下所示:「2015年4月23日12:10:08,063 [http-bio-9999-exec-3]錯誤RetrievePasswordAction - RetrievePasswordAction中的錯誤:[Ljava.lang.StackTraceElement; @ 58c9430」 – user3681970

回答

4

的標準方式記錄異常,以得到一個完整的堆棧跟蹤

try { 
    ... 
} catch (SomeException ex) { 
    logger.error("Could not retrieve password", ex); // separate with a comma 
} 

您的代碼段說:log.error("Error in RetrievePasswordAction : "+e)。問題在於您在錯誤消息和異常對象之間使用了加號。 加號將左側的字符串連接到右側的對象,產生新的字符串。這個新的字符串然後被記錄。這相當於:

// concatenate String+e.toString() into a new String 
String msg = "Error in RetrievePasswordAction : "+e; 
log.error(msg); // then log that new string; not what you intended... 
+0

正是我想要的。感謝您指出。 :) – user3681970

1
catch (UserException e) { 
     log.error("Error in RetrievePasswordAction : "+e); 
     for(int i= 0; i<e.getStackTrace().length; i++){ 
      log.error("+++" +e.getStackTrace()[i] +"+++"); 
     } 

我希望這可以幫助你。不是最好的解決辦法,但作品:)

+1

是的,它的工作!爲此+1。但正如你所說,我認爲一些更優化的解決方案必須可用於此。 – user3681970

+0

繼續搜索並讓我知道,因爲我也找到這樣的解決方案:) –

+0

當然,先生!我很樂意發佈解決方案,如果我有更高效的東西:) – user3681970