Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
| 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
| 5 | * An open source application development framework for PHP 4.3.2 or newer |
| 6 | * |
| 7 | * @package CodeIgniter |
| 8 | * @author ExpressionEngine Dev Team |
Derek Jones | 7f3719f | 2010-01-05 13:35:37 +0000 | [diff] [blame] | 9 | * @copyright Copyright (c) 2008 - 2010, EllisLab, Inc. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 10 | * @license http://codeigniter.com/user_guide/license.html |
| 11 | * @link http://codeigniter.com |
| 12 | * @since Version 1.0 |
| 13 | * @filesource |
| 14 | */ |
| 15 | |
| 16 | // ------------------------------------------------------------------------ |
| 17 | |
| 18 | /** |
| 19 | * Logging Class |
| 20 | * |
| 21 | * @package CodeIgniter |
| 22 | * @subpackage Libraries |
| 23 | * @category Logging |
| 24 | * @author ExpressionEngine Dev Team |
| 25 | * @link http://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 | */ |
| 40 | function CI_Log() |
| 41 | { |
| 42 | $config =& get_config(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 43 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 44 | $this->log_path = ($config['log_path'] != '') ? $config['log_path'] : BASEPATH.'logs/'; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 45 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 46 | if ( ! is_dir($this->log_path) OR ! is_really_writable($this->log_path)) |
| 47 | { |
| 48 | $this->_enabled = FALSE; |
| 49 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 50 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 51 | if (is_numeric($config['log_threshold'])) |
| 52 | { |
| 53 | $this->_threshold = $config['log_threshold']; |
| 54 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 55 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 56 | if ($config['log_date_format'] != '') |
| 57 | { |
| 58 | $this->_date_fmt = $config['log_date_format']; |
| 59 | } |
| 60 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 61 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 62 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 63 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 64 | /** |
| 65 | * Write Log File |
| 66 | * |
| 67 | * Generally this function will be called using the global log_message() function |
| 68 | * |
| 69 | * @access public |
| 70 | * @param string the error level |
| 71 | * @param string the error message |
| 72 | * @param bool whether the error is a native PHP error |
| 73 | * @return bool |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 74 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 75 | function write_log($level = 'error', $msg, $php_error = FALSE) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 76 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 77 | if ($this->_enabled === FALSE) |
| 78 | { |
| 79 | return FALSE; |
| 80 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 81 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 82 | $level = strtoupper($level); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 83 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 84 | if ( ! isset($this->_levels[$level]) OR ($this->_levels[$level] > $this->_threshold)) |
| 85 | { |
| 86 | return FALSE; |
| 87 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 88 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 89 | $filepath = $this->log_path.'log-'.date('Y-m-d').EXT; |
| 90 | $message = ''; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 91 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 92 | if ( ! file_exists($filepath)) |
| 93 | { |
| 94 | $message .= "<"."?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); ?".">\n\n"; |
| 95 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 96 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 97 | if ( ! $fp = @fopen($filepath, FOPEN_WRITE_CREATE)) |
| 98 | { |
| 99 | return FALSE; |
| 100 | } |
| 101 | |
| 102 | $message .= $level.' '.(($level == 'INFO') ? ' -' : '-').' '.date($this->_date_fmt). ' --> '.$msg."\n"; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 103 | |
| 104 | flock($fp, LOCK_EX); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 105 | fwrite($fp, $message); |
| 106 | flock($fp, LOCK_UN); |
| 107 | fclose($fp); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 108 | |
| 109 | @chmod($filepath, FILE_WRITE_MODE); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 110 | return TRUE; |
| 111 | } |
| 112 | |
| 113 | } |
| 114 | // END Log Class |
| 115 | |
| 116 | /* End of file Log.php */ |
Derek Jones | a3ffbbb | 2008-05-11 18:18:29 +0000 | [diff] [blame] | 117 | /* Location: ./system/libraries/Log.php */ |