blob: 4c9542ef1f1cc6a58409fa39d3d2de8ad90c2859 [file] [log] [blame]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!-- Generated by Apache Maven Doxia at Oct 13, 2012 -->
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Apache log4php -
Renderers - Apache log4php</title>
<link rel="stylesheet" href="../css/bootstrap.min.css" type="text/css" />
<link rel="stylesheet" href="../css/site.css" type="text/css" />
<script type="text/javascript" src="../js/jquery.min.js"></script>
<script type="text/javascript" src="../js/bootstrap.min.js"></script>
<script type="text/javascript" src="../js/prettify.min.js"></script>
<script type="text/javascript" src="../js/site.js"></script>
<meta name="Date-Revision-yyyymmdd" content="20121013" />
<meta http-equiv="Content-Language" content="en" />
<!-- Google Analytics -->
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-26177991-1']);
_gaq.push (['_gat._anonymizeIp']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
</head>
<body class="composite">
<div class="navbar">
<div class="navbar-inner">
<div class="container-fluid">
<a class="brand" href="http://logging.apache.org/log4php">Apache log4php &trade;</a>
<ul class="nav">
<li>
<a href="../index.html" title="About">About</a>
</li>
<li>
<a href="../download.html" title="Download">Download</a>
</li>
<li>
<a href="../install.html" title="Install">Install</a>
</li>
<li>
<a href="../quickstart.html" title="Quick start">Quick start</a>
</li>
</ul>
<!-- Twitter link -->
<ul class="nav pull-right">
<li><a href="http://twitter.com/log4php/" class="external">Follow <strong>@log4php</strong></a></li>
</ul>
<!-- Google CSE Search Box -->
<form class="navbar-search pull-right" id="cref" action="http://www.google.com/cse">
<input type="hidden" name="cref" value="http://logging.apache.org/log4php/cse.xml" />
<input class="search-query pull-left" type="text" name="q" size="40" placeholder="Search" />
</form>
</div>
</div>
</div>
<div class="container-fluid">
<table class="layout-table">
<tr>
<td class="sidebar">
<div class="well sidebar-nav">
<ul class="nav nav-list">
<li class="nav-header"><i class="icon-home"></i>Apache log4php™</li>
<li class="none">
<a href="../index.html" title="About">About</a>
</li>
<li class="none">
<a href="../download.html" title="Download">Download</a>
</li>
<li class="none">
<a href="../install.html" title="Install">Install</a>
</li>
<li class="none">
<a href="../changelog.html" title="Changelog">Changelog</a>
</li>
</ul>
<ul class="nav nav-list">
<li class="nav-header"><i class="icon-book"></i>Documentation</li>
<li class="none">
<a href="../quickstart.html" title="Quick start">Quick start</a>
</li>
<li class="none">
<a href="../docs/introduction.html" title="Introduction">Introduction</a>
</li>
<li class="none">
<a href="../docs/configuration.html" title="Configuration">Configuration</a>
</li>
<li class="none">
<a href="../docs/loggers.html" title="Loggers">Loggers</a>
</li>
<li class="collapsed">
<a href="../docs/appenders.html" title="Appenders">Appenders</a>
</li>
<li class="collapsed">
<a href="../docs/layouts.html" title="Layouts">Layouts</a>
</li>
<li class="none">
<a href="../docs/filters.html" title="Filters">Filters</a>
</li>
<li class="none active">
<a href="../docs/renderers.html" title="Renderers">Renderers</a>
</li>
<li class="none">
<a href="../apidocs/index.html" title="API documentation">API documentation</a>
</li>
</ul>
<ul class="nav nav-list">
<li class="nav-header"><i class="icon-user"></i>Community</li>
<li class="none">
<a href="../volunteering.html" title="Volunteering">Volunteering</a>
</li>
<li class="none">
<a href="../contributingpatches.html" title="Contributing Patches">Contributing Patches</a>
</li>
<li class="none">
<a href="http://wiki.apache.org/logging-log4php" class="external" target="_blank" title="Wiki">Wiki</a>
</li>
<li class="none">
<a href="http://blogs.apache.org/logging/" class="external" target="_blank" title="Blog">Blog</a>
</li>
</ul>
<ul class="nav nav-list">
<li class="nav-header"><i class="icon-info-sign"></i>Project Information</li>
<li class="none">
<a href="../integration.html" title="Continuous Integration">Continuous Integration</a>
</li>
<li class="none">
<a href="../source-repository.html" title="Source Repository">Source Repository</a>
</li>
<li class="none">
<a href="../dependencies.html" title="Dependencies">Dependencies</a>
</li>
<li class="none">
<a href="../license.html" title="Project License">Project License</a>
</li>
<li class="none">
<a href="../team-list.html" title="Project Team">Project Team</a>
</li>
<li class="none">
<a href="../issue-tracking.html" title="Issue Tracking">Issue Tracking</a>
</li>
<li class="none">
<a href="../mail-lists.html" title="Mailing Lists">Mailing Lists</a>
</li>
</ul>
<ul class="nav nav-list">
<li class="nav-header"><i class="icon-cog"></i>Project Reports</li>
<li class="none">
<a href="../changes-report.html" title="Changes Report">Changes Report</a>
</li>
<li class="none">
<a href="../surefire-report.html" title="Surefire Report">Surefire Report</a>
</li>
<li class="none">
<a href="../rat-report.html" title="RAT Report">RAT Report</a>
</li>
<li class="none">
<a href="../coverage-report/index.html" title="Code Coverage">Code Coverage</a>
</li>
</ul>
<ul class="nav nav-list">
<li class="nav-header"><i class="icon-heart"></i>Apache</li>
<li class="none">
<a href="http://www.apache.org" class="external" target="_blank" title="Home">Home</a>
</li>
<li class="none">
<a href="http://www.apache.org/foundation/sponsorship.html" class="external" target="_blank" title="Sponsorship">Sponsorship</a>
</li>
<li class="none">
<a href="http://www.apache.org/licenses/" class="external" target="_blank" title="License">License</a>
</li>
<li class="none">
<a href="http://www.apache.org/foundation/thanks.html" class="external" target="_blank" title="Thanks">Thanks</a>
</li>
<li class="none">
<a href="http://www.apachecon.com" class="external" target="_blank" title="Conferences">Conferences</a>
</li>
<li class="none">
<a href="http://www.apache.org/security/" class="external" target="_blank" title="Security">Security</a>
</li>
</ul>
</div>
<div id="poweredBy">
<a href="http://maven.apache.org/" title="Built by Maven" class="poweredBy">
<img class="poweredBy" alt="Built by Maven" src="../images/logos/maven-feather.png" />
</a>
</div>
</td>
<td class="content">
<!-- Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. -->
<div class="section"><h2>Renderers<a name="Renderers"></a></h2>
<p>Apache log4php can log more than just string messages. If you try to log an object it will be converted
to a string and logged. The component which converts Objects to strings in log4php is called a
<i>renderer</i>.</p>
<div class="section"><h3>Default renderer<a name="Default_renderer"></a></h3>
<p>For example, let's say that an application uses a Person object, like this one:</p>
<div><pre class="prettyprint linenums">
class Person {
public $firstName;
public $lastName;
public $age;
}
</pre></div>
<p>If you try logging it:</p>
<div><pre class="prettyprint linenums">
$person = new Person();
$person-&gt;firstName = 'John';
$person-&gt;lastName = 'Doe';
$person-&gt;age = 37;
$logger = Logger::getLogger('main');
$logger-&gt;info(&quot;Here comes the person:&quot;);
$logger-&gt;info($person);
</pre></div>
<p>Log4php will render it using the default renderer and you will end the output will look something
like:</p>
<div><pre class="prettyprint linenums">
INFO - Here comes the person:
INFO - Person Object
(
[firstName] =&gt; John
[lastName] =&gt; Doe
[age] =&gt; 37
)
</pre></div>
</div>
<div class="section"><h3>Creating a custom renderer<a name="Creating_a_custom_renderer"></a></h3>
<p>In order to make log4php log the Person object in a more readable format, a custom renderer
class can be defined which will convert Person objects to a string suitable for logging.</p>
<p>Let's call this renderer class PersonRenderer.</p>
<div><pre class="prettyprint linenums">
/** All renderers must implement the LoggerRenderer interface. */
class PersonRenderer implements LoggerRenderer {
public function render($person) {
return &quot;{$person-&gt;firstName} {$person-&gt;lastName} ({$person-&gt;age})&quot;;
}
}
</pre></div>
<p>Now log4php has to be configured to use PersonRenderer for rendering Person objects. This is done
in the configuration file.</p>
<div class="auto-tabs">
<ul>
<li>XML</li>
<li>PHP</li>
</ul>
<div class="tab-content">
<div class="tab-pane">
<div><pre class="prettyprint linenums">
&lt;configuration xmlns=&quot;http://logging.apache.org/log4php/&quot;&gt;
&lt;renderer renderedClass=&quot;Person&quot; renderingClass=&quot;PersonRenderer&quot; /&gt;
&lt;appender name=&quot;defualt&quot; class=&quot;LoggerAppenderEcho&quot; /&gt;
&lt;root&gt;
&lt;appender_ref ref=&quot;defualt&quot; /&gt;
&lt;/root&gt;
&lt;/configuration&gt;
</pre></div>
</div>
<div class="tab-pane">
<div><pre class="prettyprint linenums">
array(
'renderers' =&gt; array(
array(
'renderedClass' =&gt; 'Person',
'renderingClass' =&gt; 'PersonRenderer'
)
),
'appenders' =&gt; array(
'default' =&gt; array(
'class' =&gt; 'LoggerAppenderEcho',
),
),
'rootLogger' =&gt; array(
'appenders' =&gt; array('default'),
),
)
</pre></div>
</div>
</div>
</div>
<p>If the same code is run as above, the following output is produced:</p>
<div><pre class="prettyprint linenums">
INFO - Here comes the person:
INFO - John Doe (37)
</pre></div>
<p>Which is much more readable than the default rendering.</p>
</div>
<div class="section"><h3>Overriding the default renderer<a name="Overriding_the_default_renderer"></a></h3>
<p>It is possible to set your own custom renderer as the default renderer.</p>
<p>For example, if you're not happy with the way in which the default renderer converts objects,
you can create your own renderer:</p>
<div><pre class="prettyprint linenums">
class MyRenderer implements LoggerRenderer {
public function render($input) {
return var_dump($input);
}
}
</pre></div>
<p>And set it as the default renderer in the configuration:</p>
<div class="auto-tabs">
<ul>
<li>XML</li>
<li>PHP</li>
</ul>
<div class="tab-content">
<div class="tab-pane">
<div><pre class="prettyprint linenums">
&lt;configuration xmlns=&quot;http://logging.apache.org/log4php/&quot;&gt;
&lt;defaultRenderer renderingClass=&quot;MyRenderer&quot; /&gt;
&lt;appender name=&quot;defualt&quot; class=&quot;LoggerAppenderEcho&quot; /&gt;
&lt;root&gt;
&lt;appender_ref ref=&quot;defualt&quot; /&gt;
&lt;/root&gt;
&lt;/configuration&gt;
</pre></div>
</div>
<div class="tab-pane">
<div><pre class="prettyprint linenums">
array(
'defaultRenderer' =&gt; 'MyRenderer',
'appenders' =&gt; array(
'default' =&gt; array(
'class' =&gt; 'LoggerAppenderEcho',
),
),
'rootLogger' =&gt; array(
'appenders' =&gt; array('default'),
),
)
</pre></div>
</div>
</div>
</div>
<p>Logging a Person object using this configuration will make log4php fall back to the newly
defined default renderer, which uses <tt>var_dump</tt> instead of <tt>print_r</tt>, and
the result will look like:</p>
<div><pre class="prettyprint linenums">
INFO - Here comes the person:
class Person#5 (3) {
public $firstName =&gt;
string(4) &quot;John&quot;
public $lastName =&gt;
string(3) &quot;Doe&quot;
public $age =&gt;
int(37)
}
</pre></div>
</div>
<div class="section"><h3>Class hierarchy<a name="Class_hierarchy"></a></h3>
<p>Object rendering follows the class hierarchy. </p>
<p>For example, if there is a class named Fruit, and classes Orange, Lemon and Apple all extend Fruit.
When FruitRenderer is registered as renderer for the Fruit class, all subclasses of Fruit will also
be rendered by FruitRenderer. Of course, it is possible to override this by registering OrangeRenderer
as renderer for the Orange class.</p>
</div>
</div>
</td>
</tr>
</table>
</div>
<div class="footer">
<p>Copyright &#169; 2012
<a href="http://www.apache.org">Apache Software Foundation</a>.
All Rights Reserved.
Licensed under the <a href="http://www.apache.org/licenses/LICENSE-2.0">Apache Software License, Version 2.0</a>.
Please read the <a href="../privacy.html">Privacy policy</a></p>
<p>Apache log4php, Apache, log4php, the Apache feather logo, the Apache Logging Services project logo and the Built by
Maven logo are trademarks of The Apache Software Foundation.</p>
<p>Site powered by <a class="external" href="http://twitter.github.com/bootstrap/">Twitter Bootstrap</a>. Icons from
<a class="external" href="http://glyphicons.com/">Glyphicons Free</a>.</p>
</div>
</body>
</html>