Skip to main content

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.

Jump to: navigation, search

EclipseLink/Examples/JPA/CustomLogger


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
    }
}

For more information, another wiki page gives detailed examples : How do I integrate with a third party logging framework


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); // Logging level finest
  aSession.setSessionLog(aCustomLogger);
  }
}

Note: The rest of the following properties can be set, as well you will want to print out more detail as I have just printed out the raw untranslated, undecoded message.

sessionLog	CustomAbstractSessionLog  (id=130)	
	dateFormat	DateFormatThreadLocal  (id=215)	
	level	1	
	session	ServerSession  (id=128)	
	shouldLogExceptionStackTrace	null	
	shouldPrintConnection	null	
	shouldPrintDate	null	
	shouldPrintSession	null	
	shouldPrintThread	null

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

The example app does some inserts of new JPA Entities on the EntityManager and commits the session to write them to the database.

[EL Finest]: 2008.09.12 13:59:36.330--ServerSession(6386542)--Thread(Thread[main,5,main])--property=eclipselink.jdbc.url; value=jdbc:oracle:thin:@x.x.x.x: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: eclipseLink_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

References

EclipseLink User Guide : Logging JPA Extensions

Originated: Michael O'Brien against EclipseLink 1.0 build 20080910

Back to the top