blob: bf10d47278a9f76d34db69d23726c60ff15865a9 [file] [log] [blame]
Derek Jones4b9c6292011-07-01 17:40:48 -05001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
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;
Nithin333f9f92011-08-21 16:52:06 -040030 protected $_threshold = 1;
31 protected $_threshold_max = 0;
32 protected $_threshold_array = array();
33 protected $_date_fmt = 'Y-m-d H:i:s';
34 protected $_enabled = TRUE;
35 protected $_levels = array('ERROR' => '1', 'DEBUG' => '2', 'INFO' => '3', 'ALL' => '4');
Derek Allard2067d1a2008-11-13 22:59:24 +000036
37 /**
38 * Constructor
Derek Allard2067d1a2008-11-13 22:59:24 +000039 */
Greg Akera9263282010-11-10 15:26:43 -060040 public function __construct()
Derek Allard2067d1a2008-11-13 22:59:24 +000041 {
42 $config =& get_config();
Barry Mienydd671972010-10-04 16:33:58 +020043
Greg Aker2eaa4072010-12-21 11:44:08 -060044 $this->_log_path = ($config['log_path'] != '') ? $config['log_path'] : APPPATH.'logs/';
Barry Mienydd671972010-10-04 16:33:58 +020045
Greg Aker2eaa4072010-12-21 11:44:08 -060046 if ( ! is_dir($this->_log_path) OR ! is_really_writable($this->_log_path))
Derek Allard2067d1a2008-11-13 22:59:24 +000047 {
48 $this->_enabled = FALSE;
49 }
Barry Mienydd671972010-10-04 16:33:58 +020050
Derek Allard2067d1a2008-11-13 22:59:24 +000051 if (is_numeric($config['log_threshold']))
52 {
53 $this->_threshold = $config['log_threshold'];
54 }
Nithin333f9f92011-08-21 16:52:06 -040055 elseif (is_array($config['log_threshold']))
56 {
57 $this->_threshold = $this->_threshold_max;
58 $this->_threshold_array = array_flip($config['log_threshold']);
59 }
Barry Mienydd671972010-10-04 16:33:58 +020060
Derek Allard2067d1a2008-11-13 22:59:24 +000061 if ($config['log_date_format'] != '')
62 {
63 $this->_date_fmt = $config['log_date_format'];
64 }
65 }
Barry Mienydd671972010-10-04 16:33:58 +020066
Derek Allard2067d1a2008-11-13 22:59:24 +000067 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +020068
Derek Allard2067d1a2008-11-13 22:59:24 +000069 /**
70 * Write Log File
71 *
72 * Generally this function will be called using the global log_message() function
73 *
Derek Allard2067d1a2008-11-13 22:59:24 +000074 * @param string the error level
75 * @param string the error message
76 * @param bool whether the error is a native PHP error
77 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +020078 */
Greg Aker2eaa4072010-12-21 11:44:08 -060079 public function write_log($level = 'error', $msg, $php_error = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +020080 {
Derek Allard2067d1a2008-11-13 22:59:24 +000081 if ($this->_enabled === FALSE)
82 {
83 return FALSE;
84 }
Barry Mienydd671972010-10-04 16:33:58 +020085
Derek Allard2067d1a2008-11-13 22:59:24 +000086 $level = strtoupper($level);
Barry Mienydd671972010-10-04 16:33:58 +020087
Derek Allard2067d1a2008-11-13 22:59:24 +000088 if ( ! isset($this->_levels[$level]) OR ($this->_levels[$level] > $this->_threshold))
89 {
Nithin333f9f92011-08-21 16:52:06 -040090 if (empty($this->_threshold_array) OR ! isset($this->_threshold_array[$this->_levels[$level]]))
91 {
92 return FALSE;
93 }
Derek Allard2067d1a2008-11-13 22:59:24 +000094 }
Barry Mienydd671972010-10-04 16:33:58 +020095
Nithin333f9f92011-08-21 16:52:06 -040096
Greg Aker3a746652011-04-19 10:59:47 -050097 $filepath = $this->_log_path.'log-'.date('Y-m-d').'.php';
Derek Jones4b9c6292011-07-01 17:40:48 -050098 $message = '';
Barry Mienydd671972010-10-04 16:33:58 +020099
Derek Allard2067d1a2008-11-13 22:59:24 +0000100 if ( ! file_exists($filepath))
101 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500102 $message .= "<"."?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); ?".">\n\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000103 }
Barry Mienydd671972010-10-04 16:33:58 +0200104
Derek Allard2067d1a2008-11-13 22:59:24 +0000105 if ( ! $fp = @fopen($filepath, FOPEN_WRITE_CREATE))
106 {
107 return FALSE;
108 }
109
110 $message .= $level.' '.(($level == 'INFO') ? ' -' : '-').' '.date($this->_date_fmt). ' --> '.$msg."\n";
Barry Mienydd671972010-10-04 16:33:58 +0200111
112 flock($fp, LOCK_EX);
Derek Allard2067d1a2008-11-13 22:59:24 +0000113 fwrite($fp, $message);
114 flock($fp, LOCK_UN);
115 fclose($fp);
Barry Mienydd671972010-10-04 16:33:58 +0200116
117 @chmod($filepath, FILE_WRITE_MODE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000118 return TRUE;
119 }
120
121}
122// END Log Class
123
124/* End of file Log.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000125/* Location: ./system/libraries/Log.php */