views:

66

answers:

1

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>
A: 

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

Bozho
I want to avoid touching my callers at this point. Because that will be a huge change for me. Changing the callers is my last option
learn_plsql
so you are saying instead of catching it and throwing it in the DAO..I should be catching it and logging it in the caller? Isn't there an alternate way or setting in log4j to not log certain type of exceptions to server.log
learn_plsql
even if there was, it would still be 'hackery'. It's best to restructure your code
Bozho
I understand that the code has problems. In the above method why is it now allowing me to throw the caught exception `SQLException` without declaring method as throws ...but it allows me to throw the uncaught exception `RunTimeException` Can I get around by throwing `SQLException` but not declare method as `throws SQLException` ?
learn_plsql
I've taken your advice and have started catching the exception up the chain. I asked another question along those lines. please see if you can help. http://stackoverflow.com/questions/3553294/ideal-error-page-for-j2ee-app
learn_plsql