blob: 4d64cd3287019dda58a2f2bc63f9e9642e74daac [file] [log] [blame]
Derek Jones0b59f272008-05-13 04:22:33 +00001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allardd2df9bc2007-04-15 17:41:17 +00002/**
3 * CodeIgniter
4 *
5 * An open source application development framework for PHP 4.3.2 or newer
6 *
7 * @package CodeIgniter
Derek Allard3d879d52008-01-18 19:41:32 +00008 * @author ExpressionEngine Dev Team
Derek Allardd2df9bc2007-04-15 17:41:17 +00009 * @copyright Copyright (c) 2006, EllisLab, Inc.
Derek Jones7a9193a2008-01-21 18:39:20 +000010 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
Derek Allardd2df9bc2007-04-15 17:41:17 +000012 * @since Version 1.0
13 * @filesource
14 */
15
16// ------------------------------------------------------------------------
17
18/**
19 * Logging Class
20 *
21 * @package CodeIgniter
22 * @subpackage Libraries
23 * @category Logging
Derek Allard3d879d52008-01-18 19:41:32 +000024 * @author ExpressionEngine Dev Team
Derek Jones7a9193a2008-01-21 18:39:20 +000025 * @link http://codeigniter.com/user_guide/general/errors.html
Derek Allardd2df9bc2007-04-15 17:41:17 +000026 */
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
37 *
38 * @access public
39 * @param string the log file path
40 * @param string the error threshold
41 * @param string the date formatting codes
42 */
43 function CI_Log()
44 {
45 $config =& get_config();
46
47 $this->log_path = ($config['log_path'] != '') ? $config['log_path'] : BASEPATH.'logs/';
48
Derek Jones0b59f272008-05-13 04:22:33 +000049 if ( ! is_dir($this->log_path) OR ! is_really_writable($this->log_path))
Derek Allardd2df9bc2007-04-15 17:41:17 +000050 {
51 $this->_enabled = FALSE;
52 }
53
54 if (is_numeric($config['log_threshold']))
55 {
56 $this->_threshold = $config['log_threshold'];
57 }
58
59 if ($config['log_date_format'] != '')
60 {
61 $this->_date_fmt = $config['log_date_format'];
62 }
63 }
64
65 // --------------------------------------------------------------------
66
67 /**
68 * Write Log File
69 *
70 * Generally this function will be called using the global log_message() function
71 *
72 * @access public
73 * @param string the error level
74 * @param string the error message
75 * @param bool whether the error is a native PHP error
76 * @return bool
77 */
78 function write_log($level = 'error', $msg, $php_error = FALSE)
79 {
80 if ($this->_enabled === FALSE)
81 {
82 return FALSE;
83 }
84
85 $level = strtoupper($level);
86
Derek Jones0b59f272008-05-13 04:22:33 +000087 if ( ! isset($this->_levels[$level]) OR ($this->_levels[$level] > $this->_threshold))
Derek Allardd2df9bc2007-04-15 17:41:17 +000088 {
89 return FALSE;
90 }
91
92 $filepath = $this->log_path.'log-'.date('Y-m-d').EXT;
93 $message = '';
94
Derek Jones0b59f272008-05-13 04:22:33 +000095 if ( ! file_exists($filepath))
Derek Allardd2df9bc2007-04-15 17:41:17 +000096 {
Derek Jones0b59f272008-05-13 04:22:33 +000097 $message .= "<"."?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); ?".">\n\n";
Derek Allardd2df9bc2007-04-15 17:41:17 +000098 }
99
Derek Jones0b59f272008-05-13 04:22:33 +0000100 if ( ! $fp = @fopen($filepath, FOPEN_WRITE_CREATE))
Derek Allardd2df9bc2007-04-15 17:41:17 +0000101 {
102 return FALSE;
103 }
104
105 $message .= $level.' '.(($level == 'INFO') ? ' -' : '-').' '.date($this->_date_fmt). ' --> '.$msg."\n";
106
107 flock($fp, LOCK_EX);
108 fwrite($fp, $message);
109 flock($fp, LOCK_UN);
110 fclose($fp);
111
Derek Jones3ad8efe2008-04-04 18:56:04 +0000112 @chmod($filepath, FILE_WRITE_MODE);
Derek Allardd2df9bc2007-04-15 17:41:17 +0000113 return TRUE;
114 }
115
116}
117// END Log Class
Derek Jones0b59f272008-05-13 04:22:33 +0000118
119/* End of file Log.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000120/* Location: ./system/libraries/Log.php */