Jump to: navigation, search

Difference between revisions of "EclipseLink/Examples/JPA/CustomLogger"

m (How to Configure a Custom Logger)
 
(8 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 +
[[Category:EclipseLink/Example/JPA|CustomLogger]]
 +
 
=How to Configure a Custom Logger=
 
=How to Configure a Custom Logger=
  
Line 27: Line 29:
 
}
 
}
 
</source>
 
</source>
 +
 +
For more information, another wiki page gives detailed examples
 +
[http://wiki.eclipse.org/EclipseLink/Foundation/Logging : How do I integrate with a third party logging framework]
 +
  
 
==SessionCustomizer Implementation==
 
==SessionCustomizer Implementation==
Line 37: Line 43:
 
   // create a custom logger
 
   // create a custom logger
 
   SessionLog aCustomLogger = new CustomAbstractSessionLog();
 
   SessionLog aCustomLogger = new CustomAbstractSessionLog();
   aCustomLogger.setLevel(1); // finest
+
   aCustomLogger.setLevel(1); // Logging level finest
 
   aSession.setSessionLog(aCustomLogger);
 
   aSession.setSessionLog(aCustomLogger);
 
   }
 
   }
 
}
 
}
 
</source>
 
</source>
 +
 +
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.
 +
 +
<pre>
 +
sessionLog CustomAbstractSessionLog  (id=130)
 +
dateFormat DateFormatThreadLocal  (id=215)
 +
level 1
 +
session ServerSession  (id=128)
 +
shouldLogExceptionStackTrace null
 +
shouldPrintConnection null
 +
shouldPrintDate null
 +
shouldPrintSession null
 +
shouldPrintThread null
 +
</pre>
  
 
==Reference SessionCustomizer in persistence.xml==
 
==Reference SessionCustomizer in persistence.xml==
Line 47: Line 67:
 
This is the property that you want to add to persistence.xml to pick up the sessionCustomizer.
 
This is the property that you want to add to persistence.xml to pick up the sessionCustomizer.
  
<source lang="xml>
+
<source lang="xml">
 
<properties>
 
<properties>
 
   <property name="eclipselink.session.customizer" value="org.eclipse.persistence.example.unified.integration.JPAEclipseLinkSessionCustomizer"/>
 
   <property name="eclipselink.session.customizer" value="org.eclipse.persistence.example.unified.integration.JPAEclipseLinkSessionCustomizer"/>
Line 54: Line 74:
  
 
==Results==
 
==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:@10.156.53.19:1521:orcl
+
<pre>
 +
[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
 
[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<<<
 
>>>Logger switchover<<<
  
CUSTOM: topLink_version
+
CUSTOM: eclipseLink_version
 
CUSTOM: login_successful
 
CUSTOM: login_successful
 
...
 
...
Line 86: Line 108:
 
CUSTOM: disconnect
 
CUSTOM: disconnect
 
CUSTOM: undeploy_end
 
CUSTOM: undeploy_end
 +
</pre>
 +
 +
====References====
 +
[http://wiki.eclipse.org/Using_EclipseLink_JPA_Extensions_%28ELUG%29#Using_EclipseLink_JPA_Extensions_for_Logging EclipseLink User Guide : Logging JPA Extensions]
  
 
Originated: Michael O'Brien against EclipseLink 1.0 build 20080910
 
Originated: Michael O'Brien against EclipseLink 1.0 build 20080910

Latest revision as of 11:53, 25 November 2009


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