我想在我的日誌文件中打印整個堆棧跟蹤。以下是我爲各類寫的示例代碼。日誌文件的輸出也被給出。我希望整個堆棧跟蹤以日誌形式打印。日誌不打印完整的堆棧跟蹤
在其中產生異常的第一個採樣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>
我想整個堆棧跟蹤來在日誌文件中。任何幫助將深受讚賞。
您是否嘗試過使用e.getStacktrace? –
是的,先生..我也試過,但仍然沒有打印堆棧跟蹤。 – user3681970
在執行e.getStackTrace時,它打印出如下所示:「2015年4月23日12:10:08,063 [http-bio-9999-exec-3]錯誤RetrievePasswordAction - RetrievePasswordAction中的錯誤:[Ljava.lang.StackTraceElement; @ 58c9430」 – user3681970