| <html><head> |
| <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> |
| <title>7. Custom Log</title><base href="display"><link rel="stylesheet" type="text/css" href="css/docbook.css"><meta name="generator" content="DocBook XSL Stylesheets V1.79.1"><link rel="home" href="manual.html" title="Apache OpenJPA 3.0 User's Guide"><link rel="up" href="ref_guide_logging.html" title="Chapter 3. Logging and Auditing"><link rel="prev" href="ref_guide_logging_slf4j.html" title="6. SLF4J"><link rel="next" href="ref_guide_audit.html" title="8. OpenJPA Audit"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">7. |
| Custom Log |
| </th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ref_guide_logging_slf4j.html">Prev</a> </td><th width="60%" align="center">Chapter 3. |
| Logging and Auditing |
| </th><td width="20%" align="right"> <a accesskey="n" href="ref_guide_audit.html">Next</a></td></tr></table><hr></div><div class="section" id="ref_guide_logging_custom"><div class="titlepage"><div><div><h2 class="title" style="clear: both">7. |
| Custom Log |
| </h2></div></div></div> |
| |
| <a class="indexterm" name="d5e9404"></a> |
| <p> |
| 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. |
| </p> |
| <p> |
| A custom logging framework must include an implementation of the |
| <a class="ulink" href="../../apidocs/org/apache/openjpa/lib/log/LogFactory.html" target="_top"><code class="classname"> |
| org.apache.openjpa.lib.log.LogFactory</code></a> interface. We present |
| a custom <code class="classname">LogFactory</code> below. |
| </p> |
| <div class="example" id="ref_guide_logging_custom_ex"><p class="title"><b>Example 3.6. |
| Custom Logging Class |
| </b></p><div class="example-contents"> |
| |
| <pre class="programlisting"> |
| 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); |
| } |
| }; |
| } |
| } |
| </pre> |
| </div></div><br class="example-break"> |
| <p> |
| To make OpenJPA use your custom log factory, set the |
| <a class="link" href="ref_guide_conf_openjpa.html#openjpa.Log" title="5.44. openjpa.Log"><code class="literal">openjpa.Log</code></a> configuration |
| property to your factory's full class name. Because this property is a plugin |
| property (see <a class="xref" href="ref_guide_conf_plugins.html" title="4. Plugin Configuration">Section 4, “ |
| Plugin Configuration |
| ”</a> ), 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 <code class="literal">openjpa.Log |
| </code> property to the following string: |
| </p> |
| <pre class="programlisting"> |
| com.xyz.CustomLogFactory(Prefix="LOG MSG") |
| </pre> |
| </div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ref_guide_logging_slf4j.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="ref_guide_logging.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="ref_guide_audit.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">6. |
| SLF4J |
| </td><td width="20%" align="center"><a accesskey="h" href="manual.html">Home</a></td><td width="40%" align="right" valign="top"> 8. OpenJPA Audit</td></tr></table></div></body></html> |