gpt4 book ai didi

logging - 日志不打印完整的堆栈跟踪

转载 作者:行者123 更新时间:2023-12-05 09:21:50 28 4
gpt4 key购买 nike

我正在尝试在我的日志文件中打印整个堆栈跟踪。这是我为各种类(class)编写的示例代码。还给出了日志文件的输出。我希望在日志中打印整个堆栈跟踪。

生成异常的第一个示例 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>

我希望整个堆栈跟踪都出现在日志文件中。任何帮助将不胜感激。

最佳答案

记录异常以获得完整堆栈跟踪的标准方法是

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...

关于logging - 日志不打印完整的堆栈跟踪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29815307/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com