blob: 66f9ebff9acc21a4b7421148610fea4af0ada726 [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
Greg Aker2eaa4072010-12-21 11:44:08 -060039 protected $_log_path;
Nithin333f9f92011-08-21 16:52:06 -040040 protected $_threshold = 1;
41 protected $_threshold_max = 0;
42 protected $_threshold_array = array();
43 protected $_date_fmt = 'Y-m-d H:i:s';
Andrey Andreev443bbd92012-04-03 15:49:11 +030044 protected $_enabled = TRUE;
45 protected $_levels = array('ERROR' => 1, 'DEBUG' => 2, 'INFO' => 3, 'ALL' => 4);
Derek Allard2067d1a2008-11-13 22:59:24 +000046
Greg Akera9263282010-11-10 15:26:43 -060047 public function __construct()
Derek Allard2067d1a2008-11-13 22:59:24 +000048 {
49 $config =& get_config();
Barry Mienydd671972010-10-04 16:33:58 +020050
Greg Aker2eaa4072010-12-21 11:44:08 -060051 $this->_log_path = ($config['log_path'] != '') ? $config['log_path'] : APPPATH.'logs/';
Barry Mienydd671972010-10-04 16:33:58 +020052
Greg Aker2eaa4072010-12-21 11:44:08 -060053 if ( ! is_dir($this->_log_path) OR ! is_really_writable($this->_log_path))
Derek Allard2067d1a2008-11-13 22:59:24 +000054 {
55 $this->_enabled = FALSE;
56 }
Barry Mienydd671972010-10-04 16:33:58 +020057
Derek Allard2067d1a2008-11-13 22:59:24 +000058 if (is_numeric($config['log_threshold']))
59 {
Andrey Andreev76cc9202011-12-22 20:02:04 +020060 $this->_threshold = (int) $config['log_threshold'];
Derek Allard2067d1a2008-11-13 22:59:24 +000061 }
Nithin333f9f92011-08-21 16:52:06 -040062 elseif (is_array($config['log_threshold']))
63 {
64 $this->_threshold = $this->_threshold_max;
65 $this->_threshold_array = array_flip($config['log_threshold']);
66 }
Barry Mienydd671972010-10-04 16:33:58 +020067
Derek Allard2067d1a2008-11-13 22:59:24 +000068 if ($config['log_date_format'] != '')
69 {
70 $this->_date_fmt = $config['log_date_format'];
71 }
72 }
Barry Mienydd671972010-10-04 16:33:58 +020073
Derek Allard2067d1a2008-11-13 22:59:24 +000074 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +020075
Derek Allard2067d1a2008-11-13 22:59:24 +000076 /**
77 * Write Log File
78 *
79 * Generally this function will be called using the global log_message() function
80 *
Derek Allard2067d1a2008-11-13 22:59:24 +000081 * @param string the error level
82 * @param string the error message
83 * @param bool whether the error is a native PHP error
84 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +020085 */
Greg Aker2eaa4072010-12-21 11:44:08 -060086 public function write_log($level = 'error', $msg, $php_error = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +020087 {
Derek Allard2067d1a2008-11-13 22:59:24 +000088 if ($this->_enabled === FALSE)
89 {
90 return FALSE;
91 }
Barry Mienydd671972010-10-04 16:33:58 +020092
Derek Allard2067d1a2008-11-13 22:59:24 +000093 $level = strtoupper($level);
Barry Mienydd671972010-10-04 16:33:58 +020094
Andrey Andreev76cc9202011-12-22 20:02:04 +020095 if (( ! isset($this->_levels[$level]) OR ($this->_levels[$level] > $this->_threshold))
Andrey Andreev443bbd92012-04-03 15:49:11 +030096 && ! isset($this->_threshold_array[$this->_levels[$level]]))
Derek Allard2067d1a2008-11-13 22:59:24 +000097 {
Andrey Andreev76cc9202011-12-22 20:02:04 +020098 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +000099 }
Barry Mienydd671972010-10-04 16:33:58 +0200100
Nithin333f9f92011-08-21 16:52:06 -0400101
Greg Aker3a746652011-04-19 10:59:47 -0500102 $filepath = $this->_log_path.'log-'.date('Y-m-d').'.php';
Derek Jones4b9c6292011-07-01 17:40:48 -0500103 $message = '';
Barry Mienydd671972010-10-04 16:33:58 +0200104
Derek Allard2067d1a2008-11-13 22:59:24 +0000105 if ( ! file_exists($filepath))
106 {
Jeroen van der Gulik2835e082011-12-04 14:19:38 +0100107 $newfile = TRUE;
Andrey Andreev76cc9202011-12-22 20:02:04 +0200108 $message .= "<"."?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); ?".">\n\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000109 }
Barry Mienydd671972010-10-04 16:33:58 +0200110
Derek Allard2067d1a2008-11-13 22:59:24 +0000111 if ( ! $fp = @fopen($filepath, FOPEN_WRITE_CREATE))
112 {
113 return FALSE;
114 }
115
116 $message .= $level.' '.(($level == 'INFO') ? ' -' : '-').' '.date($this->_date_fmt). ' --> '.$msg."\n";
Barry Mienydd671972010-10-04 16:33:58 +0200117
118 flock($fp, LOCK_EX);
Derek Allard2067d1a2008-11-13 22:59:24 +0000119 fwrite($fp, $message);
120 flock($fp, LOCK_UN);
121 fclose($fp);
Barry Mienydd671972010-10-04 16:33:58 +0200122
Andrey Andreev443bbd92012-04-03 15:49:11 +0300123 if (isset($newfile) && $newfile === TRUE)
Jeroen van der Gulik2835e082011-12-04 14:19:38 +0100124 {
125 @chmod($filepath, FILE_WRITE_MODE);
126 }
Andrey Andreev443bbd92012-04-03 15:49:11 +0300127
Derek Allard2067d1a2008-11-13 22:59:24 +0000128 return TRUE;
129 }
130
131}
Derek Allard2067d1a2008-11-13 22:59:24 +0000132
133/* End of file Log.php */
Andrey Andreev443bbd92012-04-03 15:49:11 +0300134/* Location: ./system/libraries/Log.php */