Log.php

Go to the documentation of this file.
00001 <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
00002 /**
00003  * CodeIgniter
00004  *
00005  * An open source application development framework for PHP 4.3.2 or newer
00006  *
00007  * @package             CodeIgniter
00008  * @author              ExpressionEngine Dev Team
00009  * @copyright   Copyright (c) 2008, EllisLab, Inc.
00010  * @license             http://codeigniter.com/user_guide/license.html
00011  * @link                http://codeigniter.com
00012  * @since               Version 1.0
00013  * @filesource
00014  */
00015 
00016 // ------------------------------------------------------------------------
00017 
00018 /**
00019  * Logging Class
00020  *
00021  * @package             CodeIgniter
00022  * @subpackage  Libraries
00023  * @category    Logging
00024  * @author              ExpressionEngine Dev Team
00025  * @link                http://codeigniter.com/user_guide/general/errors.html
00026  */
00027 class CI_Log {
00028 
00029         var $log_path;
00030         var $_threshold = 1;
00031         var $_date_fmt  = 'Y-m-d H:i:s';
00032         var $_enabled   = TRUE;
00033         var $_levels    = array('ERROR' => '1', 'DEBUG' => '2',  'INFO' => '3', 'ALL' => '4');
00034 
00035         /**
00036          * Constructor
00037          *
00038          * @access      public
00039          */
00040         function CI_Log()
00041         {
00042                 $config =& get_config();
00043                 
00044                 $this->log_path = ($config['log_path'] != '') ? $config['log_path'] : BASEPATH.'logs/';
00045                 
00046                 if ( ! is_dir($this->log_path) OR ! is_really_writable($this->log_path))
00047                 {
00048                         $this->_enabled = FALSE;
00049                 }
00050                 
00051                 if (is_numeric($config['log_threshold']))
00052                 {
00053                         $this->_threshold = $config['log_threshold'];
00054                 }
00055                         
00056                 if ($config['log_date_format'] != '')
00057                 {
00058                         $this->_date_fmt = $config['log_date_format'];
00059                 }
00060         }
00061         
00062         // --------------------------------------------------------------------
00063         
00064         /**
00065          * Write Log File
00066          *
00067          * Generally this function will be called using the global log_message() function
00068          *
00069          * @access      public
00070          * @param       string  the error level
00071          * @param       string  the error message
00072          * @param       bool    whether the error is a native PHP error
00073          * @return      bool
00074          */             
00075         function write_log($level = 'error', $msg, $php_error = FALSE)
00076         {               
00077                 if ($this->_enabled === FALSE)
00078                 {
00079                         return FALSE;
00080                 }
00081         
00082                 $level = strtoupper($level);
00083                 
00084                 if ( ! isset($this->_levels[$level]) OR ($this->_levels[$level] > $this->_threshold))
00085                 {
00086                         return FALSE;
00087                 }
00088         
00089                 $filepath = $this->log_path.'log-'.date('Y-m-d').EXT;
00090                 $message  = '';
00091                 
00092                 if ( ! file_exists($filepath))
00093                 {
00094                         $message .= "<"."?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed'); ?".">\n\n";
00095                 }
00096                         
00097                 if ( ! $fp = @fopen($filepath, FOPEN_WRITE_CREATE))
00098                 {
00099                         return FALSE;
00100                 }
00101 
00102                 $message .= $level.' '.(($level == 'INFO') ? ' -' : '-').' '.date($this->_date_fmt). ' --> '.$msg."\n";
00103                 
00104                 flock($fp, LOCK_EX);    
00105                 fwrite($fp, $message);
00106                 flock($fp, LOCK_UN);
00107                 fclose($fp);
00108         
00109                 @chmod($filepath, FILE_WRITE_MODE);             
00110                 return TRUE;
00111         }
00112 
00113 }
00114 // END Log Class
00115 
00116 /* End of file Log.php */
00117 /* Location: ./system/libraries/Log.php */