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 | * |
Phil Sturgeon | 07c1ac8 | 2012-03-09 17:03:37 +0000 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.2.4 or newer |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 6 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 7 | * NOTICE OF LICENSE |
Andrey Andreev | 76cc920 | 2011-12-22 20:02:04 +0200 | [diff] [blame] | 8 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 9 | * Licensed under the Open Software License version 3.0 |
Andrey Andreev | 76cc920 | 2011-12-22 20:02:04 +0200 | [diff] [blame] | 10 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 11 | * This source file is subject to the Open Software License (OSL 3.0) that is |
| 12 | * bundled with this package in the files license.txt / license.rst. It is |
| 13 | * also available through the world wide web at this URL: |
| 14 | * http://opensource.org/licenses/OSL-3.0 |
| 15 | * If you did not receive a copy of the license and are unable to obtain it |
| 16 | * through the world wide web, please send an email to |
| 17 | * licensing@ellislab.com so we can send you a copy immediately. |
| 18 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 19 | * @package CodeIgniter |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 20 | * @author EllisLab Dev Team |
darwinel | 871754a | 2014-02-11 17:34:57 +0100 | [diff] [blame] | 21 | * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/) |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 22 | * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 23 | * @link http://codeigniter.com |
| 24 | * @since Version 1.0 |
| 25 | * @filesource |
| 26 | */ |
Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 27 | defined('BASEPATH') OR exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 28 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 29 | /** |
| 30 | * Logging Class |
| 31 | * |
| 32 | * @package CodeIgniter |
| 33 | * @subpackage Libraries |
| 34 | * @category Logging |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 35 | * @author EllisLab Dev Team |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 36 | * @link http://codeigniter.com/user_guide/general/errors.html |
| 37 | */ |
| 38 | class CI_Log { |
| 39 | |
Timothy Warren | b82bc3a | 2012-04-27 09:12:58 -0400 | [diff] [blame] | 40 | /** |
| 41 | * Path to save log files |
| 42 | * |
| 43 | * @var string |
| 44 | */ |
Greg Aker | 2eaa407 | 2010-12-21 11:44:08 -0600 | [diff] [blame] | 45 | protected $_log_path; |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 46 | |
Timothy Warren | b82bc3a | 2012-04-27 09:12:58 -0400 | [diff] [blame] | 47 | /** |
Andrey Andreev | 4596574 | 2014-08-27 20:40:11 +0300 | [diff] [blame] | 48 | * File permissions |
| 49 | * |
| 50 | * @var int |
| 51 | */ |
| 52 | protected $_file_permissions = 0644; |
| 53 | |
| 54 | /** |
Timothy Warren | b82bc3a | 2012-04-27 09:12:58 -0400 | [diff] [blame] | 55 | * Level of logging |
| 56 | * |
| 57 | * @var int |
| 58 | */ |
Andrey Andreev | 4596574 | 2014-08-27 20:40:11 +0300 | [diff] [blame] | 59 | protected $_threshold = 1; |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 60 | |
Timothy Warren | b82bc3a | 2012-04-27 09:12:58 -0400 | [diff] [blame] | 61 | /** |
| 62 | * Highest level of logging |
| 63 | * |
| 64 | * @var int |
| 65 | */ |
Andrey Andreev | 4596574 | 2014-08-27 20:40:11 +0300 | [diff] [blame] | 66 | protected $_threshold_max = 0; |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 67 | |
Timothy Warren | b82bc3a | 2012-04-27 09:12:58 -0400 | [diff] [blame] | 68 | /** |
| 69 | * Array of threshold levels to log |
| 70 | * |
| 71 | * @var array |
| 72 | */ |
Andrey Andreev | 4596574 | 2014-08-27 20:40:11 +0300 | [diff] [blame] | 73 | protected $_threshold_array = array(); |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 74 | |
Timothy Warren | b82bc3a | 2012-04-27 09:12:58 -0400 | [diff] [blame] | 75 | /** |
| 76 | * Format of timestamp for log files |
| 77 | * |
| 78 | * @var string |
| 79 | */ |
Andrey Andreev | 4596574 | 2014-08-27 20:40:11 +0300 | [diff] [blame] | 80 | protected $_date_fmt = 'Y-m-d H:i:s'; |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 81 | |
Timothy Warren | b82bc3a | 2012-04-27 09:12:58 -0400 | [diff] [blame] | 82 | /** |
Andrey Andreev | a107a0f | 2013-02-15 22:30:31 +0200 | [diff] [blame] | 83 | * Filename extension |
| 84 | * |
| 85 | * @var string |
| 86 | */ |
| 87 | protected $_file_ext; |
| 88 | |
| 89 | /** |
Timothy Warren | b82bc3a | 2012-04-27 09:12:58 -0400 | [diff] [blame] | 90 | * Whether or not the logger can write to the log files |
| 91 | * |
| 92 | * @var bool |
| 93 | */ |
Andrey Andreev | 4596574 | 2014-08-27 20:40:11 +0300 | [diff] [blame] | 94 | protected $_enabled = TRUE; |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 95 | |
Timothy Warren | b82bc3a | 2012-04-27 09:12:58 -0400 | [diff] [blame] | 96 | /** |
| 97 | * Predefined logging levels |
| 98 | * |
| 99 | * @var array |
| 100 | */ |
Andrey Andreev | 4596574 | 2014-08-27 20:40:11 +0300 | [diff] [blame] | 101 | protected $_levels = array('ERROR' => 1, 'DEBUG' => 2, 'INFO' => 3, 'ALL' => 4); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 102 | |
Andrey Andreev | a107a0f | 2013-02-15 22:30:31 +0200 | [diff] [blame] | 103 | // -------------------------------------------------------------------- |
| 104 | |
Timothy Warren | b82bc3a | 2012-04-27 09:12:58 -0400 | [diff] [blame] | 105 | /** |
Andrey Andreev | a107a0f | 2013-02-15 22:30:31 +0200 | [diff] [blame] | 106 | * Class constructor |
Timothy Warren | b82bc3a | 2012-04-27 09:12:58 -0400 | [diff] [blame] | 107 | * |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 108 | * @return void |
Timothy Warren | b82bc3a | 2012-04-27 09:12:58 -0400 | [diff] [blame] | 109 | */ |
Greg Aker | a926328 | 2010-11-10 15:26:43 -0600 | [diff] [blame] | 110 | public function __construct() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 111 | { |
| 112 | $config =& get_config(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 113 | |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 114 | $this->_log_path = ($config['log_path'] !== '') ? $config['log_path'] : APPPATH.'logs/'; |
Andrey Andreev | a107a0f | 2013-02-15 22:30:31 +0200 | [diff] [blame] | 115 | $this->_file_ext = (isset($config['log_file_extension']) && $config['log_file_extension'] !== '') |
| 116 | ? ltrim($config['log_file_extension'], '.') : 'php'; |
Chris Passas | 0bd6b28 | 2013-02-13 14:16:18 -0500 | [diff] [blame] | 117 | |
Andrey Andreev | 4596574 | 2014-08-27 20:40:11 +0300 | [diff] [blame] | 118 | file_exists($this->_log_path) OR mkdir($this->_log_path, 0755, TRUE); |
Andrey Andreev | 9f690f1 | 2013-01-21 15:30:25 +0200 | [diff] [blame] | 119 | |
Greg Aker | 2eaa407 | 2010-12-21 11:44:08 -0600 | [diff] [blame] | 120 | 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] | 121 | { |
| 122 | $this->_enabled = FALSE; |
| 123 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 124 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 125 | if (is_numeric($config['log_threshold'])) |
| 126 | { |
Andrey Andreev | 76cc920 | 2011-12-22 20:02:04 +0200 | [diff] [blame] | 127 | $this->_threshold = (int) $config['log_threshold']; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 128 | } |
Nithin | 333f9f9 | 2011-08-21 16:52:06 -0400 | [diff] [blame] | 129 | elseif (is_array($config['log_threshold'])) |
| 130 | { |
| 131 | $this->_threshold = $this->_threshold_max; |
| 132 | $this->_threshold_array = array_flip($config['log_threshold']); |
| 133 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 134 | |
Andrey Andreev | 4596574 | 2014-08-27 20:40:11 +0300 | [diff] [blame] | 135 | if ( ! empty($config['log_date_format'])) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 136 | { |
| 137 | $this->_date_fmt = $config['log_date_format']; |
| 138 | } |
Andrey Andreev | 4596574 | 2014-08-27 20:40:11 +0300 | [diff] [blame] | 139 | |
Andrey Andreev | dc0ad20 | 2014-09-17 10:34:04 +0300 | [diff] [blame^] | 140 | if ( ! empty($config['log_file_permissions']) && is_int($config['log_file_permissions'])) |
Andrey Andreev | 4596574 | 2014-08-27 20:40:11 +0300 | [diff] [blame] | 141 | { |
| 142 | $this->_file_permissions = $config['log_file_permissions']; |
| 143 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 144 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 145 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 146 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 147 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 148 | /** |
| 149 | * Write Log File |
| 150 | * |
| 151 | * Generally this function will be called using the global log_message() function |
| 152 | * |
vlakoff | d0c30ab | 2013-05-07 07:49:23 +0200 | [diff] [blame] | 153 | * @param string the error level: 'error', 'debug' or 'info' |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 154 | * @param string the error message |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 155 | * @return bool |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 156 | */ |
Andrey Andreev | 838c9a9 | 2013-09-13 14:05:13 +0300 | [diff] [blame] | 157 | public function write_log($level, $msg) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 158 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 159 | if ($this->_enabled === FALSE) |
| 160 | { |
| 161 | return FALSE; |
| 162 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 163 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 164 | $level = strtoupper($level); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 165 | |
Andrey Andreev | 76cc920 | 2011-12-22 20:02:04 +0200 | [diff] [blame] | 166 | if (( ! isset($this->_levels[$level]) OR ($this->_levels[$level] > $this->_threshold)) |
Andrey Andreev | 443bbd9 | 2012-04-03 15:49:11 +0300 | [diff] [blame] | 167 | && ! isset($this->_threshold_array[$this->_levels[$level]])) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 168 | { |
Andrey Andreev | 76cc920 | 2011-12-22 20:02:04 +0200 | [diff] [blame] | 169 | return FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 170 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 171 | |
Andrey Andreev | a107a0f | 2013-02-15 22:30:31 +0200 | [diff] [blame] | 172 | $filepath = $this->_log_path.'log-'.date('Y-m-d').'.'.$this->_file_ext; |
| 173 | $message = ''; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 174 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 175 | if ( ! file_exists($filepath)) |
| 176 | { |
Jeroen van der Gulik | 2835e08 | 2011-12-04 14:19:38 +0100 | [diff] [blame] | 177 | $newfile = TRUE; |
Andrey Andreev | a107a0f | 2013-02-15 22:30:31 +0200 | [diff] [blame] | 178 | // Only add protection to php files |
| 179 | if ($this->_file_ext === 'php') |
Chris Passas | fb8de24 | 2013-02-14 09:01:24 -0500 | [diff] [blame] | 180 | { |
Andrey Andreev | a107a0f | 2013-02-15 22:30:31 +0200 | [diff] [blame] | 181 | $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] | 182 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 183 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 184 | |
Andrey Andreev | 7cf682a | 2014-03-13 14:55:45 +0200 | [diff] [blame] | 185 | if ( ! $fp = @fopen($filepath, 'ab')) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 186 | { |
| 187 | return FALSE; |
| 188 | } |
| 189 | |
Andrey Andreev | 3d21520 | 2014-01-15 11:08:47 +0200 | [diff] [blame] | 190 | $message .= $level.' - '.date($this->_date_fmt).' --> '.$msg."\n"; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 191 | |
| 192 | flock($fp, LOCK_EX); |
Andrey Andreev | d8b1ad3 | 2014-01-15 17:42:52 +0200 | [diff] [blame] | 193 | |
| 194 | for ($written = 0, $length = strlen($message); $written < $length; $written += $result) |
| 195 | { |
| 196 | if (($result = fwrite($fp, substr($message, $written))) === FALSE) |
| 197 | { |
| 198 | break; |
| 199 | } |
| 200 | } |
| 201 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 202 | flock($fp, LOCK_UN); |
| 203 | fclose($fp); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 204 | |
Andrey Andreev | 443bbd9 | 2012-04-03 15:49:11 +0300 | [diff] [blame] | 205 | if (isset($newfile) && $newfile === TRUE) |
Jeroen van der Gulik | 2835e08 | 2011-12-04 14:19:38 +0100 | [diff] [blame] | 206 | { |
Andrey Andreev | 4596574 | 2014-08-27 20:40:11 +0300 | [diff] [blame] | 207 | chmod($filepath, $this->_file_permissions); |
Jeroen van der Gulik | 2835e08 | 2011-12-04 14:19:38 +0100 | [diff] [blame] | 208 | } |
Andrey Andreev | 443bbd9 | 2012-04-03 15:49:11 +0300 | [diff] [blame] | 209 | |
Andrey Andreev | d8b1ad3 | 2014-01-15 17:42:52 +0200 | [diff] [blame] | 210 | return is_int($result); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 214 | |
| 215 | /* End of file Log.php */ |
Andrey Andreev | 13c818e | 2013-09-14 21:44:36 +0300 | [diff] [blame] | 216 | /* Location: ./system/core/Log.php */ |