如何在java中不向server.log抛出异常

发布于 2024-09-15 19:37:17 字数 1736 浏览 4 评论 0原文

在我的应用程序中,我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

他不在意 2024-09-22 19:37:17

所有未捕获的异常都会记录到服务器日志中。如果您不希望某些内容出现在那里,请不要重新抛出 RuntimeException

如果您想停止方法执行,只需从方法返回即可。

如果您还想停止调用方方法中的执行,则不要重新抛出未经检查的异常。声明要抛出已检查异常的方法并捕获它并将其记录在调用者中。

可以为未捕获的异常设置自定义异常处理程序:

Thread.currentThread().setUncaughtExceptionHandler(yourExceptionHandler);

但我建议更改代码,而不是将此类“黑客”放置在随机位置。

相关问题: 如何配置 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:

Thread.currentThread().setUncaughtExceptionHandler(yourExceptionHandler);

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文