Location: PHPKode > projects > Lion PHP Framework > lion/documentation/Log/tutorial_Log.pkg.html
<table cellpadding="10" cellspacing="0" width="100%" border="0"><tr><td valign="top">
<div><a name=""></a><h1>The Logging facade</h1>
  <div class="ref-purpose">The log facade</div>
 <div class="ref-synopsis"></div>
<h2>Table of Contents</h2>
<ul>

<a href="../Log/tutorial_Log.pkg.html#intro">What's logging</a><br />

<a href="../Log/tutorial_Log.pkg.html#configuring_logging_facade">Configuring the Logging facade</a><br />

<a href="../Log/tutorial_Log.pkg.html#logging_facade_api">Logging facade API</a><br />
</ul>

 <p></p>
 <span><a name="intro"></a><h2>What's logging</h2><p>The Logging facade provided by the Lion Framework eases you to control the messages logged in your application.<br />
  However, it's important to note that Lion hasn't his own logging implementation since there are really excelents solutions out there. Instead of it, Lion brings a facade as an integrated way to manage logging.</p>
  <p>By default, Lion framework is configured to use <a href="http://logging.apache.org/log4php">log4php</a>, a logging framework for PHP currently undergoing incubation at the Apache Software Foundation, which support a lot of interesting logging capabilities.</p>
  <p>However, the Logging facade has been designed to work against whatever logging framework, so we are not binding our application to a concrete implementation at all</p></span>
 <span><a name="configuring_logging_facade"></a><h2>Configuring the Logging facade</h2><p>The lion.ini file (located as part of the lion distribution within the <strong>app/lion.ini</strong> file) has the following logging configuration by default:
  <div class="listing"><pre><ol><li><div class="src-line">...</div></li>
<li><div class="src-line">&nbsp;</div></li>
<li><div class="src-line"><span class="src-sym">;</span>-------------------------------------</div></li>
<li><div class="src-line"><span class="src-sym">;&nbsp;</span><span class="src-id">LOGGING&nbsp;DIRECTIVES</span>:</div></li>
<li><div class="src-line"><span class="src-sym">;</span>-------------------------------------</div></li>
<li><div class="src-line"><span class="src-id">LOG_ENABLED&nbsp;</span>=&nbsp;<span class="src-id">yes</span></div></li>
<li><div class="src-line"><span class="src-id">LOGGER_CLASS&nbsp;</span>=&nbsp;<span class="src-str">&quot;__Log4PhpLogger&quot;</span></div></li>
<li><div class="src-line"><span class="src-id">LOG4PHP_CONFIG_FILE&nbsp;</span>=&nbsp;<span class="src-str">&quot;config/log4php.properties&quot;</span></div></li>
<li><div class="src-line">&nbsp;</div></li>
<li><div class="src-line">...</div></li>
</ol></pre></div></p>
  <p>We can enable or disable the cache by altering the value of LOG_ENABLED directive. Note that you can also point to whatever facade implementation by just setting the LOGGER_CLASS directive.</p>
  <p>As soon as the default lion's logging facade work against log4php, we can configure it by setting the <i>config_log4php.properties</i> file.</p>
  <p>By default, this file contains the following settings:
  <div class="listing"><pre><ol><li><div class="src-line"><span class="src-id">log4php</span>.<span class="src-id">appender</span>.<span class="src-key">default&nbsp;</span>=&nbsp;<span class="src-id">LoggerAppenderFile</span></div></li>
