how to not throw exceptions to server.log in java
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.comm开发者_Python百科on.logging.HUDLogLevel" />
<param name="LevelMax" value="FATAL" />
</filter>
</appender>
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
精彩评论