Skip to main content

TopLink Essentials: How to use Java Logger in Java SE mode

Posted by guruwons on September 6, 2006 at 4:06 AM PDT

TopLink Essentials(GlassFish JPA RI) can be used also in Java SE mode as you know. I will talk about how to change the default logger to java logger in this article.

When you use TopLink in Java SE, you see following log messages generated by a default logger.

[TopLink Info]: 2006.09.06 05:03:05.221--ServerSession(24529889)--
Thread(Thread[main,5,main])--TopLink, version: Oracle TopLink Essentials - 2006.8 (Build 060818)
[TopLink Info]: 2006.09.06 05:03:05.791--ServerSession(24529889)--
Thread(Thread[main,5,main])--file:/home/wonskim/works/toplink-essentials/samples/javalogger/classes-HR login successful
...

There is no direct configuration to change this default logger in Java SE. The default logger prints logs only in standard out(System.out) and you cannot log in files. How inflexible it is! You might think. Therefore I filed an RFE for this.

Workaround

But there is a workaround you can use now. Let's see how GlassFish is doing to integrate TopLink logger with its Java logger. It is done through ServerPlatform extension. ServerPlatform is used for integrating TopLink with application server (SunAS9ServerPlatform is used for GlassFish). Generally it is not used in Java SE. But with ServerPlatform you can change logger type. So by supplying custom ServerPlatform class, you can use Java logger.

See the following JavaLoggerServerPlatform class.

package toplink;

import oracle.toplink.essentials.platform.server.ServerPlatformBase;
import oracle.toplink.essentials.internal.sessions.DatabaseSessionImpl;
import oracle.toplink.essentials.internal.databaseaccess.Platform;
import oracle.toplink.essentials.logging.JavaLog;
import oracle.toplink.essentials.logging.SessionLog;

/**
* TopLink Essentials ServerPlatform class which provides Java Logger only.
* <p/>
* This code is based from <code>NoServerPlatform</code>.
*
* @author Wonseok Kim
*/
public class JavaLoggerServerPlatform extends ServerPlatformBase {

    public JavaLoggerServerPlatform(DatabaseSessionImpl newDatabaseSession) {
        super(newDatabaseSession);
        this.disableRuntimeServices();
        ;
        this.disableJTA();
    }

    public String getServerNameAndVersion() {
        return null;
    }

    public Class getExternalTransactionControllerClass() {
        return null;
    }

    public void launchContainerThread(Thread thread) {
    }

    public SessionLog getServerLog() {
        return new JavaLog();
    }

    public java.sql.Connection unwrapOracleConnection(Platform platform, java.sql.Connection connection){
        return connection;
    }

}

To compile this toplink-essentials.jar is required. Put this in your classpath and specify server platform property in persistence.xml like below.

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
             version="1.0">
  <persistence-unit name="HR">

    ...
    <properties>
      <property name="toplink.server.platform.class.name" value="toplink.JavaLoggerServerPlatform"/>
      <!-- For GlassFish v2 use the following new property -->
      <!--property name="toplink.target-server" value="toplink.JavaLoggerServerPlatform"/-->
     
      <property name="toplink.logging.level" value="FINEST"/>
    ...
    </properties>

  </persistence-unit>
</persistence>

Currently there is an issue that a ServerPlatform is intialized late, so default logger is used in early stage like below before Java logger.

[TopLink Finest]: 2006.09.06 05:06:05.523--ServerSession(24529889)--
Thread(Thread[main,5,main])--property=toplink.weaving; value=false
[TopLink Config]: 2006.09.06 05:06:05.716--ServerSession(24529889)--
Thread(Thread[main,5,main])--The alias name for the entity class [class jpatest.entities.Employee] is being defaulted to: Employee.
...
Sep 6, 2006 5:06:06 PM oracle.toplink.essentials.file:/home/wonskim/works/toplink-essentials/samples/javalogger/classes-HR
INFO: TopLink, version: Oracle TopLink Essentials - 2006.8 (Build 060818)
Sep 6, 2006 5:06:06 PM oracle.toplink.essentials.file:/home/wonskim/works/toplink-essentials/samples/javalogger/classes-HR
FINEST: DBPlatform: oracle.toplink.essentials.platform.database.PointBasePlatform, RegularExpression: (?i)pointbase.*.
Sep 6, 2006 5:06:06 PM oracle.toplink.essentials.file:/home/wonskim/works/toplink-essentials/samples/javalogger/classes-HR
...

It will be fixed in GlassFish v2.

Logging Level

Log level can be set by "toplink.logging.level" property. But it's not enough for Java logger. You need to set handler level if you want a level below INFO.

Modify JRE/lib/logging.properties file or create new one and use -Djava.util.logging.config.file=your_file.

 java.util.logging.ConsoleHandler.level = ALL

Formatter

TopLink provides also two formatters for printing out more info about its internal session.

  • oracle.toplink.essentials.logging.TopLinkSimpleFormatter
  • oracle.toplink.essentials.logging.TopLinkXMLFormatter

Configure it like below if you want more!

 java.util.logging.ConsoleHandler.formatter = oracle.toplink.essentials.logging.TopLinkSimpleFormatter

Enjoy!

Related Topics >>