<li><div class="src-line"><span class="src-id">log4php</span>.<span class="src-id">appender</span>.<span class="src-key">default</span>.<a href="http://www.php.net/file">file</a>&nbsp;=&nbsp;<span class="src-id">logs</span>/<span class="src-id">error</span>.<a href="http://www.php.net/log">log</a></div></li>
<li><div class="src-line"><span class="src-id">log4php</span>.<span class="src-id">appender</span>.<span class="src-key">default</span>.<span class="src-id">layout&nbsp;</span>=&nbsp;<span class="src-id">LoggerLayoutTTCC</span></div></li>
<li><div class="src-line"><span class="src-id">log4php</span>.<span class="src-id">rootLogger&nbsp;</span>=&nbsp;<span class="src-id">DEBUG</span><span class="src-sym">,&nbsp;</span><span class="src-key">default</span></div></li>
</ol></pre></div></p>
  <center><div class="messagebox note metadata plainlinks"><table style="width:100%; background:none"><tr><td width="60"><img alt="" src="../images/note_icon.png" width="48" height="48" border="0" /></td><td>To learn more about log4php configuration, visit the <a href="http://logging.apache.org/log4php">log4php</a> site.</td></tr></table></div></center>
  <p>This properties file set a default appender to log against the <i>logs_error.log</i>.<br />
  In the other hand, the logging level has been set to DEBUG, which means that everything will be logged within our application. However, we are able to setup the logging level as desired.</p></span>
 <span><a name="logging_facade_api"></a><h2>Logging facade API</h2><p>Logging facade provides a main instance to log: The <strong>logger</strong></p>
  <p>To retrieve the default logger associated to our application context, we just need to call the __ContextContainer::getLogger() method:
  <div class="listing"><pre><ol><li><div class="src-line"><span class="src-php">&lt;?php</span></div></li>
<li><div class="src-line">&nbsp;</div></li>
<li><div class="src-line">&nbsp;&nbsp;&nbsp;&nbsp;<span class="src-comm">//get&nbsp;the&nbsp;application&nbsp;default&nbsp;logger:</span></div></li>
<li><div class="src-line">&nbsp;&nbsp;&nbsp;&nbsp;<span class="src-var">$logger&nbsp;</span>=&nbsp;<span class="src-id"><a href="../Context/__ApplicationContext.html">__ApplicationContext</a></span><span class="src-sym">::</span><a href="../Context/__ApplicationContext.html#methodgetInstance">getInstance</a><span class="src-sym">(</span><span class="src-sym">)</span><span class="src-sym">-&gt;</span><span class="src-id">getLogger</span><span class="src-sym">(</span><span class="src-sym">)</span><span class="src-sym">;</span></div></li>
</ol></pre></div></p>
  <p>The logger exposes mainly the following methods:</p>
  <ul><li>debug (see <a href="../Log/__ILogger.html#methoddebug">__ILogger::debug()</a>): Log an string in debug level</li>
    <li>info (see <a href="../Log/__ILogger.html#methodinfo">__ILogger::info()</a>): Log an string in info level</li>
    <li>warn (see <a href="../Log/__ILogger.html#methodwarn">__ILogger::warn()</a>): Log an string in warn level</li>
    <li>error (see <a href="../Log/__ILogger.html#methoderror">__ILogger::error()</a>): Log an string in error level</li>
    <li>fatal (see <a href="../Log/__ILogger.html#methodfatal">__ILogger::fatal()</a>): Log an string in fatal level</li></ul>   
  <p>i.e.
  <div class="listing"><pre><ol><li><div class="src-line"><span class="src-php">&lt;?php</span></div></li>
<li><div class="src-line">&nbsp;</div></li>
<li><div class="src-line">&nbsp;&nbsp;&nbsp;&nbsp;<span class="src-comm">//get&nbsp;the&nbsp;default&nbsp;application's&nbsp;logger:</span></div></li>
<li><div class="src-line">&nbsp;&nbsp;&nbsp;&nbsp;<span class="src-var">$logger&nbsp;</span>=&nbsp;<span class="src-id"><a href="../Context/__ApplicationContext.html">__ApplicationContext</a></span><span class="src-sym">::</span><a href="../Context/__ApplicationContext.html#methodgetInstance">getInstance</a><span class="src-sym">(</span><span class="src-sym">)</span><span class="src-sym">-&gt;</span><span class="src-id">getLogger</span><span class="src-sym">(</span><span class="src-sym">)</span><span class="src-sym">;</span></div></li>
<li><div class="src-line">&nbsp;&nbsp;&nbsp;&nbsp;<span class="src-comm">//log&nbsp;in&nbsp;debug&nbsp;mode:</span></div></li>
<li><div class="src-line">&nbsp;&nbsp;&nbsp;&nbsp;<span class="src-var">$logger</span><span class="src-sym">-&gt;</span><span class="src-id">debug</span><span class="src-sym">(</span><span class="src-str">'This&nbsp;is&nbsp;a&nbsp;debug&nbsp;message'</span><span class="src-sym">)</span><span class="src-sym">;</span></div></li>
</ol></pre></div></p></span></div>
      </td></tr></table>
Return current item: Lion PHP Framework