blob: 80e491e5bb739c91349f7281174a574e4ebe7876 [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 Feb 18, 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>
Quick start - 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="20120218" />
<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="#">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 class="active">
<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="externalLink">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 active">
<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">
<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="externalLink" target="_blank" title="Wiki">Wiki</a>
</li>
<li class="none">
<a href="http://blogs.apache.org/logging/" class="externalLink" 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="externalLink" target="_blank" title="Home">Home</a>
</li>
<li class="none">
<a href="http://www.apache.org/foundation/sponsorship.html" class="externalLink" target="_blank" title="Sponsorship">Sponsorship</a>
</li>
<li class="none">
<a href="http://www.apache.org/licenses/" class="externalLink" target="_blank" title="License">License</a>
</li>
<li class="none">
<a href="http://www.apache.org/foundation/thanks.html" class="externalLink" target="_blank" title="Thanks">Thanks</a>
</li>
<li class="none">
<a href="http://www.apachecon.com" class="externalLink" target="_blank" title="Conferences">Conferences</a>
</li>
<li class="none">
<a href="http://www.apache.org/security/" class="externalLink" 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>Quick start<a name="Quick_start"></a></h2>
<p>First, <a href="install.html">install Apache log4php</a>.</p>
<p>You may also like to read the <a href="docs/introduction.html">introduction chapter</a> to familiarise
yoursef with the basic concepts used throughout the documentation and examples.</p>
<div class="section"><h3>A trivial example<a name="A_trivial_example"></a></h3>
<p>Just want logging to stdout?</p>
<div class="prettyprint linenums"><pre>
include('log4php/Logger.php');
$logger = Logger::getLogger(&quot;main&quot;);
$logger-&gt;info(&quot;foo&quot;);
$logger-&gt;warn(&quot;bar&quot;);
</pre></div>
<p>This produces the following output:</p>
<div><pre>
Sun Jul 26 01:40:23 2009,021 [10093] INFO main - foo
Sun Jul 26 01:40:23 2009,030 [10093] WARN main - bar
</pre></div>
</div>
<div class="section"><h3>A simple example<a name="A_simple_example"></a></h3>
<p>This example demonstrates how to log messages to a file, but only with level greater or equal to
WARN.</p>
<p>Create a configuration file named <tt>log4php.xml</tt> containing:</p>
<div class="prettyprint linenums"><pre>
&lt;configuration xmlns=&quot;http://logging.apache.org/log4php/&quot;&gt;
&lt;appender name=&quot;myAppender&quot; class=&quot;LoggerAppenderFile&quot;&gt;
&lt;param name=&quot;file&quot; value=&quot;myLog.log&quot; /&gt;
&lt;/appender&gt;
&lt;root&gt;
&lt;level value=&quot;WARN&quot; /&gt;
&lt;appender_ref ref=&quot;myAppender&quot; /&gt;
&lt;/root&gt;
&lt;/configuration&gt;
</pre></div>
<p>This configuration file:</p>
<ul>
<li>Creates an appender named <tt>myAppender</tt> using appender class <tt>
<a href="docs/appenders.html#LoggerAppenderFile">LoggerAppenderFile</a></tt> which is used for
logging to a file (on line 2).</li>
<li>Sets the <tt>file</tt> parameter, which is required for LoggerAppenderFile, to the path
to the file in which events will be logged (on line 3).</li>
<li>Sets the root logger level to <tt>WARN</tt> (on line 6)). This means that logging requests
with the level lower than WARN will be ignored.</li>
<li>Links <tt>myAppender</tt> to the root logger (on line 7) so that all events recieved by the root
logger will be forwarded to <tt>myAppender</tt> and written into the log file.</li>
</ul>
<p>To try it out, run the following code:</p>
<div class="prettyprint linenums"><pre>
// Insert the path where you unpacked log4php
include('log4php/Logger.php');
// Tell log4php to use our configuration file.
Logger::configure('log4php.xml');
// Fetch a logger, it will inherit settings from the root logger
$log = Logger::getLogger('myLogger');
// Start logging
$log-&gt;trace(&quot;My first message.&quot;); // Not logged because TRACE &lt; WARN
$log-&gt;debug(&quot;My second message.&quot;); // Not logged because DEBUG &lt; WARN
$log-&gt;info(&quot;My third message.&quot;); // Not logged because INFO &lt; WARN
$log-&gt;warn(&quot;My fourth message.&quot;); // Logged because WARN &gt;= WARN
$log-&gt;error(&quot;My fifth message.&quot;); // Logged because ERROR &gt;= WARN
$log-&gt;fatal(&quot;My sixth message.&quot;); // Logged because FATAL &gt;= WARN
</pre></div>
<p>This will create a file named <tt>myLog.log</tt> containing the following output:</p>
<div><pre>
WARN - My fourth message.
ERROR - My fifth message.
FATAL - My sixth message.
</pre></div>
</div>
<div class="section"><h3>An advanced example<a name="An_advanced_example"></a></h3>
<p>This example covers named loggers, layouts and best practices in object-oriented programming.</p>
<p>Create a configuration file named <tt>log4php.xml</tt> with the following content:</p>
<div class="prettyprint linenums"><pre>
&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;configuration xmlns=&quot;http://logging.apache.org/log4php/&quot;&gt;
&lt;appender name=&quot;myConsoleAppender&quot; class=&quot;LoggerAppenderConsole&quot; /&gt;
&lt;appender name=&quot;myFileAppender&quot; class=&quot;LoggerAppenderFile&quot;&gt;
&lt;layout class=&quot;LoggerLayoutTTCC&quot; /&gt;
&lt;param name=&quot;file&quot; value=&quot;myLog.log&quot; /&gt;
&lt;/appender&gt;
&lt;logger name=&quot;Foo&quot;&gt;
&lt;appender_ref ref=&quot;myFileAppender&quot; /&gt;
&lt;/logger&gt;
&lt;root&gt;
&lt;level value=&quot;DEBUG&quot; /&gt;
&lt;appender_ref ref=&quot;myConsoleAppender&quot; /&gt;
&lt;/root&gt;
&lt;/configuration&gt;
</pre></div>
<p>The configuration defines two appenders: one writes to the console, and the other to a file.</p>
<p>The
console appender doesn't have a layout defined, so it will revert to default layout
(<tt><a href="docs/layouts/simple.html">LoggerLayoutSimple</a></tt>). The
file appender uses a different layout
(<tt><a href="docs/layouts/ttcc.html">LoggerLayoutTTCC</a></tt>)
which will result in different formatting of the logging
events.</p>
<p>The console appender is linked to the root logger. The file appender is linked to the logger named
<tt>Foo</tt>, however <tt>Foo</tt> also inherits appenders from the root logger (in this case
the console appender). This means that logging events sent to the <tt>Foo</tt> logger will be
logged both to the console and the file.</p>
<p>Consider the following code snippet:</p>
<div class="prettyprint linenums"><pre>
// Include and configure log4php
include('log4php/Logger.php');
Logger::configure('log4php.xml');
/**
* This is a classic usage pattern: one logger object per class.
*/
class Foo
{
/** Holds the Logger. */
private $log;
/** Logger is instantiated in the constructor. */
public function __construct()
{
// The __CLASS__ constant holds the class name, in our case &quot;Foo&quot;.
// Therefore this creates a logger named &quot;Foo&quot; (which we configured in the config file)
$this-&gt;log = Logger::getLogger(__CLASS__);
}
/** Logger can be used from any member method. */
public function go()
{
$this-&gt;log-&gt;info(&quot;We have liftoff.&quot;);
}
}
$foo = new Foo();
$foo-&gt;go();
</pre></div>
<p>This produces the following output in the console:</p>
<div><pre>INFO - We have liftoff.</pre></div>
<p>And the following in the log file:</p>
<div><pre>01/06/11 18:43:39,545 [5428] INFO Foo - We have liftoff.</pre></div>
<p>Note the different layout, this is because LoggerLayoutTTCC was used as layout for the file appender.</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>
</div>
</body>
</html>