如何在java中不向server.log抛出异常
在我的应用程序中,我使用 log4j
并有一个 my_system.log
,所有消息都应在其中抛出。我遇到的问题是,当发生错误时,它也会显示在 server.log
中(我不希望这种情况发生)。
public String getAccessFlag (String userId, String participantCode, String roleId) {
HashMap parmMap = new HashMap();
parmMap.put("userId", userId.toUpperCase());
parmMap.put("roleId", roleId);
log.info(parmMap);
try {
getSqlMapClientOltp().queryForList("auth.getAccess", parmMap);
} catch (SQLException ex) {
log.error(ex, ex);
throw new RuntimeException (ex);
}
List result = (List)parmMap.get("Result0");
return ((String)(((HashMap) result.get(0)).get("accessVoucher"))).trim();
}
正如您所看到的,我捕获异常是因为我想将其记录在 my_system.log 中,但因为我希望执行停止(因为错误发生),所以我再次抛出错误。
我相信因为我抛出了它,所以它显示在 server.log 中。
我怎样才能避免这种情况?
更新:
以下是我的附加程序:
<appender name="myAppender" class="org.apache.log4j.AsyncAppender">
<appender-ref ref="myFile" />
</appender>
<appender name="myFile" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="../systems/my/log/my_system.log" />
<param name="Append" value="true" />
<param name="DatePattern" value="'.'yyyy-MM-dd" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%d{dd/MMM/yyyy:HH:mm:ss.SSS}]~[%-5p]~[%c:%M]~[%m]%n" />
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="TRACE#com.org.common.logging.HUDLogLevel" />
<param name="LevelMax" value="FATAL" />
</filter>
</appender>
In my application I am using log4j
and have a my_system.log
where all the messages should be thrown. Problem I have is that when an error happens it is also showing up in server.log
(I don't want this to happen).
public String getAccessFlag (String userId, String participantCode, String roleId) {
HashMap parmMap = new HashMap();
parmMap.put("userId", userId.toUpperCase());
parmMap.put("roleId", roleId);
log.info(parmMap);
try {
getSqlMapClientOltp().queryForList("auth.getAccess", parmMap);
} catch (SQLException ex) {
log.error(ex, ex);
throw new RuntimeException (ex);
}
List result = (List)parmMap.get("Result0");
return ((String)(((HashMap) result.get(0)).get("accessVoucher"))).trim();
}
As you can see, I am catching the exceptions because I want to log it in my_system.log
but because I want the execution to halt (since the error happened) I throw the error again.
I believe because I am throwing it, it is showing in server.log.
How can I avoid this ?
UPDATE:
Following are my appenders:
<appender name="myAppender" class="org.apache.log4j.AsyncAppender">
<appender-ref ref="myFile" />
</appender>
<appender name="myFile" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="../systems/my/log/my_system.log" />
<param name="Append" value="true" />
<param name="DatePattern" value="'.'yyyy-MM-dd" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%d{dd/MMM/yyyy:HH:mm:ss.SSS}]~[%-5p]~[%c:%M]~[%m]%n" />
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="TRACE#com.org.common.logging.HUDLogLevel" />
<param name="LevelMax" value="FATAL" />
</filter>
</appender>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
所有未捕获的异常都会记录到服务器日志中。如果您不希望某些内容出现在那里,请不要重新抛出
RuntimeException
。如果您想停止方法执行,只需从方法
返回
即可。如果您还想停止调用方方法中的执行,则不要重新抛出未经检查的异常。声明要抛出已检查异常的方法并捕获它并将其记录在调用者中。
您可以为未捕获的异常设置自定义异常处理程序:
但我建议更改代码,而不是将此类“黑客”放置在随机位置。
相关问题: 如何配置 log4j 不打印异常 stactrace
All uncatched exceptions will go to the server log. If you want something not to go there, don't rethrow
RuntimeException
.If you want to halt the method execution, simply
return
from the method.If you want to halt the execution in the caller method as well, then don't rethrow an unchecked exception. Declare the method to be throwing the checked exception and catch it and log it in the caller.
You can set a custom exception handler for uncaught exceptions:
But I'd advise for changing the code rather than placing such "hacks" in random places.
Related question: How can I configure log4j to not print the exception stactrace