Notice: This Wiki is now read only and edits are no longer possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.
EclipseLink/Examples/JPA/CustomLogger
Contents
How to Configure a Custom Logger
This example will detail the minimum steps required to implement a custom logger for EclipseLink JPA.
- 1) Add the logger to the session via a SessionCustomizer
- 2) Set the level
- 3) Override log(SessionLogEntry sessionLogEntry)
This example does not use a logging framework like Apache commons or Log4J yet so that I could isolate EclipseLink behavior.
In your JPA application, add the following changes and the custom logger will get picked at the end of the deploy() after predeploy().
Before the switchover to your custom logger you will encounter 6 instantiations of the DefaultSessionLog implementation of the SessionLog interface.
There is no need to override the other log() functions as they all eventually call log(SessionLogEntry).
Custom Logger Implementation
public class CustomAbstractSessionLog extends AbstractSessionLog implements SessionLog { /* @see org.eclipse.persistence.logging.AbstractSessionLog#log(org.eclipse.persistence.logging.SessionLogEntry) */ @Override public void log(SessionLogEntry sessionLogEntry) { System.out.println("CUSTOM: " + sessionLogEntry.getMessage()); // untranslated/undecoded message_id } }
SessionCustomizer Implementation
This is the customizer that will set your custom logger on the session
public class JPAEclipseLinkSessionCustomizer implements SessionCustomizer { public void customize(Session aSession) throws Exception { // create a custom logger SessionLog aCustomLogger = new CustomAbstractSessionLog(); aCustomLogger.setLevel(1); // finest aSession.setSessionLog(aCustomLogger); } }
Reference SessionCustomizer in persistence.xml
This is the property that you want to add to persistence.xml to pick up the sessionCustomizer.
<properties> <property name="eclipselink.session.customizer" value="org.eclipse.persistence.example.unified.integration.JPAEclipseLinkSessionCustomizer"/>
Results
[EL Finest]: 2008.09.12 13:59:36.330--ServerSession(6386542)--Thread(Thread[main,5,main])--property=eclipselink.jdbc.url; value=jdbc:oracle:thin:@10.156.53.19:1521:orcl [EL Finest]: 2008.09.12 13:59:36.330--ServerSession(6386542)--Thread(Thread[main,5,main])--property=eclipselink.session.customizer; value=org.eclipse.persistence.example.unified.integration.JPAEclipseLinkSessionCustomizer
>>>Logger switchover<<<
CUSTOM: topLink_version CUSTOM: login_successful ... CUSTOM: INSERT INTO STAT_CLASS (ID, JDK_VERSION, MODIFIED, INTERNAL, LINES, TEST, CLASS_TYPE_CODE, NAME, VERSION, CLASS_TYPE, LABEL, CLASS_PACKAGE) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) bind => [8627, null, 2008-07-04, null, 159, 0, null, XMLPlatformException, concrete, null, 1626, 1366] CUSTOM: execute_query CUSTOM: execute_query CUSTOM: execute_query CUSTOM: INSERT INTO STAT_CLASS (ID, JDK_VERSION, MODIFIED, INTERNAL, LINES, TEST, CLASS_TYPE_CODE, NAME, VERSION, CLASS_TYPE, LABEL, CLASS_PACKAGE) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) bind => [7573, null, 2008-07-04, null, 117, 0, null, QueueableWeakCacheKey, concrete, null, 1626, 1242] CUSTOM: execute_query CUSTOM: execute_query .... CUSTOM: commit_transaction CUSTOM: end_unit_of_work_commit CUSTOM: resume_unit_of_work CUSTOM: release_unit_of_work CUSTOM: client_released CUSTOM: undeploy_begin CUSTOM: sequencing_disconnected CUSTOM: disconnect CUSTOM: initialize_identitymaps CUSTOM: logout_successful CUSTOM: disconnect CUSTOM: disconnect CUSTOM: undeploy_end
Originated: Michael O'Brien against EclipseLink 1.0 build 20080910