blob: 17b96b2c2c75a2854d43a602e0a0826fb4340077 [file] [log] [blame]
adminb0dd10f2006-08-25 17:25:49 +00001<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
2/**
3 * Code Igniter
4 *
5 * An open source application development framework for PHP 4.3.2 or newer
6 *
7 * @package CodeIgniter
8 * @author Rick Ellis
9 * @copyright Copyright (c) 2006, pMachine, Inc.
10 * @license http://www.codeignitor.com/user_guide/license.html
11 * @link http://www.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 Rick Ellis
25 * @link http://www.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
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($path = '', $threshold = '', $date_fmt = '')
44 {
45 $this->log_path = ($path != '') ? $path : BASEPATH.'logs/';
46
47 if ( ! is_dir($this->log_path) OR ! is_writable($this->log_path))
48 {
49 $this->_enabled = FALSE;
50 }
51
52 if (ctype_digit($threshold))
53 {
54 $this->_threshold = $threshold;
55 }
56
57 if ($date_fmt != '')
58 {
59 $this->_date_fmt = $date_fmt;
60 }
61 }
62 // END CI_Log()
63
64 // --------------------------------------------------------------------
65
66 /**
67 * Write Log File
68 *
69 * Generally this function will be called using the global log_message() function
70 *
71 * @access public
72 * @param string the error level
73 * @param string the error message
74 * @param bool whether the error is a native PHP error
75 * @return bool
76 */
77 function write_log($level = 'error', $msg, $php_error = FALSE)
78 {
79 if ($this->_enabled === FALSE)
80 {
81 return FALSE;
82 }
83
84 $level = strtoupper($level);
85
86 if ( ! isset($this->_levels[$level]) OR ($this->_levels[$level] > $this->_threshold))
87 {
88 return FALSE;
89 }
90
admin141808a2006-08-27 01:52:51 +000091 $filepath = $this->log_path.'log-'.date('Y-m-d').EXT;
adminb0dd10f2006-08-25 17:25:49 +000092 $message = '';
93
94 if ( ! file_exists($filepath))
95 {
96 $message .= "<"."?php if (!defined('BASEPATH')) exit('No direct script access allowed'); ?".">\n\n";
97 }
98
99 if ( ! $fp = @fopen($filepath, "a"))
100 {
101 return FALSE;
102 }
103
104 $message .= $level.' '.(($level == 'INFO') ? ' -' : '-').' '.date($this->_date_fmt). ' --> '.$msg."\n";
105
106 flock($fp, LOCK_EX);
107 fwrite($fp, $message);
108 flock($fp, LOCK_UN);
109 fclose($fp);
110
111 @chmod($filepath, 0666);
112 return TRUE;
113 }
114 // END write_log()
115}
116// END Log Class
117?>