php 로깅 관련

svn 2008. 1. 18. 10:59

 

 

marionweb.com에서 퍼옴

 

 

Intentionally throw PHP errors
Written by Marion Consulting   

Error messages are not something that most developers enjoy seeing, but they can be very useful especially when debugging or testing an applications code.  Sometimes we would like throw an error to see how the application behaves and below is an example of how to do just this.

trigger_error

Generates a user-level error/warning/notice message (PHP 4 >= 4.0.1, PHP 5)

bool trigger_error ( string error_msg [, int error_type] )
 

 

error_msg

The designated error message for this error. It's limited to 1024 characters in length. Any additional characters beyond 1024 will be truncated.

error_type

The designated error type for this error. It only works with the E_USER family of constants, and will default to E_USER_NOTICE.

Example usage: trigger_error ('I just threw an error", E_USER_ERROR); 

 

How to use log4php
Written by Marion Consulting   

This is a very basic tutorial on how to implement log4php into your php projects.

Log4php is a port of log4j, which is a very popular logging utility in java.  It works very nice in PHP and makes it very simple to log information in many different formats.  The following should allow you to begin using this new functionality in your applications.  Download the example code at a zip file that is provided in plain text below

Get log4php source from http://www.vxr.it/log4php/download.html

Untar/zip the files into a directory on your webserver machine.  There are many directories inside the archive that you do not need unless you want to look over the examples. 

      //define the following in your application

      define('LOG4PHP_DIR', 'log4php-0.9'); // the name of the log4php directory
        The log4php-<version>/src/log4php directory is the directory the above variable should point to.
     

      define('LOG4PHP_CONFIGURATION','log_configuration.xml');
 
      require_once(LOG4PHP_DIR.'/LoggerManager.php');    

      // You can create new logger managers for each script/function you call so you can determine where the logging is        occuring.  I suggest naming it the same as the script that it is in.
      $logger = & LoggerManager::getLogger('your_log_mgr_name');

      $logger->debug('place a debugging log statement here');
      $logger->info('place a info log statement here');
      $logger->warn('place a warning log statement here');
      $logger->error('place a error log statement here');
      $logger->fatal('place a fatal log statement here');

      LoggerManager::shutdown();

 

 

 

Explanation and Examples of log_configuration.xml

The xml file can be named whatever you wish, you just need to reference the appropriate name in the definition of LOG4PHP_CONFIGURATION above.  The xml below writes a log file to the filesystem for all logged messages (debug to fatal), and it sends an email to the specified email address on any fatal logged error.  The xml can be configured for a number of different formats, but the log file and email are my preferred logging methods.

<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
    /**
    *  Script: log_configuration.xml
    *  Author: Author <tony [at] marionweb [dot] com>
    *  
    *  Purpose: Setup logging configuration
    *  Creation Date: 1/29/2006
    *  Details:  Currently setup to write to log file for all messages debug -> fatal.  All fatal messages are emailed to site admin.
    */
-->
<log4php:configuration xmlns:log4php="http://www.vxr.it/log4php/" threshold="all" debug="false">
    <appender name="write_to_log" class="LoggerAppenderDailyFile">
        <param name="datePattern" value="Ymd" />
        <param name="file" value="/path_to_logfile/logs/web_application_%s.log" />
        <layout class="LoggerLayoutTTCC">
            <param name="threadPrinting" value="true" />
            <param name="categoryPrefixing" value="true" />
            <param name="contextPrinting" value="true" />
            <param name="microSecondsPrinting" value="true" />
        </layout>
    </appender>
    <appender name="email_log" class="LoggerAppenderMailEvent">
        <param name="from" value=" admin@email.com" />
        <param name="to" value=" admin@email.com" />
        <param name="subject" value="Log4php test" />
        <layout class="LoggerLayoutTTCC" />
    </appender>
    <root>
        <level value="debug" />       
        <appender_ref ref="write_to_log" />
    </root>
    <root>
        <level value="fatal" />       
        <appender_ref ref="email_log" />
    </root>
</log4php:configuration>

Posted by '김용환'

댓글을 달아 주세요