| <!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> |
| Configuration - 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 ™</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="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"> |
| <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 active"> |
| <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>Configuration<a name="Configuration"></a></h2> |
| |
| <p>Most components of log4php have various settings which determing their behaviour. They can all be |
| configured programatically, but a much more common way is by providing the configuration options |
| in a file.</p> |
| |
| <p>Log4php understands three configuration formats: XML, PHP and Properties, all of which are covered in |
| more details in the following sections.</p> |
| |
| <p>The configuration is passed to log4php by calling the static method <tt>Logger::configure()</tt> |
| before issuing any logging requests. In case log4php is not configured by the time a logging request is |
| issued, log4php will configure itself using the <a href="#Default_configuration">default configuration</a>.</p> |
| |
| <a name="XML"></a><div class="section"><h3 id="XML">XML</h3> |
| |
| <p>XML is the most common configuration format, and it is the most prominently featured in the |
| documentation and examples.</p> |
| |
| <p>A simple configuration looks like this:</p> |
| |
| <div class="prettyprint linenums"><pre> |
| <?xml version="1.0" encoding="UTF-8"?> |
| <configuration xmlns="http://logging.apache.org/log4php/"> |
| <appender name="default" class="LoggerAppenderFile"> |
| <layout class="LoggerLayoutTTCC" /> |
| <param name="file" value="/var/log/my.log" /> |
| <param name="append" value="true" /> |
| </appender> |
| <root> |
| <appender_ref ref="default" /> |
| </root> |
| </configuration> |
| </pre></div> |
| |
| <p>Detailed instructions on configuring each component is outlined in the corresponding compomnent's |
| documentation: |
| <a href="loggers.html">loggers</a>, |
| <a href="appenders.html">appenders</a>, |
| <a href="layouts.html">layouts</a>, |
| <a href="filters.html">filters</a>, |
| <a href="renderers.html">renderers</a> |
| </p> |
| </div> |
| |
| <a name="PHP"></a><div class="section"><h3 id="PHP">PHP</h3> |
| |
| <p>Configuration can also be stored in a PHP array. This is the format used internally by log4php. All |
| other formats are converted to a PHP array before being used by the configurator. Because of this, the |
| PHP configuration format should be used when performance is important since it will avoid the overhead |
| of parsing the ini or XML file.</p> |
| |
| <p>This format can be used in one of two ways:</p> |
| |
| <p>The configuration array can directly be passed to <tt>Logger::configure()</tt>.</p> |
| |
| <div class="prettyprint linenums"><pre> |
| Logger::configure(array( |
| 'rootLogger' => array( |
| 'appenders' => array('default'), |
| ), |
| 'appenders' => array( |
| 'default' => array( |
| 'class' => 'LoggerAppenderConsole', |
| 'layout' => array( |
| 'class' => 'LoggerLayoutSimple' |
| ) |
| ) |
| ) |
| )); |
| </pre></div> |
| |
| <p>Alternatively a file can be created which holds the PHP configuration array. The file must |
| have the <tt>php</tt> extension and it should <i>return</i> the configuration array. For example, |
| a file named <tt>config.php</tt> with the following content:</p> |
| |
| <div class="prettyprint linenums"><pre> |
| return array( |
| 'rootLogger' => array( |
| 'appenders' => array('default'), |
| ), |
| 'appenders' => array( |
| 'default' => array( |
| 'class' => 'LoggerAppenderConsole', |
| 'layout' => array( |
| 'class' => 'LoggerLayoutSimple' |
| ) |
| ) |
| ) |
| ); |
| </pre></div> |
| |
| <p>This file can then be used to configure log4php:</p> |
| |
| <div class="prettyprint"><pre>Logger::configure('config.php');</pre></div> |
| |
| <div class="alert alert-info"> |
| <p><b>Hint:</b> to translate a XML or properties configuration file to PHP, run the following code:</p> |
| <div class="prettyprint"><pre> |
| $configurator = new LoggerConfiguratorDefault(); |
| $config = $configurator->parse('/path/to/config.xml'); |
| </pre></div> |
| </div> |
| </div> |
| |
| |
| <a name="INI"></a><div class="section"><h3 id="INI">Properties (INI)</h3> |
| |
| <p>The properties configuration format is a legacy method of configuring log4php. It was inherited from |
| <a href="logging.apache.org/log4j/1.2/manual.html">Apache log4j</a> and uses the same format. The only |
| difference is that lines begin with <tt>log4php</tt> instead of <tt>log4j</tt>.</p> |
| |
| <div class="alert alert-warning"> |
| <p>This format has been deprecated. Support will not be removed for the foreseeable future, however |
| it may not be updated to include newly introduced features. It is recommended that you use either |
| the <a href="#XML">XML</a> or <a href="#PHP">PHP</a> configuration format.</p> |
| </div> |
| |
| <p>The properites configuration format does not support filters.</p> |
| |
| <p>The following is a high level overview of this format:</p> |
| |
| |
| <div class="prettyprint"><pre> |
| # Appender named "default" |
| log4php.appender.default = LoggerAppenderEcho |
| log4php.appender.default.layout = LoggerLayoutTTCC |
| |
| # Appender named "file" |
| log4php.appender.file = LoggerAppenderDailyFile |
| log4php.appender.file.layout = LoggerLayoutPattern |
| log4php.appender.file.layout.conversionPattern = %d{ISO8601} [%p] %c: %m (at %F line %L)%n |
| log4php.appender.file.datePattern = Ymd |
| log4php.appender.file.file = target/examples/daily_%s.log |
| log4php.appender.file.threshold = warn |
| |
| # Root logger, linked to "default" appender |
| log4php.rootLogger = DEBUG, default |
| |
| # Logger named "foo", linked to "default" appender |
| log4php.logger.foo = warn, default |
| |
| # Logger named "foo.bar", linked to "file" appender |
| log4php.logger.foo.bar = debug, file |
| log4php.additivity.foo.bar = true |
| |
| # Logger named "foo.bar.baz", linked to both "file" and "default" appenders |
| log4php.logger.foo.bar.baz = trace, default, file |
| log4php.additivity.foo.bar.baz = false |
| |
| # Renderers for Fruit and Beer classes |
| log4php.renderer.Fruit = FruitRenderer |
| log4php.renderer.Beer = BeerRenderer |
| |
| # Setting base threshold |
| log4php.threshold = debug |
| </pre></div> |
| </div> |
| |
| <a name="Default_configuration"></a><div class="section"><h3 id="Default_configuration">Default configuration</h3> |
| <p>If no configuration is provided before the initial logging request is issued, log4php will configure |
| using the default configuration. This consists of a single <tt>LoggerAppenderEcho</tt> appender, |
| using <tt>LoggerLayoutTTCC</tt>, attached to the root logger and set to the DEBUG level.</p> |
| |
| <p>The default configuration in PHP format is:</p> |
| |
| <div class="prettyprint linenums"><pre> |
| array( |
| 'rootLogger' => array( |
| 'appenders' => array('default'), |
| ), |
| 'appenders' => array( |
| 'default' => array( |
| 'class' => 'LoggerAppenderConsole', |
| 'layout' => array( |
| 'class' => 'LoggerLayoutSimple' |
| ) |
| ) |
| ) |
| ) |
| </pre></div> |
| |
| <div class="alert alert-info"> |
| <p><b>Hint:</b> You can fetch the default configuration as a PHP array by running:</p> |
| <div class="prettyprint"><pre>LoggerConfiguratorDefault::getDefaultConfiguration();</pre></div> |
| </div> |
| </div> |
| |
| <div class="section"><h3>Programmatic configuration<a name="Programmatic_configuration"></a></h3> |
| |
| <p>It is possible to configure log4php fully programmatically. This requires the user to implement |
| their own configurator object. Configurators must implement the <tt>LoggerConfigurator</tt> |
| interface.</p> |
| |
| <p>Here is an example:</p> |
| |
| <div class="prettyprint linenums"><pre> |
| class MyConfigurator implements LoggerConfigurator { |
| |
| public function configure(LoggerHierarchy $hierarchy, $input = null) { |
| |
| // Create an appender which logs to file |
| $appFile = new LoggerAppenderFile('foo'); |
| $appFile->setFile('D:/Temp/log.txt'); |
| $appFile->setAppend(true); |
| $appFile->setThreshold('all'); |
| $appFile->activateOptions(); |
| |
| // Use a different layout for the next appender |
| $layout = new LoggerLayoutTTCC(); |
| $layout->setContextPrinting(false); |
| $layout->setDateFormat('%Y-%m-%d %H:%M:%S'); |
| $layout->activateOptions(); |
| |
| // Create an appender which echoes log events, using a custom layout |
| // and with the threshold set to INFO |
| $appEcho = new LoggerAppenderEcho('bar'); |
| $appEcho->setLayout($layout); |
| $appEcho->setThreshold('info'); |
| $appEcho->activateOptions(); |
| |
| // Add both appenders to the root logger |
| $root = $hierarchy->getRootLogger(); |
| $root->addAppender($appFile); |
| $root->addAppender($appEcho); |
| } |
| } |
| </pre></div> |
| |
| <p>To use the configurator, pass it as a second parameter to <tt>Logger::configure()</tt> (either |
| the name of the class as a string or an instance). Any value passed as <tt>$configuration</tt> |
| will be available in the configure() method of the LoggerConfigurator as <tt>$input</tt>.</p> |
| |
| <div class="prettyprint linenums"><pre> |
| // User defined configuration (optional) |
| $configuration = array( |
| 'foo' => 1, |
| 'bar' => 2 |
| ); |
| |
| // Passing the configurator as string |
| Logger::configure($configuration, 'MyConfigurator'); |
| |
| // Passing the configurator as an instance |
| Logger::configure($configuration, new MyConfigurator()); |
| </pre></div> |
| <div class="alert alert-warning"> |
| <p><b>Note: </b>Always call <tt>activateOptions()</tt> on all appenders, filters and layouts after setting |
| their configuration parameters. Otherwise, the configuration may not be properly activated.</p> |
| </div> |
| </div> |
| </div> |
| |
| |
| </td> |
| </tr> |
| </table> |
| </div> |
| |
| <div class="footer"> |
| <p>Copyright © 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> |