blob: fb2c5a49b8d32081cdf3f9ea92eda80303e1de8e [file] [log] [blame]
Derek Allard2067d1a2008-11-13 22:59:24 +00001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2/**
3 * CodeIgniter
4 *
Greg Aker741de1c2010-11-10 14:52:57 -06005 * An open source application development framework for PHP 5.1.6 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
7 * @package CodeIgniter
8 * @author ExpressionEngine Dev Team
Greg Aker0711dc82011-01-05 10:49:40 -06009 * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc.
Derek Allard2067d1a2008-11-13 22:59:24 +000010 * @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 */
27class CI_Log {
28
Greg Aker2eaa4072010-12-21 11:44:08 -060029 protected $_log_path;
30 protected $_threshold = 1;
31 protected $_date_fmt = 'Y-m-d H:i:s';
32 protected $_enabled = TRUE;
33 protected $_levels = array('ERROR' => '1', 'DEBUG' => '2', 'INFO' => '3', 'ALL' => '4');
Derek Allard2067d1a2008-11-13 22:59:24 +000034
35 /**
36 * Constructor
Derek Allard2067d1a2008-11-13 22:59:24 +000037 */
Greg Akera9263282010-11-10 15:26:43 -060038 public function __construct()
Derek Allard2067d1a2008-11-13 22:59:24 +000039 {
40 $config =& get_config();
Barry Mienydd671972010-10-04 16:33:58 +020041
Greg Aker2eaa4072010-12-21 11:44:08 -060042 $this->_log_path = ($config['log_path'] != '') ? $config['log_path'] : APPPATH.'logs/';
Barry Mienydd671972010-10-04 16:33:58 +020043
Greg Aker2eaa4072010-12-21 11:44:08 -060044 if ( ! is_dir($this->_log_path) OR ! is_really_writable($this->_log_path))
Derek Allard2067d1a2008-11-13 22:59:24 +000045 {
46 $this->_enabled = FALSE;
47 }
Barry Mienydd671972010-10-04 16:33:58 +020048
Derek Allard2067d1a2008-11-13 22:59:24 +000049 if (is_numeric($config['log_threshold']))
50 {
51 $this->_threshold = $config['log_threshold'];
52 }
Barry Mienydd671972010-10-04 16:33:58 +020053
Derek Allard2067d1a2008-11-13 22:59:24 +000054 if ($config['log_date_format'] != '')
55 {
56 $this->_date_fmt = $config['log_date_format'];
57 }
58 }
Barry Mienydd671972010-10-04 16:33:58 +020059
Derek Allard2067d1a2008-11-13 22:59:24 +000060 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +020061
Derek Allard2067d1a2008-11-13 22:59:24 +000062 /**
63 * Write Log File
64 *
65 * Generally this function will be called using the global log_message() function
66 *
Derek Allard2067d1a2008-11-13 22:59:24 +000067 * @param string the error level
68 * @param string the error message
69 * @param bool whether the error is a native PHP error
70 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +020071 */
Greg Aker2eaa4072010-12-21 11:44:08 -060072 public function write_log($level = 'error', $msg, $php_error = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +020073 {
Derek Allard2067d1a2008-11-13 22:59:24 +000074 if ($this->_enabled === FALSE)
75 {
76 return FALSE;
77 }
Barry Mienydd671972010-10-04 16:33:58 +020078
Derek Allard2067d1a2008-11-13 22:59:24 +000079 $level = strtoupper($level);
Barry Mienydd671972010-10-04 16:33:58 +020080
Derek Allard2067d1a2008-11-13 22:59:24 +000081 if ( ! isset($this->_levels[$level]) OR ($this->_levels[$level] > $this->_threshold))
82 {
83 return FALSE;
84 }
Barry Mienydd671972010-10-04 16:33:58 +020085
Greg Aker2eaa4072010-12-21 11:44:08 -060086 $filepath = $this->_log_path.'log-'.date('Y-m-d').EXT;
Derek Allard2067d1a2008-11-13 22:59:24 +000087 $message = '';
Barry Mienydd671972010-10-04 16:33:58 +020088
Derek Allard2067d1a2008-11-13 22:59:24 +000089 if ( ! file_exists($filepath))
90 {
91 $message .= "<"."?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); ?".">\n\n";
92 }
Barry Mienydd671972010-10-04 16:33:58 +020093
Derek Allard2067d1a2008-11-13 22:59:24 +000094 if ( ! $fp = @fopen($filepath, FOPEN_WRITE_CREATE))
95 {
96 return FALSE;
97 }
98
99 $message .= $level.' '.(($level == 'INFO') ? ' -' : '-').' '.date($this->_date_fmt). ' --> '.$msg."\n";
Barry Mienydd671972010-10-04 16:33:58 +0200100
101 flock($fp, LOCK_EX);
Derek Allard2067d1a2008-11-13 22:59:24 +0000102 fwrite($fp, $message);
103 flock($fp, LOCK_UN);
104 fclose($fp);
Barry Mienydd671972010-10-04 16:33:58 +0200105
106 @chmod($filepath, FILE_WRITE_MODE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000107 return TRUE;
108 }
109
110}
111// END Log Class
112
113/* End of file Log.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000114/* Location: ./system/libraries/Log.php */