問題
ビジネスロジックでExceptionが発生すると、TerasolunaのログインターセプタがExceptionの内容を出力しますが、stackTraceを出力しません。
これではエラーの原因調査がしづらいです。
これは、TerasolunaのログインターセプタBLogicLogInterceptor
のメソッドで、error(Object message)
メソッドを実行しているからです。error(Object message, Throwable t)
メソッドを実行すれば、stackTraceが出力されます。
jp.terasoluna.fw.ex.aop.log.BLogicLogInterceptor.java
/**
* BLogicクラスの各メソッドの実行前後に、デバッグログを出力します。
* また、メソッド実行中に例外が発生した場合、エラーログを出力します。
*
* @param invocation インタセプタ対象のオブジェクト
* @return インタセプタ適用後のオブジェクト
* @throws Throwable 実行例外
*
* @see org.aopalliance.intercept.MethodInterceptor#
* invoke(org.aopalliance.intercept.MethodInvocation)
*/
public Object invoke(MethodInvocation invocation) throws Throwable {
Class<? extends Object> blogicClass = null;
if (logger.isDebugEnabled()) {
blogicClass = invocation.getThis().getClass();
logger.debug(blogicClass.getSimpleName().concat(
" is being executed..."));
logger.debug(createParamsInfo(invocation.getArguments()));
}
Object result = null;
try {
result = invocation.proceed();
} catch (Throwable e) {
if (checkException(e)) {
logger.error(e); //【exception出力】
throw e;
} else {
logger.debug(e); //【exception出力】
throw e;
}
}
if (logger.isDebugEnabled()) {
logger.debug(createResultInfo(result));
logger.debug(blogicClass.getSimpleName().concat(
" was executed completely."));
}
return result;
}
対応
カスタムログインターセプタは作成して、「ビジネスロジック終了⇒カスタムログインターセプタ⇒BLogicLogInterceptor」と呼ばれるようにしました。
カスタムログインターセプタでは、error(Object message, Throwable t)
を実行して、stackTraceを出力しています。
applicationContext.xml
<!-- Terasolunaのログインタセプタの定義 -->
<bean id="blogicLogInterceptor"
class="jp.terasoluna.fw.ex.aop.log.BLogicLogInterceptor" >
<property name="noErrorLogExceptionLists">
<list>
<value>jp.terasoluna.fw.web.thin.UnauthenticatedException</value>
</list>
</property>
</bean>
<aop:config>
<aop:pointcut id="blogicBeans" expression="bean(*BLogic)" />
<aop:advisor pointcut-ref="blogicBeans" advice-ref="blogicLogInterceptor" />
</aop:config>
<!-- カスタムログ出力用インタセプタの定義 -->
<bean id="myBlogicLogInterceptor"
class="jp.co.sample.common.interceptor.MyBLogicLogInterceptor" />
<aop:config>
<aop:pointcut id="myBlogicLogBeans" expression="bean(*BLogic)" />
<aop:advisor pointcut-ref="myBlogicLogBeans" advice-ref="myBlogicLogInterceptor" />
</aop:config>
jp.co.sample.common.interceptor.MyBLogicLogInterceptor.java
public Object invoke(MethodInvocation invocation) throws Throwable {
Object result = null;
try {
result = invocation.proceed();
} catch (Throwable e) {
logger.error("", e); //messageを空文字にする
throw e;
}
return result;
}
余談
修正願いを出したいんだけど、GitHubの使い方が分からない...
環境
TERASOLUNA Server Framework for Java 2.0.5.3
https://osdn.jp/projects/terasoluna/releases/63023