blob: 7f3a307f3a82743c937f451fc97dd6de2443d495 [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 *
Greg Aker741de1c2010-11-10 14:52:57 -06005 * An open source application development framework for PHP 5.1.6 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
21 * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc. (http://ellislab.com/)
22 * @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
28// ------------------------------------------------------------------------
29
30/**
31 * Logging Class
32 *
33 * @package CodeIgniter
34 * @subpackage Libraries
35 * @category Logging
Derek Jonesf4a4bd82011-10-20 12:18:42 -050036 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000037 * @link http://codeigniter.com/user_guide/general/errors.html
38 */
39class CI_Log {
40
Greg Aker2eaa4072010-12-21 11:44:08 -060041 protected $_log_path;
Nithin333f9f92011-08-21 16:52:06 -040042 protected $_threshold = 1;
43 protected $_threshold_max = 0;
44 protected $_threshold_array = array();
45 protected $_date_fmt = 'Y-m-d H:i:s';
46 protected $_enabled = TRUE;
Andrey Andreev76cc9202011-12-22 20:02:04 +020047 protected $_levels = array('ERROR' => 1, 'DEBUG' => 2, 'INFO' => 3, 'ALL' => 4);
Derek Allard2067d1a2008-11-13 22:59:24 +000048
49 /**
50 * Constructor
Derek Allard2067d1a2008-11-13 22:59:24 +000051 */
Greg Akera9263282010-11-10 15:26:43 -060052 public function __construct()
Derek Allard2067d1a2008-11-13 22:59:24 +000053 {
54 $config =& get_config();
Barry Mienydd671972010-10-04 16:33:58 +020055
Greg Aker2eaa4072010-12-21 11:44:08 -060056 $this->_log_path = ($config['log_path'] != '') ? $config['log_path'] : APPPATH.'logs/';
Barry Mienydd671972010-10-04 16:33:58 +020057
Greg Aker2eaa4072010-12-21 11:44:08 -060058 if ( ! is_dir($this->_log_path) OR ! is_really_writable($this->_log_path))
Derek Allard2067d1a2008-11-13 22:59:24 +000059 {
60 $this->_enabled = FALSE;
61 }
Barry Mienydd671972010-10-04 16:33:58 +020062
Derek Allard2067d1a2008-11-13 22:59:24 +000063 if (is_numeric($config['log_threshold']))
64 {
Andrey Andreev76cc9202011-12-22 20:02:04 +020065 $this->_threshold = (int) $config['log_threshold'];
Derek Allard2067d1a2008-11-13 22:59:24 +000066 }
Nithin333f9f92011-08-21 16:52:06 -040067 elseif (is_array($config['log_threshold']))
68 {
69 $this->_threshold = $this->_threshold_max;
70 $this->_threshold_array = array_flip($config['log_threshold']);
71 }
Barry Mienydd671972010-10-04 16:33:58 +020072
Derek Allard2067d1a2008-11-13 22:59:24 +000073 if ($config['log_date_format'] != '')
74 {
75 $this->_date_fmt = $config['log_date_format'];
76 }
77 }
Barry Mienydd671972010-10-04 16:33:58 +020078
Derek Allard2067d1a2008-11-13 22:59:24 +000079 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +020080
Derek Allard2067d1a2008-11-13 22:59:24 +000081 /**
82 * Write Log File
83 *
84 * Generally this function will be called using the global log_message() function
85 *
Derek Allard2067d1a2008-11-13 22:59:24 +000086 * @param string the error level
87 * @param string the error message
88 * @param bool whether the error is a native PHP error
89 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +020090 */
Greg Aker2eaa4072010-12-21 11:44:08 -060091 public function write_log($level = 'error', $msg, $php_error = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +020092 {
Derek Allard2067d1a2008-11-13 22:59:24 +000093 if ($this->_enabled === FALSE)
94 {
95 return FALSE;
96 }
Barry Mienydd671972010-10-04 16:33:58 +020097
Derek Allard2067d1a2008-11-13 22:59:24 +000098 $level = strtoupper($level);
Barry Mienydd671972010-10-04 16:33:58 +020099
Andrey Andreev76cc9202011-12-22 20:02:04 +0200100 if (( ! isset($this->_levels[$level]) OR ($this->_levels[$level] > $this->_threshold))
101 AND ! isset($this->_threshold_array[$this->_levels[$level]]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000102 {
Andrey Andreev76cc9202011-12-22 20:02:04 +0200103 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000104 }
Barry Mienydd671972010-10-04 16:33:58 +0200105
Nithin333f9f92011-08-21 16:52:06 -0400106
Greg Aker3a746652011-04-19 10:59:47 -0500107 $filepath = $this->_log_path.'log-'.date('Y-m-d').'.php';
Derek Jones4b9c6292011-07-01 17:40:48 -0500108 $message = '';
Barry Mienydd671972010-10-04 16:33:58 +0200109
Derek Allard2067d1a2008-11-13 22:59:24 +0000110 if ( ! file_exists($filepath))
111 {
Jeroen van der Gulik2835e082011-12-04 14:19:38 +0100112 $newfile = TRUE;
Andrey Andreev76cc9202011-12-22 20:02:04 +0200113 $message .= "<"."?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); ?".">\n\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000114 }
Barry Mienydd671972010-10-04 16:33:58 +0200115
Derek Allard2067d1a2008-11-13 22:59:24 +0000116 if ( ! $fp = @fopen($filepath, FOPEN_WRITE_CREATE))
117 {
118 return FALSE;
119 }
120
121 $message .= $level.' '.(($level == 'INFO') ? ' -' : '-').' '.date($this->_date_fmt). ' --> '.$msg."\n";
Barry Mienydd671972010-10-04 16:33:58 +0200122
123 flock($fp, LOCK_EX);
Derek Allard2067d1a2008-11-13 22:59:24 +0000124 fwrite($fp, $message);
125 flock($fp, LOCK_UN);
126 fclose($fp);
Barry Mienydd671972010-10-04 16:33:58 +0200127
Jeroen van der Gulik2835e082011-12-04 14:19:38 +0100128 if (isset($newfile) AND $newfile === TRUE)
129 {
130 @chmod($filepath, FILE_WRITE_MODE);
131 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000132 return TRUE;
133 }
134
135}
136// END Log Class
137
138/* End of file Log.php */
Jeroen van der Gulik2835e082011-12-04 14:19:38 +0100139/* Location: ./system/libraries/Log.php */