blob: cdd2c53597b82d7a35799081b541ba75781f5bf4 [file] [log] [blame]
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>7.&nbsp; Custom Log</title><base href="display"><link rel="stylesheet" type="text/css" href="css/docbook.css"><meta name="generator" content="DocBook XSL-NS Stylesheets V1.76.1"><link rel="home" href="manual.html" title="Apache OpenJPA 2.3 User's Guide"><link rel="up" href="ref_guide_logging.html" title="Chapter&nbsp;3.&nbsp; Logging and Auditing"><link rel="prev" href="ref_guide_logging_slf4j.html" title="6.&nbsp; SLF4J"><link rel="next" href="ref_guide_audit.html" title="8.&nbsp;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.&nbsp;
Custom Log
</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ref_guide_logging_slf4j.html">Prev</a>&nbsp;</td><th width="60%" align="center">Chapter&nbsp;3.&nbsp;
Logging and Auditing
</th><td width="20%" align="right">&nbsp;<a accesskey="n" href="ref_guide_audit.html">Next</a></td></tr></table><hr></div><div class="section" title="7.&nbsp; Custom Log"><div class="titlepage"><div><div><h2 class="title" style="clear: both" id="ref_guide_logging_custom">7.&nbsp;
Custom Log
</h2></div></div></div>
<a class="indexterm" name="d5e9376"></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="../javadoc/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"><a name="ref_guide_logging_custom_ex"></a><p class="title"><b>Example&nbsp;3.6.&nbsp;
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.&nbsp; 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.&nbsp; Plugin Configuration">Section&nbsp;4, &#8220;
Plugin Configuration
&#8221;</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>&nbsp;</td><td width="20%" align="center"><a accesskey="u" href="ref_guide_logging.html">Up</a></td><td width="40%" align="right">&nbsp;<a accesskey="n" href="ref_guide_audit.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">6.&nbsp;
SLF4J
&nbsp;</td><td width="20%" align="center"><a accesskey="h" href="manual.html">Home</a></td><td width="40%" align="right" valign="top">&nbsp;8.&nbsp;OpenJPA Audit</td></tr></table></div></body></html>