blob: 20bc55986f964010695f14ebcc88f922a41400ee [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
Andrey Andreev80500af2013-01-01 08:16:53 +020021 * @copyright Copyright (c) 2008 - 2013, 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 /**
48 * Level of logging
49 *
50 * @var int
51 */
Nithin333f9f92011-08-21 16:52:06 -040052 protected $_threshold = 1;
Andrey Andreev56454792012-05-17 14:32:19 +030053
Timothy Warrenb82bc3a2012-04-27 09:12:58 -040054 /**
55 * Highest level of logging
56 *
57 * @var int
58 */
Nithin333f9f92011-08-21 16:52:06 -040059 protected $_threshold_max = 0;
Andrey Andreev56454792012-05-17 14:32:19 +030060
Timothy Warrenb82bc3a2012-04-27 09:12:58 -040061 /**
62 * Array of threshold levels to log
63 *
64 * @var array
65 */
Nithin333f9f92011-08-21 16:52:06 -040066 protected $_threshold_array = array();
Andrey Andreev56454792012-05-17 14:32:19 +030067
Timothy Warrenb82bc3a2012-04-27 09:12:58 -040068 /**
69 * Format of timestamp for log files
70 *
71 * @var string
72 */
Nithin333f9f92011-08-21 16:52:06 -040073 protected $_date_fmt = 'Y-m-d H:i:s';
Andrey Andreev56454792012-05-17 14:32:19 +030074
Timothy Warrenb82bc3a2012-04-27 09:12:58 -040075 /**
Andrey Andreeva107a0f2013-02-15 22:30:31 +020076 * Filename extension
77 *
78 * @var string
79 */
80 protected $_file_ext;
81
82 /**
Timothy Warrenb82bc3a2012-04-27 09:12:58 -040083 * Whether or not the logger can write to the log files
84 *
85 * @var bool
86 */
Andrey Andreev443bbd92012-04-03 15:49:11 +030087 protected $_enabled = TRUE;
Andrey Andreev56454792012-05-17 14:32:19 +030088
Timothy Warrenb82bc3a2012-04-27 09:12:58 -040089 /**
90 * Predefined logging levels
91 *
92 * @var array
93 */
Andrey Andreev838a9d62012-12-03 14:37:47 +020094 protected $_levels = array('ERROR' => 1, 'DEBUG' => 2, 'INFO' => 3, 'ALL' => 4);
Derek Allard2067d1a2008-11-13 22:59:24 +000095
Andrey Andreeva107a0f2013-02-15 22:30:31 +020096 // --------------------------------------------------------------------
97
Timothy Warrenb82bc3a2012-04-27 09:12:58 -040098 /**
Andrey Andreeva107a0f2013-02-15 22:30:31 +020099 * Class constructor
Timothy Warrenb82bc3a2012-04-27 09:12:58 -0400100 *
Andrey Andreev56454792012-05-17 14:32:19 +0300101 * @return void
Timothy Warrenb82bc3a2012-04-27 09:12:58 -0400102 */
Greg Akera9263282010-11-10 15:26:43 -0600103 public function __construct()
Derek Allard2067d1a2008-11-13 22:59:24 +0000104 {
105 $config =& get_config();
Barry Mienydd671972010-10-04 16:33:58 +0200106
Alex Bilbied261b1e2012-06-02 11:12:16 +0100107 $this->_log_path = ($config['log_path'] !== '') ? $config['log_path'] : APPPATH.'logs/';
Andrey Andreeva107a0f2013-02-15 22:30:31 +0200108 $this->_file_ext = (isset($config['log_file_extension']) && $config['log_file_extension'] !== '')
109 ? ltrim($config['log_file_extension'], '.') : 'php';
Chris Passas0bd6b282013-02-13 14:16:18 -0500110
Andrey Andreev9f690f12013-01-21 15:30:25 +0200111 file_exists($this->_log_path) OR mkdir($this->_log_path, DIR_WRITE_MODE, TRUE);
112
Greg Aker2eaa4072010-12-21 11:44:08 -0600113 if ( ! is_dir($this->_log_path) OR ! is_really_writable($this->_log_path))
Derek Allard2067d1a2008-11-13 22:59:24 +0000114 {
115 $this->_enabled = FALSE;
116 }
Barry Mienydd671972010-10-04 16:33:58 +0200117
Derek Allard2067d1a2008-11-13 22:59:24 +0000118 if (is_numeric($config['log_threshold']))
119 {
Andrey Andreev76cc9202011-12-22 20:02:04 +0200120 $this->_threshold = (int) $config['log_threshold'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000121 }
Nithin333f9f92011-08-21 16:52:06 -0400122 elseif (is_array($config['log_threshold']))
123 {
124 $this->_threshold = $this->_threshold_max;
125 $this->_threshold_array = array_flip($config['log_threshold']);
126 }
Barry Mienydd671972010-10-04 16:33:58 +0200127
Alex Bilbied261b1e2012-06-02 11:12:16 +0100128 if ($config['log_date_format'] !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000129 {
130 $this->_date_fmt = $config['log_date_format'];
131 }
132 }
Barry Mienydd671972010-10-04 16:33:58 +0200133
Derek Allard2067d1a2008-11-13 22:59:24 +0000134 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200135
Derek Allard2067d1a2008-11-13 22:59:24 +0000136 /**
137 * Write Log File
138 *
139 * Generally this function will be called using the global log_message() function
140 *
vlakoffd0c30ab2013-05-07 07:49:23 +0200141 * @param string the error level: 'error', 'debug' or 'info'
Derek Allard2067d1a2008-11-13 22:59:24 +0000142 * @param string the error message
143 * @param bool whether the error is a native PHP error
144 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200145 */
Andrey Andreev838c9a92013-09-13 14:05:13 +0300146 public function write_log($level, $msg)
Barry Mienydd671972010-10-04 16:33:58 +0200147 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000148 if ($this->_enabled === FALSE)
149 {
150 return FALSE;
151 }
Barry Mienydd671972010-10-04 16:33:58 +0200152
Derek Allard2067d1a2008-11-13 22:59:24 +0000153 $level = strtoupper($level);
Barry Mienydd671972010-10-04 16:33:58 +0200154
Andrey Andreev76cc9202011-12-22 20:02:04 +0200155 if (( ! isset($this->_levels[$level]) OR ($this->_levels[$level] > $this->_threshold))
Andrey Andreev443bbd92012-04-03 15:49:11 +0300156 && ! isset($this->_threshold_array[$this->_levels[$level]]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000157 {
Andrey Andreev76cc9202011-12-22 20:02:04 +0200158 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000159 }
Barry Mienydd671972010-10-04 16:33:58 +0200160
Andrey Andreeva107a0f2013-02-15 22:30:31 +0200161 $filepath = $this->_log_path.'log-'.date('Y-m-d').'.'.$this->_file_ext;
162 $message = '';
Barry Mienydd671972010-10-04 16:33:58 +0200163
Derek Allard2067d1a2008-11-13 22:59:24 +0000164 if ( ! file_exists($filepath))
165 {
Jeroen van der Gulik2835e082011-12-04 14:19:38 +0100166 $newfile = TRUE;
Andrey Andreeva107a0f2013-02-15 22:30:31 +0200167 // Only add protection to php files
168 if ($this->_file_ext === 'php')
Chris Passasfb8de242013-02-14 09:01:24 -0500169 {
Andrey Andreeva107a0f2013-02-15 22:30:31 +0200170 $message .= "<?php defined('BASEPATH') OR exit('No direct script access allowed'); ?>\n\n";
Chris Passasfb8de242013-02-14 09:01:24 -0500171 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000172 }
Barry Mienydd671972010-10-04 16:33:58 +0200173
Derek Allard2067d1a2008-11-13 22:59:24 +0000174 if ( ! $fp = @fopen($filepath, FOPEN_WRITE_CREATE))
175 {
176 return FALSE;
177 }
178
Andrey Andreev56454792012-05-17 14:32:19 +0300179 $message .= $level.' '.($level === 'INFO' ? ' -' : '-').' '.date($this->_date_fmt).' --> '.$msg."\n";
Barry Mienydd671972010-10-04 16:33:58 +0200180
181 flock($fp, LOCK_EX);
Derek Allard2067d1a2008-11-13 22:59:24 +0000182 fwrite($fp, $message);
183 flock($fp, LOCK_UN);
184 fclose($fp);
Barry Mienydd671972010-10-04 16:33:58 +0200185
Andrey Andreev443bbd92012-04-03 15:49:11 +0300186 if (isset($newfile) && $newfile === TRUE)
Jeroen van der Gulik2835e082011-12-04 14:19:38 +0100187 {
188 @chmod($filepath, FILE_WRITE_MODE);
189 }
Andrey Andreev443bbd92012-04-03 15:49:11 +0300190
Derek Allard2067d1a2008-11-13 22:59:24 +0000191 return TRUE;
192 }
193
194}
Derek Allard2067d1a2008-11-13 22:59:24 +0000195
196/* End of file Log.php */
Chris Passas6f19fd72013-02-13 11:47:02 -0500197/* Location: ./system/core/Log.php */