| 68 |
lars |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* CodeIgniter
|
|
|
4 |
*
|
|
|
5 |
* An open source application development framework for PHP
|
|
|
6 |
*
|
|
|
7 |
* This content is released under the MIT License (MIT)
|
|
|
8 |
*
|
| 2257 |
lars |
9 |
* Copyright (c) 2014 - 2018, British Columbia Institute of Technology
|
| 68 |
lars |
10 |
*
|
|
|
11 |
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
12 |
* of this software and associated documentation files (the "Software"), to deal
|
|
|
13 |
* in the Software without restriction, including without limitation the rights
|
|
|
14 |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
15 |
* copies of the Software, and to permit persons to whom the Software is
|
|
|
16 |
* furnished to do so, subject to the following conditions:
|
|
|
17 |
*
|
|
|
18 |
* The above copyright notice and this permission notice shall be included in
|
|
|
19 |
* all copies or substantial portions of the Software.
|
|
|
20 |
*
|
|
|
21 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
22 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
23 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
24 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
25 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
26 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
27 |
* THE SOFTWARE.
|
|
|
28 |
*
|
|
|
29 |
* @package CodeIgniter
|
|
|
30 |
* @author EllisLab Dev Team
|
|
|
31 |
* @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
|
| 2257 |
lars |
32 |
* @copyright Copyright (c) 2014 - 2018, British Columbia Institute of Technology (http://bcit.ca/)
|
| 68 |
lars |
33 |
* @license http://opensource.org/licenses/MIT MIT License
|
|
|
34 |
* @link https://codeigniter.com
|
|
|
35 |
* @since Version 1.0.0
|
|
|
36 |
* @filesource
|
|
|
37 |
*/
|
|
|
38 |
defined('BASEPATH') OR exit('No direct script access allowed');
|
|
|
39 |
|
|
|
40 |
/**
|
|
|
41 |
* Logging Class
|
|
|
42 |
*
|
|
|
43 |
* @package CodeIgniter
|
|
|
44 |
* @subpackage Libraries
|
|
|
45 |
* @category Logging
|
|
|
46 |
* @author EllisLab Dev Team
|
|
|
47 |
* @link https://codeigniter.com/user_guide/general/errors.html
|
|
|
48 |
*/
|
|
|
49 |
class CI_Log {
|
|
|
50 |
|
|
|
51 |
/**
|
|
|
52 |
* Path to save log files
|
|
|
53 |
*
|
|
|
54 |
* @var string
|
|
|
55 |
*/
|
|
|
56 |
protected $_log_path;
|
|
|
57 |
|
|
|
58 |
/**
|
|
|
59 |
* File permissions
|
|
|
60 |
*
|
|
|
61 |
* @var int
|
|
|
62 |
*/
|
|
|
63 |
protected $_file_permissions = 0644;
|
|
|
64 |
|
|
|
65 |
/**
|
|
|
66 |
* Level of logging
|
|
|
67 |
*
|
|
|
68 |
* @var int
|
|
|
69 |
*/
|
|
|
70 |
protected $_threshold = 1;
|
|
|
71 |
|
|
|
72 |
/**
|
|
|
73 |
* Array of threshold levels to log
|
|
|
74 |
*
|
|
|
75 |
* @var array
|
|
|
76 |
*/
|
|
|
77 |
protected $_threshold_array = array();
|
|
|
78 |
|
|
|
79 |
/**
|
|
|
80 |
* Format of timestamp for log files
|
|
|
81 |
*
|
|
|
82 |
* @var string
|
|
|
83 |
*/
|
|
|
84 |
protected $_date_fmt = 'Y-m-d H:i:s';
|
|
|
85 |
|
|
|
86 |
/**
|
|
|
87 |
* Filename extension
|
|
|
88 |
*
|
|
|
89 |
* @var string
|
|
|
90 |
*/
|
|
|
91 |
protected $_file_ext;
|
|
|
92 |
|
|
|
93 |
/**
|
|
|
94 |
* Whether or not the logger can write to the log files
|
|
|
95 |
*
|
|
|
96 |
* @var bool
|
|
|
97 |
*/
|
|
|
98 |
protected $_enabled = TRUE;
|
|
|
99 |
|
|
|
100 |
/**
|
|
|
101 |
* Predefined logging levels
|
|
|
102 |
*
|
|
|
103 |
* @var array
|
|
|
104 |
*/
|
|
|
105 |
protected $_levels = array('ERROR' => 1, 'DEBUG' => 2, 'INFO' => 3, 'ALL' => 4);
|
|
|
106 |
|
| 1257 |
lars |
107 |
/**
|
| 2107 |
lars |
108 |
* mbstring.func_overload flag
|
| 1257 |
lars |
109 |
*
|
|
|
110 |
* @var bool
|
|
|
111 |
*/
|
| 2107 |
lars |
112 |
protected static $func_overload;
|
| 1257 |
lars |
113 |
|
| 68 |
lars |
114 |
// --------------------------------------------------------------------
|
|
|
115 |
|
|
|
116 |
/**
|
|
|
117 |
* Class constructor
|
|
|
118 |
*
|
|
|
119 |
* @return void
|
|
|
120 |
*/
|
|
|
121 |
public function __construct()
|
|
|
122 |
{
|
|
|
123 |
$config =& get_config();
|
|
|
124 |
|
| 2107 |
lars |
125 |
isset(self::$func_overload) OR self::$func_overload = (extension_loaded('mbstring') && ini_get('mbstring.func_overload'));
|
| 1257 |
lars |
126 |
|
| 68 |
lars |
127 |
$this->_log_path = ($config['log_path'] !== '') ? $config['log_path'] : APPPATH.'logs/';
|
|
|
128 |
$this->_file_ext = (isset($config['log_file_extension']) && $config['log_file_extension'] !== '')
|
|
|
129 |
? ltrim($config['log_file_extension'], '.') : 'php';
|
|
|
130 |
|
|
|
131 |
file_exists($this->_log_path) OR mkdir($this->_log_path, 0755, TRUE);
|
|
|
132 |
|
|
|
133 |
if ( ! is_dir($this->_log_path) OR ! is_really_writable($this->_log_path))
|
|
|
134 |
{
|
|
|
135 |
$this->_enabled = FALSE;
|
|
|
136 |
}
|
|
|
137 |
|
|
|
138 |
if (is_numeric($config['log_threshold']))
|
|
|
139 |
{
|
|
|
140 |
$this->_threshold = (int) $config['log_threshold'];
|
|
|
141 |
}
|
|
|
142 |
elseif (is_array($config['log_threshold']))
|
|
|
143 |
{
|
|
|
144 |
$this->_threshold = 0;
|
|
|
145 |
$this->_threshold_array = array_flip($config['log_threshold']);
|
|
|
146 |
}
|
|
|
147 |
|
|
|
148 |
if ( ! empty($config['log_date_format']))
|
|
|
149 |
{
|
|
|
150 |
$this->_date_fmt = $config['log_date_format'];
|
|
|
151 |
}
|
|
|
152 |
|
|
|
153 |
if ( ! empty($config['log_file_permissions']) && is_int($config['log_file_permissions']))
|
|
|
154 |
{
|
|
|
155 |
$this->_file_permissions = $config['log_file_permissions'];
|
|
|
156 |
}
|
|
|
157 |
}
|
|
|
158 |
|
|
|
159 |
// --------------------------------------------------------------------
|
|
|
160 |
|
|
|
161 |
/**
|
|
|
162 |
* Write Log File
|
|
|
163 |
*
|
|
|
164 |
* Generally this function will be called using the global log_message() function
|
|
|
165 |
*
|
|
|
166 |
* @param string $level The error level: 'error', 'debug' or 'info'
|
|
|
167 |
* @param string $msg The error message
|
|
|
168 |
* @return bool
|
|
|
169 |
*/
|
|
|
170 |
public function write_log($level, $msg)
|
|
|
171 |
{
|
|
|
172 |
if ($this->_enabled === FALSE)
|
|
|
173 |
{
|
|
|
174 |
return FALSE;
|
|
|
175 |
}
|
|
|
176 |
|
|
|
177 |
$level = strtoupper($level);
|
|
|
178 |
|
|
|
179 |
if (( ! isset($this->_levels[$level]) OR ($this->_levels[$level] > $this->_threshold))
|
|
|
180 |
&& ! isset($this->_threshold_array[$this->_levels[$level]]))
|
|
|
181 |
{
|
|
|
182 |
return FALSE;
|
|
|
183 |
}
|
|
|
184 |
|
|
|
185 |
$filepath = $this->_log_path.'log-'.date('Y-m-d').'.'.$this->_file_ext;
|
|
|
186 |
$message = '';
|
|
|
187 |
|
|
|
188 |
if ( ! file_exists($filepath))
|
|
|
189 |
{
|
|
|
190 |
$newfile = TRUE;
|
|
|
191 |
// Only add protection to php files
|
|
|
192 |
if ($this->_file_ext === 'php')
|
|
|
193 |
{
|
|
|
194 |
$message .= "<?php defined('BASEPATH') OR exit('No direct script access allowed'); ?>\n\n";
|
|
|
195 |
}
|
|
|
196 |
}
|
|
|
197 |
|
|
|
198 |
if ( ! $fp = @fopen($filepath, 'ab'))
|
|
|
199 |
{
|
|
|
200 |
return FALSE;
|
|
|
201 |
}
|
|
|
202 |
|
|
|
203 |
flock($fp, LOCK_EX);
|
|
|
204 |
|
|
|
205 |
// Instantiating DateTime with microseconds appended to initial date is needed for proper support of this format
|
|
|
206 |
if (strpos($this->_date_fmt, 'u') !== FALSE)
|
|
|
207 |
{
|
|
|
208 |
$microtime_full = microtime(TRUE);
|
|
|
209 |
$microtime_short = sprintf("%06d", ($microtime_full - floor($microtime_full)) * 1000000);
|
|
|
210 |
$date = new DateTime(date('Y-m-d H:i:s.'.$microtime_short, $microtime_full));
|
|
|
211 |
$date = $date->format($this->_date_fmt);
|
|
|
212 |
}
|
|
|
213 |
else
|
|
|
214 |
{
|
|
|
215 |
$date = date($this->_date_fmt);
|
|
|
216 |
}
|
|
|
217 |
|
|
|
218 |
$message .= $this->_format_line($level, $date, $msg);
|
|
|
219 |
|
| 1257 |
lars |
220 |
for ($written = 0, $length = self::strlen($message); $written < $length; $written += $result)
|
| 68 |
lars |
221 |
{
|
| 1257 |
lars |
222 |
if (($result = fwrite($fp, self::substr($message, $written))) === FALSE)
|
| 68 |
lars |
223 |
{
|
|
|
224 |
break;
|
|
|
225 |
}
|
|
|
226 |
}
|
|
|
227 |
|
|
|
228 |
flock($fp, LOCK_UN);
|
|
|
229 |
fclose($fp);
|
|
|
230 |
|
|
|
231 |
if (isset($newfile) && $newfile === TRUE)
|
|
|
232 |
{
|
|
|
233 |
chmod($filepath, $this->_file_permissions);
|
|
|
234 |
}
|
|
|
235 |
|
|
|
236 |
return is_int($result);
|
|
|
237 |
}
|
|
|
238 |
|
|
|
239 |
// --------------------------------------------------------------------
|
|
|
240 |
|
|
|
241 |
/**
|
|
|
242 |
* Format the log line.
|
|
|
243 |
*
|
|
|
244 |
* This is for extensibility of log formatting
|
|
|
245 |
* If you want to change the log format, extend the CI_Log class and override this method
|
|
|
246 |
*
|
|
|
247 |
* @param string $level The error level
|
|
|
248 |
* @param string $date Formatted date string
|
|
|
249 |
* @param string $message The log message
|
|
|
250 |
* @return string Formatted log line with a new line character '\n' at the end
|
|
|
251 |
*/
|
|
|
252 |
protected function _format_line($level, $date, $message)
|
|
|
253 |
{
|
|
|
254 |
return $level.' - '.$date.' --> '.$message."\n";
|
|
|
255 |
}
|
| 1257 |
lars |
256 |
|
|
|
257 |
// --------------------------------------------------------------------
|
|
|
258 |
|
|
|
259 |
/**
|
|
|
260 |
* Byte-safe strlen()
|
|
|
261 |
*
|
|
|
262 |
* @param string $str
|
|
|
263 |
* @return int
|
|
|
264 |
*/
|
|
|
265 |
protected static function strlen($str)
|
|
|
266 |
{
|
| 2107 |
lars |
267 |
return (self::$func_overload)
|
| 1257 |
lars |
268 |
? mb_strlen($str, '8bit')
|
|
|
269 |
: strlen($str);
|
|
|
270 |
}
|
|
|
271 |
|
|
|
272 |
// --------------------------------------------------------------------
|
|
|
273 |
|
|
|
274 |
/**
|
|
|
275 |
* Byte-safe substr()
|
|
|
276 |
*
|
|
|
277 |
* @param string $str
|
|
|
278 |
* @param int $start
|
|
|
279 |
* @param int $length
|
|
|
280 |
* @return string
|
|
|
281 |
*/
|
|
|
282 |
protected static function substr($str, $start, $length = NULL)
|
|
|
283 |
{
|
| 2107 |
lars |
284 |
if (self::$func_overload)
|
| 1257 |
lars |
285 |
{
|
|
|
286 |
// mb_substr($str, $start, null, '8bit') returns an empty
|
|
|
287 |
// string on PHP 5.3
|
|
|
288 |
isset($length) OR $length = ($start >= 0 ? self::strlen($str) - $start : -$start);
|
|
|
289 |
return mb_substr($str, $start, $length, '8bit');
|
|
|
290 |
}
|
|
|
291 |
|
|
|
292 |
return isset($length)
|
|
|
293 |
? substr($str, $start, $length)
|
|
|
294 |
: substr($str, $start);
|
|
|
295 |
}
|
| 68 |
lars |
296 |
}
|