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.
Difference between revisions of "EclipseLink/Examples/Foundation/Logging"
(New page: == How do I integrate with a third party logging framework == EclipseLink's default logging is based on Java (1.4 an higher) java.util.logging but is also extensible to allow other loggin...) |
(→Using Log4J) |
||
(17 intermediate revisions by the same user not shown) | |||
Line 5: | Line 5: | ||
__TOC__ | __TOC__ | ||
− | <div style="border:1px solid #999999;background-color:# | + | <div style="border:1px solid #999999;background-color:#eeeeee"> |
− | + | ''' EclipseLink User Guide References ''' | |
* [[Introduction_to_EclipseLink_Sessions_(ELUG)#Logging | Logging Overview]] | * [[Introduction_to_EclipseLink_Sessions_(ELUG)#Logging | Logging Overview]] | ||
* [[Configuring_a_Session_(ELUG)#Configuring_Logging | Logging configuration on a native session using Workbench and Java ]] | * [[Configuring_a_Session_(ELUG)#Configuring_Logging | Logging configuration on a native session using Workbench and Java ]] | ||
Line 14: | Line 14: | ||
This how-to example will illustrate how other logging solutions such as Log4J and Apache common logging can be easily used with EclipseLink. | This how-to example will illustrate how other logging solutions such as Log4J and Apache common logging can be easily used with EclipseLink. | ||
+ | ===References=== | ||
+ | [[EclipseLink/Examples/JPA/CustomLogger|How to configure a custom logger in JPA]] | ||
== Logging Extension Basics == | == Logging Extension Basics == | ||
− | + | Customizing EclipseLink to use another logging framework or integrate with an existing logging infrastructure involves: | |
+ | |||
+ | # Implementing your own [http://www.eclipse.org/eclipselink/api/1.0/org/eclipse/persistence/logging/SessionLog.html SessionLog] | ||
+ | # Configuring the use of your SessionLog | ||
== Using Log4J == | == Using Log4J == | ||
− | + | Two bug reports exist for that point : | |
+ | # [https://bugs.eclipse.org/bugs/show_bug.cgi?id=232240 : apache commons logging & log4J integration] | ||
+ | # [https://bugs.eclipse.org/bugs/show_bug.cgi?id=247852 : documentation] | ||
+ | |||
+ | The first deals about apache commons Logging 1.1 & Log4J integration with eclipseLink 1.x and the second about Log4J integration only. | ||
+ | |||
+ | Both propose custom implementations for the abstract SessionLog class. | ||
+ | |||
+ | |||
+ | |||
+ | '''How to use Log4J :''' | ||
+ | |||
+ | - add apache commons logging 1.1 & log4J libraries in your classpath | ||
+ | |||
+ | - add the sample log4.xml in your classpath [https://bugs.eclipse.org/bugs/attachment.cgi?id=103596 : log4j.xml template] | ||
+ | |||
+ | - add the following classes in the org.eclipse.persistence.logging package of your own project | ||
+ | [https://bugs.eclipse.org/bugs/attachment.cgi?id=135503 : CommonsLoggingSessionLog class] | ||
+ | [https://bugs.eclipse.org/bugs/attachment.cgi?id=135505 : FastLogFormatter class] | ||
+ | |||
+ | - add the property '''eclipselink.logging.logger''' to your persistence.xml : | ||
+ | <pre> | ||
+ | |||
+ | <?xml version="1.0" encoding="UTF-8"?> | ||
+ | <persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" | ||
+ | xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
+ | xsi:schemaLocation="http://java.sun.com/xml/ns/persistence | ||
+ | http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"> | ||
+ | |||
+ | <persistence-unit name="myPersistenceUnit" transaction-type="RESOURCE_LOCAL"> | ||
+ | <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider> | ||
+ | <properties> | ||
+ | <!-- custom SessionLog implementation to use apache commons logging 1.1 API (so log4J) --> | ||
+ | <property name="eclipselink.logging.logger" value="org.eclipse.persistence.logging.CommonsLoggingSessionLog"/> | ||
+ | </properties> | ||
+ | ... | ||
+ | ... | ||
+ | </persistence-unit> | ||
+ | </persistence> | ||
+ | |||
+ | |||
+ | <property name="eclipselink.logging.logger" value="org.eclipse.persistence.logging.CommonsLoggingSessionLog"/> | ||
+ | </pre> | ||
+ | |||
+ | - Compile & deploy your code : that's done. | ||
+ | |||
+ | |||
+ | |||
+ | '''About log4J levels & eclipselink log levels :''' | ||
+ | |||
+ | Altough the log4j.xml defines a log4J level for the 'org.eclipse.persistence' logger, the CommonsLoggingSessionLog overrides that level at runtime to be compatible with the chosen eclipselink.logging.level : | ||
+ | |||
+ | For example, in log4j.xml : | ||
+ | <pre> | ||
+ | <logger name="org.eclipse.persistence"> | ||
+ | <level value="info"/> | ||
+ | </logger> | ||
+ | </pre> | ||
+ | |||
+ | |||
+ | - add the property '''eclipselink.logging.level''' to your persistence.xml : | ||
+ | <pre> | ||
+ | |||
+ | <?xml version="1.0" encoding="UTF-8"?> | ||
+ | <persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" | ||
+ | xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
+ | xsi:schemaLocation="http://java.sun.com/xml/ns/persistence | ||
+ | http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"> | ||
+ | |||
+ | <persistence-unit name="myPersistenceUnit" transaction-type="RESOURCE_LOCAL"> | ||
+ | <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider> | ||
+ | <properties> | ||
+ | <!-- | ||
+ | # ----------------------------------------------------------------------------- | ||
+ | # Logging level : | ||
+ | # OFF \u2013 disables logging | ||
+ | # SEVERE \u2013 logs exceptions indicating EclipseLink cannot continue, as well as any exceptions generated during login. This includes a stack trace. | ||
+ | # WARNING \u2013 logs exceptions that do not force EclipseLink to stop, including all exceptions not logged with severe level. This does not include a stack trace. | ||
+ | # INFO \u2013 logs the login/logout per sever session, including the user name. After acquiring the session, detailed information is logged. | ||
+ | # CONFIG \u2013 logs only login, JDBC connection, and database information. | ||
+ | # FINE \u2013 logs SQL. | ||
+ | # FINER \u2013 similar to warning. Includes stack trace. | ||
+ | # FINEST \u2013 includes additional low level information. | ||
+ | # | ||
+ | # ----------------------------------------------------------------------------- | ||
+ | --> | ||
+ | <property name="eclipselink.logging.level" value="severe" /> | ||
+ | <!-- custom SessionLog implementation to use apache commons logging 1.1 API (so log4J) --> | ||
+ | <property name="eclipselink.logging.logger" value="org.eclipse.persistence.logging.CommonsLoggingSessionLog"/> | ||
+ | </properties> | ||
+ | ... | ||
+ | ... | ||
+ | </persistence-unit> | ||
+ | </persistence> | ||
+ | </pre> | ||
+ | |||
+ | However, you should check that your root logger and appenders are well defined to produce log outputs (console, files or whatever appender ...) | ||
+ | |||
+ | |||
+ | |||
+ | '''Change Log levels at runtime :''' | ||
+ | As you may not know, log4J levels can be changed at runtime for debug purposes : see the CommonsLoggingSessionLog#setLevel() method : | ||
+ | |||
+ | <pre> | ||
+ | /** | ||
+ | * PUBLIC: Set the log level to a logger with name space extracted from the given category. | ||
+ | * | ||
+ | * @param level value according to the java.util.logging.Levels | ||
+ | * @param category category | ||
+ | */ | ||
+ | @Override | ||
+ | public final void setLevel(final int level, final String category) | ||
+ | </pre> | ||
== Using Apache Commons Logging == | == Using Apache Commons Logging == | ||
− | + | An eclipselink bug report exists : | |
+ | [https://bugs.eclipse.org/bugs/show_bug.cgi?id=232240 : apache commons Logging 1.1 & Log4J integration] | ||
+ | |||
+ | It deals about apache commons Logging 1.1 & Log4J integration with eclipseLink 1.x. | ||
+ | |||
+ | |||
+ | To use Apache Commons Logging without Apache Log4J, you can get inspired by this class : | ||
+ | [https://bugs.eclipse.org/bugs/attachment.cgi?id=135503 : CommonsLoggingSessionLog class] | ||
+ | |||
+ | |||
+ | Just remove any reference to org.apache.log4j classes ... | ||
+ | |||
+ | Please contribute to eclipselink then. | ||
+ | |||
+ | == References == | ||
+ | *http://wiki.eclipse.org/EclipseLink/Examples/JPA/CustomLogger | ||
+ | *http://bugs.eclipse.org/247852 |
Latest revision as of 20:56, 9 November 2011
How do I integrate with a third party logging framework
EclipseLink's default logging is based on Java (1.4 an higher) java.util.logging but is also extensible to allow other logging frameworks to be used.
Contents
EclipseLink User Guide References
This how-to example will illustrate how other logging solutions such as Log4J and Apache common logging can be easily used with EclipseLink.
References
How to configure a custom logger in JPA
Logging Extension Basics
Customizing EclipseLink to use another logging framework or integrate with an existing logging infrastructure involves:
- Implementing your own SessionLog
- Configuring the use of your SessionLog
Using Log4J
Two bug reports exist for that point :
The first deals about apache commons Logging 1.1 & Log4J integration with eclipseLink 1.x and the second about Log4J integration only.
Both propose custom implementations for the abstract SessionLog class.
How to use Log4J :
- add apache commons logging 1.1 & log4J libraries in your classpath
- add the sample log4.xml in your classpath : log4j.xml template
- add the following classes in the org.eclipse.persistence.logging package of your own project : CommonsLoggingSessionLog class : FastLogFormatter class
- add the property eclipselink.logging.logger to your persistence.xml :
<?xml version="1.0" encoding="UTF-8"?> <persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"> <persistence-unit name="myPersistenceUnit" transaction-type="RESOURCE_LOCAL"> <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider> <properties> <!-- custom SessionLog implementation to use apache commons logging 1.1 API (so log4J) --> <property name="eclipselink.logging.logger" value="org.eclipse.persistence.logging.CommonsLoggingSessionLog"/> </properties> ... ... </persistence-unit> </persistence> <property name="eclipselink.logging.logger" value="org.eclipse.persistence.logging.CommonsLoggingSessionLog"/>
- Compile & deploy your code : that's done.
About log4J levels & eclipselink log levels :
Altough the log4j.xml defines a log4J level for the 'org.eclipse.persistence' logger, the CommonsLoggingSessionLog overrides that level at runtime to be compatible with the chosen eclipselink.logging.level :
For example, in log4j.xml :
<logger name="org.eclipse.persistence"> <level value="info"/> </logger>
- add the property eclipselink.logging.level to your persistence.xml :
<?xml version="1.0" encoding="UTF-8"?> <persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"> <persistence-unit name="myPersistenceUnit" transaction-type="RESOURCE_LOCAL"> <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider> <properties> <!-- # ----------------------------------------------------------------------------- # Logging level : # OFF \u2013 disables logging # SEVERE \u2013 logs exceptions indicating EclipseLink cannot continue, as well as any exceptions generated during login. This includes a stack trace. # WARNING \u2013 logs exceptions that do not force EclipseLink to stop, including all exceptions not logged with severe level. This does not include a stack trace. # INFO \u2013 logs the login/logout per sever session, including the user name. After acquiring the session, detailed information is logged. # CONFIG \u2013 logs only login, JDBC connection, and database information. # FINE \u2013 logs SQL. # FINER \u2013 similar to warning. Includes stack trace. # FINEST \u2013 includes additional low level information. # # ----------------------------------------------------------------------------- --> <property name="eclipselink.logging.level" value="severe" /> <!-- custom SessionLog implementation to use apache commons logging 1.1 API (so log4J) --> <property name="eclipselink.logging.logger" value="org.eclipse.persistence.logging.CommonsLoggingSessionLog"/> </properties> ... ... </persistence-unit> </persistence>
However, you should check that your root logger and appenders are well defined to produce log outputs (console, files or whatever appender ...)
Change Log levels at runtime : As you may not know, log4J levels can be changed at runtime for debug purposes : see the CommonsLoggingSessionLog#setLevel() method :
/** * PUBLIC: Set the log level to a logger with name space extracted from the given category. * * @param level value according to the java.util.logging.Levels * @param category category */ @Override public final void setLevel(final int level, final String category)
Using Apache Commons Logging
An eclipselink bug report exists : : apache commons Logging 1.1 & Log4J integration
It deals about apache commons Logging 1.1 & Log4J integration with eclipseLink 1.x.
To use Apache Commons Logging without Apache Log4J, you can get inspired by this class :
: CommonsLoggingSessionLog class
Just remove any reference to org.apache.log4j classes ...
Please contribute to eclipselink then.