blob: 6ea905f738fbf099dcdcf1d1b544e79f96fbddb5 [file] [log] [blame]
Derek Jones4b9c6292011-07-01 17:40:48 -05001<?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
8 *
9 * Licensed under the Open Software License version 3.0
10 *
11 * 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;
47 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 {
65 $this->_threshold = $config['log_threshold'];
66 }
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
Derek Allard2067d1a2008-11-13 22:59:24 +0000100 if ( ! isset($this->_levels[$level]) OR ($this->_levels[$level] > $this->_threshold))
101 {
Nithin333f9f92011-08-21 16:52:06 -0400102 if (empty($this->_threshold_array) OR ! isset($this->_threshold_array[$this->_levels[$level]]))
103 {
104 return FALSE;
105 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000106 }
Barry Mienydd671972010-10-04 16:33:58 +0200107
Nithin333f9f92011-08-21 16:52:06 -0400108
Greg Aker3a746652011-04-19 10:59:47 -0500109 $filepath = $this->_log_path.'log-'.date('Y-m-d').'.php';
Derek Jones4b9c6292011-07-01 17:40:48 -0500110 $message = '';
Barry Mienydd671972010-10-04 16:33:58 +0200111
Derek Allard2067d1a2008-11-13 22:59:24 +0000112 if ( ! file_exists($filepath))
113 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500114 $message .= "<"."?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); ?".">\n\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000115 }
Barry Mienydd671972010-10-04 16:33:58 +0200116
Derek Allard2067d1a2008-11-13 22:59:24 +0000117 if ( ! $fp = @fopen($filepath, FOPEN_WRITE_CREATE))
118 {
119 return FALSE;
120 }
121
122 $message .= $level.' '.(($level == 'INFO') ? ' -' : '-').' '.date($this->_date_fmt). ' --> '.$msg."\n";
Barry Mienydd671972010-10-04 16:33:58 +0200123
124 flock($fp, LOCK_EX);
Derek Allard2067d1a2008-11-13 22:59:24 +0000125 fwrite($fp, $message);
126 flock($fp, LOCK_UN);
127 fclose($fp);
Barry Mienydd671972010-10-04 16:33:58 +0200128
129 @chmod($filepath, FILE_WRITE_MODE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000130 return TRUE;
131 }
132
133}
134// END Log Class
135
136/* End of file Log.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000137/* Location: ./system/libraries/Log.php */