現在O/RマッパーとしてEclipseLinkを利用しています。初めてO/Rマッパーを使うので不慣れなところが色々あるのですが、ログ出力もその1つです。現在、気になっている所としては
・O/RマッパーのログはGlassFish側のログファイルではなく、SLF4J&Logbackを使って出力するログファイルへ出力したい
・誰が実行したSQLか、を出せるか
辺りです。で、とりあえず前者をトライしました。
とりあえずEclipseLinkの本家ドキュメントを中心に探ります。
◆EclipseLinkにおける基本的なLoggingに関して
http://wiki.eclipse.org/EclipseLink/Examples/JPA/Logging
◆LOG4JやApache Commons Loggingを利用している場合
http://wiki.eclipse.org/EclipseLink/Foundation/Logging
◆CustomLogger
http://wiki.eclipse.org/EclipseLink/Examples/JPA/CustomLogger
SLF4J&Logbackの場合は3つ目なのかな…?と、まずはサンプルコードプラスアルファで試してみました。
まずは「Custom Logger Implementation」と記述のある部分
package customlog;
import org.eclipse.persistence.logging.AbstractSessionLog;
import org.eclipse.persistence.logging.SessionLog;
import org.eclipse.persistence.logging.SessionLogEntry;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
…省略…
public class CustomSessionLog extends AbstractSessionLog implements SessionLog {
private static Logger logger = LoggerFactory.getLogger("customlog.CustomSessionLog");
@Override
public void log(SessionLogEntry sle) {
logger.info("ログレベル:" + sle.getLevel() + "ログメッセージ:" + sle.getMessage());
}
}
次に「SessionCustomizer Implementation」と記述のある部分
package customlog;
import org.eclipse.persistence.config.SessionCustomizer;
import org.eclipse.persistence.logging.SessionLog;
import org.eclipse.persistence.sessions.Session;
…省略…
public class JPAEclipseLinkSessionCustomizer implements SessionCustomizer{
@Override
public void customize(Session sn) throws Exception {
SessionLog aCustomLogger = new CustomSessionLog();
aCustomLogger.setLevel(SessionLog.FINENEST);
sn.setSessionLog(aCustomLogger);
}
}
あとは、persistence.xmlに
<properties>
<property name="eclipselink.session.customizer" value="customlog.JPAEclipseLinkSessionCustomizer"/>
</properties>
を追加して、とりあえず実行すると…。(logback.xmlなどは設定されてる前提です)
2012-12-04 19:18:27.407,INFO ,,,,,ログレベル:5ログメッセージ:login_successful
2012-12-04 19:18:27.408,INFO ,,,,,ログレベル:6ログメッセージ:jmx_mbean_runtime_services_registration_encountered_multiple_mbeanserver_instances
2012-12-04 19:18:27.408,INFO ,,,,,ログレベル:2ログメッセージ:jmx_mbean_runtime_services_registration_mbeanserver_print
2012-12-04 19:18:27.408,INFO ,,,,,ログレベル:6ログメッセージ:jmx_mbean_runtime_services_switching_to_alternate_mbeanserver
2012-12-04 19:18:27.408,INFO ,,,,,ログレベル:2ログメッセージ:jmx_mbean_runtime_services_registration_mbeanserver_print
2012-12-04 19:18:27.409,INFO ,,,,,ログレベル:6ログメッセージ:jmx_mbean_runtime_services_switching_to_alternate_mbeanserver
2012-12-04 19:18:27.409,INFO ,,,,,ログレベル:1ログメッセージ:registered_mbean
2012-12-04 19:18:27.409,INFO ,,,,,ログレベル:1ログメッセージ:registered_mbean
2012-12-04 19:18:27.410,INFO ,,,,,ログレベル:1ログメッセージ:jmx_mbean_classloader_in_use
2012-12-04 19:18:27.410,INFO ,,,,,ログレベル:1ログメッセージ:mbean_get_application_name
2012-12-04 19:18:27.411,INFO ,,,,,ログレベル:1ログメッセージ:mbean_get_module_name
2012-12-04 19:18:27.416,INFO ,,,,,ログレベル:2ログメッセージ:metamodel_canonical_model_class_found
2012-12-04 19:18:27.417,INFO ,,,,,ログレベル:1ログメッセージ:deploy_end
2012-12-04 19:18:31.939,INFO ,,,,,ログレベル:2ログメッセージ:client_acquired
2012-12-04 19:18:31.940,INFO ,,,,,ログレベル:2ログメッセージ:TX_bind
2012-12-04 19:18:31.940,INFO ,,,,,ログレベル:2ログメッセージ:acquire_unit_of_work_with_argument
2012-12-04 19:18:31.941,INFO ,,,,,ログレベル:1ログメッセージ:register_new_for_persist
2012-12-04 19:18:31.952,INFO ,,,,,ログレベル:2ログメッセージ:TX_beforeCompletion
2012-12-04 19:18:31.952,INFO ,,,,,ログレベル:2ログメッセージ:begin_unit_of_work_commit
2012-12-04 19:18:31.955,INFO ,,,,,ログレベル:2ログメッセージ:TX_begin
2012-12-04 19:18:31.956,INFO ,,,,,ログレベル:1ログメッセージ:execute_query
2012-12-04 19:18:31.957,INFO ,,,,,ログレベル:1ログメッセージ:acquire_connection
2012-12-04 19:18:31.957,INFO ,,,,,ログレベル:1ログメッセージ:reconnecting_to_external_connection_pool
2012-12-04 19:18:31.965,INFO ,,,,,ログレベル:3ログメッセージ:INSERT INTO MANUFACTURER (MANUFACTURER_ID, ADDRESSLINE1, ADDRESSLINE2, CITY, EMAIL, FAX, NAME, PHONE, REP, STATE, ZIP) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
bind => [274803, null, null, null, null, null, Manufacturer274803, null, null, null, null]
FINESTなので超詳細まで出てますが、とりあえず何か出ました。でも、このままだとプログラム側でログレベルを設定するようになっているので嫌です。やはりpersistence.xmlで設定された
<property name="eclipselink.logging.level" value="FINE"/>
辺りか、logback.xmlのログレベルを(EclipseLinkのログレベルにマッピングして)設定したい所です。
あとは、詳細すぎる余計なログは不要なので、トランザクション周りとSQL文だけを出力するようにしたいなぁ。
この辺りはまた継続調査です。