Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 1 | <?php |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Andrey Andreev | fe9309d | 2015-01-09 17:48:58 +0200 | [diff] [blame] | 5 | * An open source application development framework for PHP |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 6 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 7 | * This content is released under the MIT License (MIT) |
Andrey Andreev | 76cc920 | 2011-12-22 20:02:04 +0200 | [diff] [blame] | 8 | * |
Andrey Andreev | 125ef47 | 2016-01-11 12:33:00 +0200 | [diff] [blame] | 9 | * Copyright (c) 2014 - 2016, British Columbia Institute of Technology |
Andrey Andreev | 76cc920 | 2011-12-22 20:02:04 +0200 | [diff] [blame] | 10 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 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: |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 17 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 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 |
Andrey Andreev | 1924e87 | 2016-01-11 12:55:34 +0200 | [diff] [blame] | 31 | * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/) |
Andrey Andreev | 125ef47 | 2016-01-11 12:33:00 +0200 | [diff] [blame] | 32 | * @copyright Copyright (c) 2014 - 2016, British Columbia Institute of Technology (http://bcit.ca/) |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 33 | * @license http://opensource.org/licenses/MIT MIT License |
Andrey Andreev | bd202c9 | 2016-01-11 12:50:18 +0200 | [diff] [blame] | 34 | * @link https://codeigniter.com |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 35 | * @since Version 1.0.0 |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 36 | * @filesource |
| 37 | */ |
Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 38 | defined('BASEPATH') OR exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 39 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 40 | /** |
| 41 | * Logging Class |
| 42 | * |
| 43 | * @package CodeIgniter |
| 44 | * @subpackage Libraries |
| 45 | * @category Logging |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 46 | * @author EllisLab Dev Team |
Andrey Andreev | bd202c9 | 2016-01-11 12:50:18 +0200 | [diff] [blame] | 47 | * @link https://codeigniter.com/user_guide/general/errors.html |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 48 | */ |
| 49 | class CI_Log { |
| 50 | |
Timothy Warren | b82bc3a | 2012-04-27 09:12:58 -0400 | [diff] [blame] | 51 | /** |
| 52 | * Path to save log files |
| 53 | * |
| 54 | * @var string |
| 55 | */ |
Greg Aker | 2eaa407 | 2010-12-21 11:44:08 -0600 | [diff] [blame] | 56 | protected $_log_path; |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 57 | |
Timothy Warren | b82bc3a | 2012-04-27 09:12:58 -0400 | [diff] [blame] | 58 | /** |
Andrey Andreev | 4596574 | 2014-08-27 20:40:11 +0300 | [diff] [blame] | 59 | * File permissions |
| 60 | * |
| 61 | * @var int |
| 62 | */ |
| 63 | protected $_file_permissions = 0644; |
| 64 | |
| 65 | /** |
Timothy Warren | b82bc3a | 2012-04-27 09:12:58 -0400 | [diff] [blame] | 66 | * Level of logging |
| 67 | * |
| 68 | * @var int |
| 69 | */ |
Andrey Andreev | 4596574 | 2014-08-27 20:40:11 +0300 | [diff] [blame] | 70 | protected $_threshold = 1; |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 71 | |
Timothy Warren | b82bc3a | 2012-04-27 09:12:58 -0400 | [diff] [blame] | 72 | /** |
Timothy Warren | b82bc3a | 2012-04-27 09:12:58 -0400 | [diff] [blame] | 73 | * Array of threshold levels to log |
| 74 | * |
| 75 | * @var array |
| 76 | */ |
Andrey Andreev | 4596574 | 2014-08-27 20:40:11 +0300 | [diff] [blame] | 77 | protected $_threshold_array = array(); |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 78 | |
Timothy Warren | b82bc3a | 2012-04-27 09:12:58 -0400 | [diff] [blame] | 79 | /** |
| 80 | * Format of timestamp for log files |
| 81 | * |
| 82 | * @var string |
| 83 | */ |
Andrey Andreev | 4596574 | 2014-08-27 20:40:11 +0300 | [diff] [blame] | 84 | protected $_date_fmt = 'Y-m-d H:i:s'; |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 85 | |
Timothy Warren | b82bc3a | 2012-04-27 09:12:58 -0400 | [diff] [blame] | 86 | /** |
Andrey Andreev | a107a0f | 2013-02-15 22:30:31 +0200 | [diff] [blame] | 87 | * Filename extension |
| 88 | * |
| 89 | * @var string |
| 90 | */ |
| 91 | protected $_file_ext; |
| 92 | |
| 93 | /** |
Timothy Warren | b82bc3a | 2012-04-27 09:12:58 -0400 | [diff] [blame] | 94 | * Whether or not the logger can write to the log files |
| 95 | * |
| 96 | * @var bool |
| 97 | */ |
Andrey Andreev | 4596574 | 2014-08-27 20:40:11 +0300 | [diff] [blame] | 98 | protected $_enabled = TRUE; |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 99 | |
Timothy Warren | b82bc3a | 2012-04-27 09:12:58 -0400 | [diff] [blame] | 100 | /** |
| 101 | * Predefined logging levels |
| 102 | * |
| 103 | * @var array |
| 104 | */ |
Andrey Andreev | 4596574 | 2014-08-27 20:40:11 +0300 | [diff] [blame] | 105 | protected $_levels = array('ERROR' => 1, 'DEBUG' => 2, 'INFO' => 3, 'ALL' => 4); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 106 | |
Andrey Andreev | a107a0f | 2013-02-15 22:30:31 +0200 | [diff] [blame] | 107 | // -------------------------------------------------------------------- |
| 108 | |
Timothy Warren | b82bc3a | 2012-04-27 09:12:58 -0400 | [diff] [blame] | 109 | /** |
Andrey Andreev | a107a0f | 2013-02-15 22:30:31 +0200 | [diff] [blame] | 110 | * Class constructor |
Timothy Warren | b82bc3a | 2012-04-27 09:12:58 -0400 | [diff] [blame] | 111 | * |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 112 | * @return void |
Timothy Warren | b82bc3a | 2012-04-27 09:12:58 -0400 | [diff] [blame] | 113 | */ |
Greg Aker | a926328 | 2010-11-10 15:26:43 -0600 | [diff] [blame] | 114 | public function __construct() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 115 | { |
| 116 | $config =& get_config(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 117 | |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 118 | $this->_log_path = ($config['log_path'] !== '') ? $config['log_path'] : APPPATH.'logs/'; |
Andrey Andreev | a107a0f | 2013-02-15 22:30:31 +0200 | [diff] [blame] | 119 | $this->_file_ext = (isset($config['log_file_extension']) && $config['log_file_extension'] !== '') |
| 120 | ? ltrim($config['log_file_extension'], '.') : 'php'; |
Chris Passas | 0bd6b28 | 2013-02-13 14:16:18 -0500 | [diff] [blame] | 121 | |
Andrey Andreev | 4596574 | 2014-08-27 20:40:11 +0300 | [diff] [blame] | 122 | file_exists($this->_log_path) OR mkdir($this->_log_path, 0755, TRUE); |
Andrey Andreev | 9f690f1 | 2013-01-21 15:30:25 +0200 | [diff] [blame] | 123 | |
Greg Aker | 2eaa407 | 2010-12-21 11:44:08 -0600 | [diff] [blame] | 124 | if ( ! is_dir($this->_log_path) OR ! is_really_writable($this->_log_path)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 125 | { |
| 126 | $this->_enabled = FALSE; |
| 127 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 128 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 129 | if (is_numeric($config['log_threshold'])) |
| 130 | { |
Andrey Andreev | 76cc920 | 2011-12-22 20:02:04 +0200 | [diff] [blame] | 131 | $this->_threshold = (int) $config['log_threshold']; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 132 | } |
Nithin | 333f9f9 | 2011-08-21 16:52:06 -0400 | [diff] [blame] | 133 | elseif (is_array($config['log_threshold'])) |
| 134 | { |
Andrey Andreev | fd08d02 | 2015-03-03 12:36:11 +0200 | [diff] [blame] | 135 | $this->_threshold = 0; |
Nithin | 333f9f9 | 2011-08-21 16:52:06 -0400 | [diff] [blame] | 136 | $this->_threshold_array = array_flip($config['log_threshold']); |
| 137 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 138 | |
Andrey Andreev | 4596574 | 2014-08-27 20:40:11 +0300 | [diff] [blame] | 139 | if ( ! empty($config['log_date_format'])) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 140 | { |
| 141 | $this->_date_fmt = $config['log_date_format']; |
| 142 | } |
Andrey Andreev | 4596574 | 2014-08-27 20:40:11 +0300 | [diff] [blame] | 143 | |
Andrey Andreev | dc0ad20 | 2014-09-17 10:34:04 +0300 | [diff] [blame] | 144 | if ( ! empty($config['log_file_permissions']) && is_int($config['log_file_permissions'])) |
Andrey Andreev | 4596574 | 2014-08-27 20:40:11 +0300 | [diff] [blame] | 145 | { |
| 146 | $this->_file_permissions = $config['log_file_permissions']; |
| 147 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 148 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 149 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 150 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 151 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 152 | /** |
| 153 | * Write Log File |
| 154 | * |
| 155 | * Generally this function will be called using the global log_message() function |
| 156 | * |
Andrey Andreev | 1ccc8be | 2016-02-09 21:15:10 +0200 | [diff] [blame] | 157 | * @param string $level The error level: 'error', 'debug' or 'info' |
| 158 | * @param string $msg The error message |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 159 | * @return bool |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 160 | */ |
Andrey Andreev | 838c9a9 | 2013-09-13 14:05:13 +0300 | [diff] [blame] | 161 | public function write_log($level, $msg) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 162 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 163 | if ($this->_enabled === FALSE) |
| 164 | { |
| 165 | return FALSE; |
| 166 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 167 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 168 | $level = strtoupper($level); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 169 | |
Andrey Andreev | 76cc920 | 2011-12-22 20:02:04 +0200 | [diff] [blame] | 170 | if (( ! isset($this->_levels[$level]) OR ($this->_levels[$level] > $this->_threshold)) |
Andrey Andreev | 443bbd9 | 2012-04-03 15:49:11 +0300 | [diff] [blame] | 171 | && ! isset($this->_threshold_array[$this->_levels[$level]])) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 172 | { |
Andrey Andreev | 76cc920 | 2011-12-22 20:02:04 +0200 | [diff] [blame] | 173 | return FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 174 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 175 | |
Andrey Andreev | a107a0f | 2013-02-15 22:30:31 +0200 | [diff] [blame] | 176 | $filepath = $this->_log_path.'log-'.date('Y-m-d').'.'.$this->_file_ext; |
| 177 | $message = ''; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 178 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 179 | if ( ! file_exists($filepath)) |
| 180 | { |
Jeroen van der Gulik | 2835e08 | 2011-12-04 14:19:38 +0100 | [diff] [blame] | 181 | $newfile = TRUE; |
Andrey Andreev | a107a0f | 2013-02-15 22:30:31 +0200 | [diff] [blame] | 182 | // Only add protection to php files |
| 183 | if ($this->_file_ext === 'php') |
Chris Passas | fb8de24 | 2013-02-14 09:01:24 -0500 | [diff] [blame] | 184 | { |
Andrey Andreev | a107a0f | 2013-02-15 22:30:31 +0200 | [diff] [blame] | 185 | $message .= "<?php defined('BASEPATH') OR exit('No direct script access allowed'); ?>\n\n"; |
Chris Passas | fb8de24 | 2013-02-14 09:01:24 -0500 | [diff] [blame] | 186 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 187 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 188 | |
Andrey Andreev | 7cf682a | 2014-03-13 14:55:45 +0200 | [diff] [blame] | 189 | if ( ! $fp = @fopen($filepath, 'ab')) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 190 | { |
| 191 | return FALSE; |
| 192 | } |
| 193 | |
Andrey Andreev | e1f36e3 | 2016-03-01 13:33:37 +0200 | [diff] [blame] | 194 | flock($fp, LOCK_EX); |
| 195 | |
Gabriel Potkány | 53e8303 | 2014-12-20 00:00:23 +0100 | [diff] [blame] | 196 | // Instantiating DateTime with microseconds appended to initial date is needed for proper support of this format |
| 197 | if (strpos($this->_date_fmt, 'u') !== FALSE) |
| 198 | { |
| 199 | $microtime_full = microtime(TRUE); |
| 200 | $microtime_short = sprintf("%06d", ($microtime_full - floor($microtime_full)) * 1000000); |
| 201 | $date = new DateTime(date('Y-m-d H:i:s.'.$microtime_short, $microtime_full)); |
| 202 | $date = $date->format($this->_date_fmt); |
| 203 | } |
| 204 | else |
| 205 | { |
| 206 | $date = date($this->_date_fmt); |
| 207 | } |
Gabriel Potkány | f83262e | 2014-12-19 12:35:00 +0100 | [diff] [blame] | 208 | |
Andrey Andreev | 1ccc8be | 2016-02-09 21:15:10 +0200 | [diff] [blame] | 209 | $message .= $this->_format_line($level, $date, $msg); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 210 | |
Andrey Andreev | d8b1ad3 | 2014-01-15 17:42:52 +0200 | [diff] [blame] | 211 | for ($written = 0, $length = strlen($message); $written < $length; $written += $result) |
| 212 | { |
| 213 | if (($result = fwrite($fp, substr($message, $written))) === FALSE) |
| 214 | { |
| 215 | break; |
| 216 | } |
| 217 | } |
| 218 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 219 | flock($fp, LOCK_UN); |
| 220 | fclose($fp); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 221 | |
Andrey Andreev | 443bbd9 | 2012-04-03 15:49:11 +0300 | [diff] [blame] | 222 | if (isset($newfile) && $newfile === TRUE) |
Jeroen van der Gulik | 2835e08 | 2011-12-04 14:19:38 +0100 | [diff] [blame] | 223 | { |
Andrey Andreev | 4596574 | 2014-08-27 20:40:11 +0300 | [diff] [blame] | 224 | chmod($filepath, $this->_file_permissions); |
Jeroen van der Gulik | 2835e08 | 2011-12-04 14:19:38 +0100 | [diff] [blame] | 225 | } |
Andrey Andreev | 443bbd9 | 2012-04-03 15:49:11 +0300 | [diff] [blame] | 226 | |
Andrey Andreev | d8b1ad3 | 2014-01-15 17:42:52 +0200 | [diff] [blame] | 227 | return is_int($result); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 228 | } |
| 229 | |
Andrey Andreev | 1ccc8be | 2016-02-09 21:15:10 +0200 | [diff] [blame] | 230 | // -------------------------------------------------------------------- |
| 231 | |
| 232 | /** |
| 233 | * Format the log line. |
| 234 | * |
| 235 | * This is for extensibility of log formatting |
| 236 | * If you want to change the log format, extend the CI_Log class and override this method |
| 237 | * |
| 238 | * @param string $level The error level |
| 239 | * @param string $date Formatted date string |
Andrey Andreev | e13fa9f | 2016-05-20 17:30:07 +0300 | [diff] [blame^] | 240 | * @param string $message The log message |
Andrey Andreev | 1ccc8be | 2016-02-09 21:15:10 +0200 | [diff] [blame] | 241 | * @return string Formatted log line with a new line character '\n' at the end |
| 242 | */ |
| 243 | protected function _format_line($level, $date, $message) |
| 244 | { |
| 245 | return $level.' - '.$date.' --> '.$message."\n"; |
| 246 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 247 | } |