blob: 6d44cfa4b8e46f51a4014e2384a54fb28f3a5eea [file] [log] [blame]
Derek Allard2067d1a2008-11-13 22:59:24 +00001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2/**
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 *
7 * @package CodeIgniter
8 * @author ExpressionEngine Dev Team
Derek Jones7f3719f2010-01-05 13:35:37 +00009 * @copyright Copyright (c) 2008 - 2010, EllisLab, Inc.
Derek Allard2067d1a2008-11-13 22:59:24 +000010 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
12 * @since Version 1.0
13 * @filesource
14 */
15
16// ------------------------------------------------------------------------
17
18/**
19 * Logging Class
20 *
21 * @package CodeIgniter
22 * @subpackage Libraries
23 * @category Logging
24 * @author ExpressionEngine Dev Team
25 * @link http://codeigniter.com/user_guide/general/errors.html
26 */
27class CI_Log {
28
29 var $log_path;
30 var $_threshold = 1;
31 var $_date_fmt = 'Y-m-d H:i:s';
32 var $_enabled = TRUE;
33 var $_levels = array('ERROR' => '1', 'DEBUG' => '2', 'INFO' => '3', 'ALL' => '4');
34
35 /**
36 * Constructor
Derek Allard2067d1a2008-11-13 22:59:24 +000037 */
Greg Akera9263282010-11-10 15:26:43 -060038 public function __construct()
Derek Allard2067d1a2008-11-13 22:59:24 +000039 {
40 $config =& get_config();
Barry Mienydd671972010-10-04 16:33:58 +020041
Derek Allard2067d1a2008-11-13 22:59:24 +000042 $this->log_path = ($config['log_path'] != '') ? $config['log_path'] : BASEPATH.'logs/';
Barry Mienydd671972010-10-04 16:33:58 +020043
Derek Allard2067d1a2008-11-13 22:59:24 +000044 if ( ! is_dir($this->log_path) OR ! is_really_writable($this->log_path))
45 {
46 $this->_enabled = FALSE;
47 }
Barry Mienydd671972010-10-04 16:33:58 +020048
Derek Allard2067d1a2008-11-13 22:59:24 +000049 if (is_numeric($config['log_threshold']))
50 {
51 $this->_threshold = $config['log_threshold'];
52 }
Barry Mienydd671972010-10-04 16:33:58 +020053
Derek Allard2067d1a2008-11-13 22:59:24 +000054 if ($config['log_date_format'] != '')
55 {
56 $this->_date_fmt = $config['log_date_format'];
57 }
58 }
Barry Mienydd671972010-10-04 16:33:58 +020059
Derek Allard2067d1a2008-11-13 22:59:24 +000060 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +020061
Derek Allard2067d1a2008-11-13 22:59:24 +000062 /**
63 * Write Log File
64 *
65 * Generally this function will be called using the global log_message() function
66 *
67 * @access public
68 * @param string the error level
69 * @param string the error message
70 * @param bool whether the error is a native PHP error
71 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +020072 */
Derek Allard2067d1a2008-11-13 22:59:24 +000073 function write_log($level = 'error', $msg, $php_error = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +020074 {
Derek Allard2067d1a2008-11-13 22:59:24 +000075 if ($this->_enabled === FALSE)
76 {
77 return FALSE;
78 }
Barry Mienydd671972010-10-04 16:33:58 +020079
Derek Allard2067d1a2008-11-13 22:59:24 +000080 $level = strtoupper($level);
Barry Mienydd671972010-10-04 16:33:58 +020081
Derek Allard2067d1a2008-11-13 22:59:24 +000082 if ( ! isset($this->_levels[$level]) OR ($this->_levels[$level] > $this->_threshold))
83 {
84 return FALSE;
85 }
Barry Mienydd671972010-10-04 16:33:58 +020086
Derek Allard2067d1a2008-11-13 22:59:24 +000087 $filepath = $this->log_path.'log-'.date('Y-m-d').EXT;
88 $message = '';
Barry Mienydd671972010-10-04 16:33:58 +020089
Derek Allard2067d1a2008-11-13 22:59:24 +000090 if ( ! file_exists($filepath))
91 {
92 $message .= "<"."?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); ?".">\n\n";
93 }
Barry Mienydd671972010-10-04 16:33:58 +020094
Derek Allard2067d1a2008-11-13 22:59:24 +000095 if ( ! $fp = @fopen($filepath, FOPEN_WRITE_CREATE))
96 {
97 return FALSE;
98 }
99
100 $message .= $level.' '.(($level == 'INFO') ? ' -' : '-').' '.date($this->_date_fmt). ' --> '.$msg."\n";
Barry Mienydd671972010-10-04 16:33:58 +0200101
102 flock($fp, LOCK_EX);
Derek Allard2067d1a2008-11-13 22:59:24 +0000103 fwrite($fp, $message);
104 flock($fp, LOCK_UN);
105 fclose($fp);
Barry Mienydd671972010-10-04 16:33:58 +0200106
107 @chmod($filepath, FILE_WRITE_MODE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000108 return TRUE;
109 }
110
111}
112// END Log Class
113
114/* End of file Log.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000115/* Location: ./system/libraries/Log.php */