admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 1 | <?php if (!defined('BASEPATH')) exit('No direct script access allowed'); |
| 2 | /** |
| 3 | * Code Igniter |
| 4 | * |
| 5 | * An open source application development framework for PHP 4.3.2 or newer |
| 6 | * |
| 7 | * @package CodeIgniter |
| 8 | * @author Rick Ellis |
| 9 | * @copyright Copyright (c) 2006, pMachine, Inc. |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 10 | * @license http://www.codeignitor.com/user_guide/license.html |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 11 | * @link http://www.codeigniter.com |
| 12 | * @since Version 1.0 |
| 13 | * @filesource |
| 14 | */ |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 15 | |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 16 | // ------------------------------------------------------------------------ |
| 17 | |
| 18 | /** |
| 19 | * Logging Class |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 20 | * |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 21 | * @package CodeIgniter |
| 22 | * @subpackage Libraries |
| 23 | * @category Logging |
| 24 | * @author Rick Ellis |
| 25 | * @link http://www.codeigniter.com/user_guide/general/errors.html |
| 26 | */ |
| 27 | class CI_Log { |
| 28 | |
| 29 | var $log_path; |
| 30 | var $_threshold = 1; |
| 31 | var $_date_fmt = 'Y-m-d H:i:s'; |
| 32 | var $_enabled = TRUE; |
| 33 | var $_levels = array('ERROR' => '1', 'DEBUG' => '2', 'INFO' => '3', 'ALL' => '4'); |
| 34 | |
| 35 | /** |
| 36 | * Constructor |
| 37 | * |
| 38 | * @access public |
| 39 | * @param string the log file path |
| 40 | * @param string the error threshold |
| 41 | * @param string the date formatting codes |
| 42 | */ |
admin | 633ec37 | 2006-09-15 20:43:32 +0000 | [diff] [blame] | 43 | function CI_Log() |
| 44 | { |
admin | 7099a58 | 2006-10-10 17:47:59 +0000 | [diff] [blame] | 45 | $config = get_config(); |
admin | 633ec37 | 2006-09-15 20:43:32 +0000 | [diff] [blame] | 46 | |
| 47 | $this->log_path = ($config['log_path'] != '') ? $config['log_path'] : BASEPATH.'logs/'; |
admin | 1cf89aa | 2006-09-03 18:24:39 +0000 | [diff] [blame] | 48 | |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 49 | if ( ! is_dir($this->log_path) OR ! is_writable($this->log_path)) |
| 50 | { |
| 51 | $this->_enabled = FALSE; |
| 52 | } |
| 53 | |
admin | 633ec37 | 2006-09-15 20:43:32 +0000 | [diff] [blame] | 54 | if (is_numeric($config['log_threshold'])) |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 55 | { |
admin | 633ec37 | 2006-09-15 20:43:32 +0000 | [diff] [blame] | 56 | $this->_threshold = $config['log_threshold']; |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 57 | } |
| 58 | |
admin | 633ec37 | 2006-09-15 20:43:32 +0000 | [diff] [blame] | 59 | if ($config['log_date_format'] != '') |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 60 | { |
admin | 633ec37 | 2006-09-15 20:43:32 +0000 | [diff] [blame] | 61 | $this->_date_fmt = $config['log_date_format']; |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 62 | } |
| 63 | } |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 64 | |
| 65 | // -------------------------------------------------------------------- |
| 66 | |
| 67 | /** |
| 68 | * Write Log File |
| 69 | * |
| 70 | * Generally this function will be called using the global log_message() function |
| 71 | * |
| 72 | * @access public |
| 73 | * @param string the error level |
| 74 | * @param string the error message |
| 75 | * @param bool whether the error is a native PHP error |
| 76 | * @return bool |
| 77 | */ |
| 78 | function write_log($level = 'error', $msg, $php_error = FALSE) |
| 79 | { |
| 80 | if ($this->_enabled === FALSE) |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 81 | { |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 82 | return FALSE; |
| 83 | } |
| 84 | |
| 85 | $level = strtoupper($level); |
| 86 | |
| 87 | if ( ! isset($this->_levels[$level]) OR ($this->_levels[$level] > $this->_threshold)) |
| 88 | { |
| 89 | return FALSE; |
| 90 | } |
| 91 | |
admin | 141808a | 2006-08-27 01:52:51 +0000 | [diff] [blame] | 92 | $filepath = $this->log_path.'log-'.date('Y-m-d').EXT; |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 93 | $message = ''; |
| 94 | |
| 95 | if ( ! file_exists($filepath)) |
| 96 | { |
| 97 | $message .= "<"."?php if (!defined('BASEPATH')) exit('No direct script access allowed'); ?".">\n\n"; |
| 98 | } |
| 99 | |
| 100 | if ( ! $fp = @fopen($filepath, "a")) |
| 101 | { |
| 102 | return FALSE; |
| 103 | } |
| 104 | |
| 105 | $message .= $level.' '.(($level == 'INFO') ? ' -' : '-').' '.date($this->_date_fmt). ' --> '.$msg."\n"; |
| 106 | |
| 107 | flock($fp, LOCK_EX); |
| 108 | fwrite($fp, $message); |
| 109 | flock($fp, LOCK_UN); |
| 110 | fclose($fp); |
| 111 | |
| 112 | @chmod($filepath, 0666); |
| 113 | return TRUE; |
| 114 | } |
admin | 10c3f41 | 2006-10-08 07:21:12 +0000 | [diff] [blame] | 115 | |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 116 | } |
| 117 | // END Log Class |
| 118 | ?> |