blob: baac80121adb79618a91a02e84e49f1f0917d367 [file] [log] [blame]
Andrey Andreev76cc9202011-12-22 20:02:04 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
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
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, 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 */
27
Derek Allard2067d1a2008-11-13 22:59:24 +000028/**
29 * Logging Class
30 *
31 * @package CodeIgniter
32 * @subpackage Libraries
33 * @category Logging
Derek Jonesf4a4bd82011-10-20 12:18:42 -050034 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000035 * @link http://codeigniter.com/user_guide/general/errors.html
36 */
37class CI_Log {
38
Timothy Warrenb82bc3a2012-04-27 09:12:58 -040039 /**
40 * Path to save log files
41 *
42 * @var string
43 */
Greg Aker2eaa4072010-12-21 11:44:08 -060044 protected $_log_path;
Andrey Andreev56454792012-05-17 14:32:19 +030045
Timothy Warrenb82bc3a2012-04-27 09:12:58 -040046 /**
47 * Level of logging
48 *
49 * @var int
50 */
Nithin333f9f92011-08-21 16:52:06 -040051 protected $_threshold = 1;
Andrey Andreev56454792012-05-17 14:32:19 +030052
Timothy Warrenb82bc3a2012-04-27 09:12:58 -040053 /**
54 * Highest level of logging
55 *
56 * @var int
57 */
Nithin333f9f92011-08-21 16:52:06 -040058 protected $_threshold_max = 0;
Andrey Andreev56454792012-05-17 14:32:19 +030059
Timothy Warrenb82bc3a2012-04-27 09:12:58 -040060 /**
61 * Array of threshold levels to log
62 *
63 * @var array
64 */
Nithin333f9f92011-08-21 16:52:06 -040065 protected $_threshold_array = array();
Andrey Andreev56454792012-05-17 14:32:19 +030066
Timothy Warrenb82bc3a2012-04-27 09:12:58 -040067 /**
68 * Format of timestamp for log files
69 *
70 * @var string
71 */
Nithin333f9f92011-08-21 16:52:06 -040072 protected $_date_fmt = 'Y-m-d H:i:s';
Andrey Andreev56454792012-05-17 14:32:19 +030073
Timothy Warrenb82bc3a2012-04-27 09:12:58 -040074 /**
75 * Whether or not the logger can write to the log files
76 *
77 * @var bool
78 */
Andrey Andreev443bbd92012-04-03 15:49:11 +030079 protected $_enabled = TRUE;
Andrey Andreev56454792012-05-17 14:32:19 +030080
Timothy Warrenb82bc3a2012-04-27 09:12:58 -040081 /**
82 * Predefined logging levels
83 *
84 * @var array
85 */
Andrey Andreev443bbd92012-04-03 15:49:11 +030086 protected $_levels = array('ERROR' => 1, 'DEBUG' => 2, 'INFO' => 3, 'ALL' => 4);
Derek Allard2067d1a2008-11-13 22:59:24 +000087
Timothy Warrenb82bc3a2012-04-27 09:12:58 -040088 /**
89 * Initialize Logging class
90 *
Andrey Andreev56454792012-05-17 14:32:19 +030091 * @return void
Timothy Warrenb82bc3a2012-04-27 09:12:58 -040092 */
Greg Akera9263282010-11-10 15:26:43 -060093 public function __construct()
Derek Allard2067d1a2008-11-13 22:59:24 +000094 {
95 $config =& get_config();
Barry Mienydd671972010-10-04 16:33:58 +020096
Alex Bilbied261b1e2012-06-02 11:12:16 +010097 $this->_log_path = ($config['log_path'] !== '') ? $config['log_path'] : APPPATH.'logs/';
Barry Mienydd671972010-10-04 16:33:58 +020098
Greg Aker2eaa4072010-12-21 11:44:08 -060099 if ( ! is_dir($this->_log_path) OR ! is_really_writable($this->_log_path))
Derek Allard2067d1a2008-11-13 22:59:24 +0000100 {
101 $this->_enabled = FALSE;
102 }
Barry Mienydd671972010-10-04 16:33:58 +0200103
Derek Allard2067d1a2008-11-13 22:59:24 +0000104 if (is_numeric($config['log_threshold']))
105 {
Andrey Andreev76cc9202011-12-22 20:02:04 +0200106 $this->_threshold = (int) $config['log_threshold'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000107 }
Nithin333f9f92011-08-21 16:52:06 -0400108 elseif (is_array($config['log_threshold']))
109 {
110 $this->_threshold = $this->_threshold_max;
111 $this->_threshold_array = array_flip($config['log_threshold']);
112 }
Barry Mienydd671972010-10-04 16:33:58 +0200113
Alex Bilbied261b1e2012-06-02 11:12:16 +0100114 if ($config['log_date_format'] !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000115 {
116 $this->_date_fmt = $config['log_date_format'];
117 }
118 }
Barry Mienydd671972010-10-04 16:33:58 +0200119
Derek Allard2067d1a2008-11-13 22:59:24 +0000120 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200121
Derek Allard2067d1a2008-11-13 22:59:24 +0000122 /**
123 * Write Log File
124 *
125 * Generally this function will be called using the global log_message() function
126 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000127 * @param string the error level
128 * @param string the error message
129 * @param bool whether the error is a native PHP error
130 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200131 */
Greg Aker2eaa4072010-12-21 11:44:08 -0600132 public function write_log($level = 'error', $msg, $php_error = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200133 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000134 if ($this->_enabled === FALSE)
135 {
136 return FALSE;
137 }
Barry Mienydd671972010-10-04 16:33:58 +0200138
Derek Allard2067d1a2008-11-13 22:59:24 +0000139 $level = strtoupper($level);
Barry Mienydd671972010-10-04 16:33:58 +0200140
Andrey Andreev76cc9202011-12-22 20:02:04 +0200141 if (( ! isset($this->_levels[$level]) OR ($this->_levels[$level] > $this->_threshold))
Andrey Andreev443bbd92012-04-03 15:49:11 +0300142 && ! isset($this->_threshold_array[$this->_levels[$level]]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000143 {
Andrey Andreev76cc9202011-12-22 20:02:04 +0200144 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000145 }
Barry Mienydd671972010-10-04 16:33:58 +0200146
Nithin333f9f92011-08-21 16:52:06 -0400147
Greg Aker3a746652011-04-19 10:59:47 -0500148 $filepath = $this->_log_path.'log-'.date('Y-m-d').'.php';
Derek Jones4b9c6292011-07-01 17:40:48 -0500149 $message = '';
Barry Mienydd671972010-10-04 16:33:58 +0200150
Derek Allard2067d1a2008-11-13 22:59:24 +0000151 if ( ! file_exists($filepath))
152 {
Jeroen van der Gulik2835e082011-12-04 14:19:38 +0100153 $newfile = TRUE;
Andrey Andreev56454792012-05-17 14:32:19 +0300154 $message .= '<'."?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); ?".">\n\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000155 }
Barry Mienydd671972010-10-04 16:33:58 +0200156
Derek Allard2067d1a2008-11-13 22:59:24 +0000157 if ( ! $fp = @fopen($filepath, FOPEN_WRITE_CREATE))
158 {
159 return FALSE;
160 }
161
Andrey Andreev56454792012-05-17 14:32:19 +0300162 $message .= $level.' '.($level === 'INFO' ? ' -' : '-').' '.date($this->_date_fmt).' --> '.$msg."\n";
Barry Mienydd671972010-10-04 16:33:58 +0200163
164 flock($fp, LOCK_EX);
Derek Allard2067d1a2008-11-13 22:59:24 +0000165 fwrite($fp, $message);
166 flock($fp, LOCK_UN);
167 fclose($fp);
Barry Mienydd671972010-10-04 16:33:58 +0200168
Andrey Andreev443bbd92012-04-03 15:49:11 +0300169 if (isset($newfile) && $newfile === TRUE)
Jeroen van der Gulik2835e082011-12-04 14:19:38 +0100170 {
171 @chmod($filepath, FILE_WRITE_MODE);
172 }
Andrey Andreev443bbd92012-04-03 15:49:11 +0300173
Derek Allard2067d1a2008-11-13 22:59:24 +0000174 return TRUE;
175 }
176
177}
Derek Allard2067d1a2008-11-13 22:59:24 +0000178
179/* End of file Log.php */
Andrey Andreev443bbd92012-04-03 15:49:11 +0300180/* Location: ./system/libraries/Log.php */