[ Index ]

PHP Cross Reference of Limb3

title

Body

[close]

/log/src/ -> lmbLogFileWriter.class.php (source)

   1  <?php
   2  /*
   3   * Limb PHP Framework
   4   *
   5   * @link http://limb-project.com 
   6   * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)
   7   * @license    LGPL http://www.gnu.org/copyleft/lesser.html 
   8   */
   9  
  10  lmb_require('limb/fs/src/lmbFs.class.php');
  11  
  12  /**

  13   * class lmbLogFileWriter.

  14   *

  15   * @package log

  16   * @version $Id$

  17   */
  18  class lmbLogFileWriter
  19  {
  20    protected $log_files;
  21  
  22    function __construct($log_dir)
  23    {
  24      $this->log_files = array(
  25        lmbLog :: NOTICE => $log_dir . '/notice.log',
  26        lmbLog :: WARNING => $log_dir . '/warning.log',
  27        lmbLog :: ERROR => $log_dir . '/error.log',
  28        lmbLog :: INFO => $log_dir . '/debug.log'
  29      );
  30    }
  31  
  32    function write($entry)
  33    {
  34      $this->_appendToFile($this->getLogFile($entry->getLevel()),
  35                           $entry->asText(),
  36                           $entry->getTime());
  37    }
  38  
  39    protected function _appendToFile($file_name, $message, $stamp)
  40    {
  41      lmbFs :: mkdir(dirname($file_name), 0775);
  42      $file_existed = file_exists($file_name);
  43  
  44      if($fh = fopen($file_name, 'a'))
  45      {
  46        @flock($fh, LOCK_EX);
  47        $time = strftime("%b %d %Y %H:%M:%S", $stamp);
  48  
  49        $log_message = "=========================[{$time}]";
  50  
  51        if(isset($_SERVER['REMOTE_ADDR']))
  52          $log_message .= '[' . $_SERVER['REMOTE_ADDR'] . ']';
  53  
  54        if(isset($_SERVER['REQUEST_URI']))
  55          $log_message .= '[' . $_SERVER['REQUEST_URI'] . ']';
  56  
  57        $log_message .= "=========================\n" . $message;
  58  
  59        fwrite($fh, $log_message);
  60        @flock($fp, LOCK_UN);
  61        fclose($fh);
  62        if(!$file_existed)
  63          chmod($file_name, 0664);
  64      }
  65      else
  66      {
  67        throw new lmbFsException("Cannot open log file '$file_name' for writing\n" .
  68                                 "The web server must be allowed to modify the file.\n" .
  69                                 "File logging for '$file_name' is disabled.");
  70      }
  71    }
  72  
  73    function getLogFile($level)
  74    {
  75      if(isset($this->log_files[$level]))
  76        return $this->log_files[$level];
  77    }
  78  }
  79  
  80  ?>


Generated: Fri Aug 29 04:49:26 2008 Cross-referenced by PHPXref 0.7