Log.php
Go to the documentation of this file.00001 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
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
00037
00038
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
00066
00067
00068
00069
00070
00071
00072
00073
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
00115
00116
00117