7. Custom Log
Prev | Chapter 3. Logging and Auditing | Next

## 7. Custom Log

If none of available logging systems meet your needs, you can configure the logging system with a custom logger. You might use custom logging to integrate with a proprietary logging framework used by some applications servers, or for logging to a graphical component for GUI applications.

A custom logging framework must include an implementation of the `org.apache.openjpa.lib.log.LogFactory` interface. We present a custom `LogFactory` below.

Example 3.6. Custom Logging Class

    package com.xyz;

    import org.apache.openjpa.lib.log.*;

    public class CustomLogFactory
        implements LogFactory {

        private String _prefix = "CUSTOM LOG";

        public void setPrefix(String prefix) {
            _prefix = prefix;
        }

        public Log getLog(String channel) {
            // Return a simple extension of AbstractLog that will log
            // everything to the System.err stream. Note that this is
            // roughly equivalent to OpenJPA's default logging behavior.
            return new AbstractLog() {

                protected boolean isEnabled(short logLevel) {
                    // log all levels
                    return true;
                }

                protected void log(short type, String message, Throwable t) {
                    // just send everything to System.err
                    System.err.println(_prefix + ": " + type + ": "
                        + message + ": " + t);
                }
            };
        }
    }

To make OpenJPA use your custom log factory, set the `openjpa.Log` configuration property to your factory's full class name. Because this property is a plugin property (see Section 4, “ Plugin Configuration ” ), you can also pass parameters to your factory. For example, to use the example factory above and set its prefix to "LOG MSG", you would set the `openjpa.Log` property to the following string:

    com.xyz.CustomLogFactory(Prefix="LOG MSG")

Prev | Up | Next
6. SLF4J | Home | 8. OpenJPA Audit