blob: 1dca1bf3bb9adc26722e2ea0d4873b039c19d82e [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreev76cc9202011-12-22 20:02:04 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreev76cc9202011-12-22 20:02:04 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * 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 Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
darwinel871754a2014-02-11 17:34:57 +010021 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000028
Derek Allard2067d1a2008-11-13 22:59:24 +000029/**
30 * Logging Class
31 *
32 * @package CodeIgniter
33 * @subpackage Libraries
34 * @category Logging
Derek Jonesf4a4bd82011-10-20 12:18:42 -050035 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000036 * @link http://codeigniter.com/user_guide/general/errors.html
37 */
38class CI_Log {
39
Timothy Warrenb82bc3a2012-04-27 09:12:58 -040040 /**
41 * Path to save log files
42 *
43 * @var string
44 */
Greg Aker2eaa4072010-12-21 11:44:08 -060045 protected $_log_path;
Andrey Andreev56454792012-05-17 14:32:19 +030046
Timothy Warrenb82bc3a2012-04-27 09:12:58 -040047 /**
Andrey Andreev45965742014-08-27 20:40:11 +030048 * File permissions
49 *
50 * @var int
51 */
52 protected $_file_permissions = 0644;
53
54 /**
Timothy Warrenb82bc3a2012-04-27 09:12:58 -040055 * Level of logging
56 *
57 * @var int
58 */
Andrey Andreev45965742014-08-27 20:40:11 +030059 protected $_threshold = 1;
Andrey Andreev56454792012-05-17 14:32:19 +030060
Timothy Warrenb82bc3a2012-04-27 09:12:58 -040061 /**
62 * Highest level of logging
63 *
64 * @var int
65 */
Andrey Andreev45965742014-08-27 20:40:11 +030066 protected $_threshold_max = 0;
Andrey Andreev56454792012-05-17 14:32:19 +030067
Timothy Warrenb82bc3a2012-04-27 09:12:58 -040068 /**
69 * Array of threshold levels to log
70 *
71 * @var array
72 */
Andrey Andreev45965742014-08-27 20:40:11 +030073 protected $_threshold_array = array();
Andrey Andreev56454792012-05-17 14:32:19 +030074
Timothy Warrenb82bc3a2012-04-27 09:12:58 -040075 /**
76 * Format of timestamp for log files
77 *
78 * @var string
79 */
Andrey Andreev45965742014-08-27 20:40:11 +030080 protected $_date_fmt = 'Y-m-d H:i:s';
Andrey Andreev56454792012-05-17 14:32:19 +030081
Timothy Warrenb82bc3a2012-04-27 09:12:58 -040082 /**
Andrey Andreeva107a0f2013-02-15 22:30:31 +020083 * Filename extension
84 *
85 * @var string
86 */
87 protected $_file_ext;
88
89 /**
Timothy Warrenb82bc3a2012-04-27 09:12:58 -040090 * Whether or not the logger can write to the log files
91 *
92 * @var bool
93 */
Andrey Andreev45965742014-08-27 20:40:11 +030094 protected $_enabled = TRUE;
Andrey Andreev56454792012-05-17 14:32:19 +030095
Timothy Warrenb82bc3a2012-04-27 09:12:58 -040096 /**
97 * Predefined logging levels
98 *
99 * @var array
100 */
Andrey Andreev45965742014-08-27 20:40:11 +0300101 protected $_levels = array('ERROR' => 1, 'DEBUG' => 2, 'INFO' => 3, 'ALL' => 4);
Derek Allard2067d1a2008-11-13 22:59:24 +0000102
Andrey Andreeva107a0f2013-02-15 22:30:31 +0200103 // --------------------------------------------------------------------
104
Timothy Warrenb82bc3a2012-04-27 09:12:58 -0400105 /**
Andrey Andreeva107a0f2013-02-15 22:30:31 +0200106 * Class constructor
Timothy Warrenb82bc3a2012-04-27 09:12:58 -0400107 *
Andrey Andreev56454792012-05-17 14:32:19 +0300108 * @return void
Timothy Warrenb82bc3a2012-04-27 09:12:58 -0400109 */
Greg Akera9263282010-11-10 15:26:43 -0600110 public function __construct()
Derek Allard2067d1a2008-11-13 22:59:24 +0000111 {
112 $config =& get_config();
Barry Mienydd671972010-10-04 16:33:58 +0200113
Alex Bilbied261b1e2012-06-02 11:12:16 +0100114 $this->_log_path = ($config['log_path'] !== '') ? $config['log_path'] : APPPATH.'logs/';
Andrey Andreeva107a0f2013-02-15 22:30:31 +0200115 $this->_file_ext = (isset($config['log_file_extension']) && $config['log_file_extension'] !== '')
116 ? ltrim($config['log_file_extension'], '.') : 'php';
Chris Passas0bd6b282013-02-13 14:16:18 -0500117
Andrey Andreev45965742014-08-27 20:40:11 +0300118 file_exists($this->_log_path) OR mkdir($this->_log_path, 0755, TRUE);
Andrey Andreev9f690f12013-01-21 15:30:25 +0200119
Greg Aker2eaa4072010-12-21 11:44:08 -0600120 if ( ! is_dir($this->_log_path) OR ! is_really_writable($this->_log_path))
Derek Allard2067d1a2008-11-13 22:59:24 +0000121 {
122 $this->_enabled = FALSE;
123 }
Barry Mienydd671972010-10-04 16:33:58 +0200124
Derek Allard2067d1a2008-11-13 22:59:24 +0000125 if (is_numeric($config['log_threshold']))
126 {
Andrey Andreev76cc9202011-12-22 20:02:04 +0200127 $this->_threshold = (int) $config['log_threshold'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000128 }
Nithin333f9f92011-08-21 16:52:06 -0400129 elseif (is_array($config['log_threshold']))
130 {
131 $this->_threshold = $this->_threshold_max;
132 $this->_threshold_array = array_flip($config['log_threshold']);
133 }
Barry Mienydd671972010-10-04 16:33:58 +0200134
Andrey Andreev45965742014-08-27 20:40:11 +0300135 if ( ! empty($config['log_date_format']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000136 {
137 $this->_date_fmt = $config['log_date_format'];
138 }
Andrey Andreev45965742014-08-27 20:40:11 +0300139
Andrey Andreevdc0ad202014-09-17 10:34:04 +0300140 if ( ! empty($config['log_file_permissions']) && is_int($config['log_file_permissions']))
Andrey Andreev45965742014-08-27 20:40:11 +0300141 {
142 $this->_file_permissions = $config['log_file_permissions'];
143 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000144 }
Barry Mienydd671972010-10-04 16:33:58 +0200145
Derek Allard2067d1a2008-11-13 22:59:24 +0000146 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200147
Derek Allard2067d1a2008-11-13 22:59:24 +0000148 /**
149 * Write Log File
150 *
151 * Generally this function will be called using the global log_message() function
152 *
vlakoffd0c30ab2013-05-07 07:49:23 +0200153 * @param string the error level: 'error', 'debug' or 'info'
Derek Allard2067d1a2008-11-13 22:59:24 +0000154 * @param string the error message
Derek Allard2067d1a2008-11-13 22:59:24 +0000155 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200156 */
Andrey Andreev838c9a92013-09-13 14:05:13 +0300157 public function write_log($level, $msg)
Barry Mienydd671972010-10-04 16:33:58 +0200158 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000159 if ($this->_enabled === FALSE)
160 {
161 return FALSE;
162 }
Barry Mienydd671972010-10-04 16:33:58 +0200163
Derek Allard2067d1a2008-11-13 22:59:24 +0000164 $level = strtoupper($level);
Barry Mienydd671972010-10-04 16:33:58 +0200165
Andrey Andreev76cc9202011-12-22 20:02:04 +0200166 if (( ! isset($this->_levels[$level]) OR ($this->_levels[$level] > $this->_threshold))
Andrey Andreev443bbd92012-04-03 15:49:11 +0300167 && ! isset($this->_threshold_array[$this->_levels[$level]]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000168 {
Andrey Andreev76cc9202011-12-22 20:02:04 +0200169 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 }
Barry Mienydd671972010-10-04 16:33:58 +0200171
Andrey Andreeva107a0f2013-02-15 22:30:31 +0200172 $filepath = $this->_log_path.'log-'.date('Y-m-d').'.'.$this->_file_ext;
173 $message = '';
Barry Mienydd671972010-10-04 16:33:58 +0200174
Derek Allard2067d1a2008-11-13 22:59:24 +0000175 if ( ! file_exists($filepath))
176 {
Jeroen van der Gulik2835e082011-12-04 14:19:38 +0100177 $newfile = TRUE;
Andrey Andreeva107a0f2013-02-15 22:30:31 +0200178 // Only add protection to php files
179 if ($this->_file_ext === 'php')
Chris Passasfb8de242013-02-14 09:01:24 -0500180 {
Andrey Andreeva107a0f2013-02-15 22:30:31 +0200181 $message .= "<?php defined('BASEPATH') OR exit('No direct script access allowed'); ?>\n\n";
Chris Passasfb8de242013-02-14 09:01:24 -0500182 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000183 }
Barry Mienydd671972010-10-04 16:33:58 +0200184
Andrey Andreev7cf682a2014-03-13 14:55:45 +0200185 if ( ! $fp = @fopen($filepath, 'ab'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000186 {
187 return FALSE;
188 }
189
Andrey Andreev3d215202014-01-15 11:08:47 +0200190 $message .= $level.' - '.date($this->_date_fmt).' --> '.$msg."\n";
Barry Mienydd671972010-10-04 16:33:58 +0200191
192 flock($fp, LOCK_EX);
Andrey Andreevd8b1ad32014-01-15 17:42:52 +0200193
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 Allard2067d1a2008-11-13 22:59:24 +0000202 flock($fp, LOCK_UN);
203 fclose($fp);
Barry Mienydd671972010-10-04 16:33:58 +0200204
Andrey Andreev443bbd92012-04-03 15:49:11 +0300205 if (isset($newfile) && $newfile === TRUE)
Jeroen van der Gulik2835e082011-12-04 14:19:38 +0100206 {
Andrey Andreev45965742014-08-27 20:40:11 +0300207 chmod($filepath, $this->_file_permissions);
Jeroen van der Gulik2835e082011-12-04 14:19:38 +0100208 }
Andrey Andreev443bbd92012-04-03 15:49:11 +0300209
Andrey Andreevd8b1ad32014-01-15 17:42:52 +0200210 return is_int($result);
Derek Allard2067d1a2008-11-13 22:59:24 +0000211 }
212
213}
Derek Allard2067d1a2008-11-13 22:59:24 +0000214
215/* End of file Log.php */
Andrey Andreev13c818e2013-09-14 21:44:36 +0300216/* Location: ./system/core/Log.php */