Fixing issues with the scaffold generator (custom actions did not work).
[akelos.git] / lib / AkLogger.php
blobd8cb51d83d7977b276c8dcd7fb28ebbe1b8882e5
1 <?php
2 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4 // WARNING. This is experimental. We might replace this by Logger4PHP
6 // +----------------------------------------------------------------------+
7 // | Akelos Framework - http://www.akelos.org |
8 // +----------------------------------------------------------------------+
9 // | Copyright (c) 2002-2006, Akelos Media, S.L. & Bermi Ferrer Martinez |
10 // | Released under the GNU Lesser General Public License, see LICENSE.txt|
11 // +----------------------------------------------------------------------+
13 /**
14 * @package ActiveSupport
15 * @subpackage Reporting
16 * @author Bermi Ferrer <bermi a.t akelos c.om>
17 * @copyright Copyright (c) 2002-2006, Akelos Media, S.L. http://www.akelos.org
18 * @license GNU Lesser General Public License <http://www.gnu.org/copyleft/lesser.html>
21 require_once(AK_LIB_DIR.DS.'Ak.php');
23 defined('AK_LOG_DIR') ? null : define('AK_LOG_DIR', AK_BASE_DIR.DS.'log');
25 // Default mail logger settings
26 defined('AK_LOGER_DEFAULT_MAIL_DESTINATION') ? null : define('AK_LOGER_DEFAULT_MAIL_DESTINATION', false);
27 defined('AK_LOGER_DEFAULT_MAIL_SENDER') ? null : define('AK_LOGER_DEFAULT_MAIL_SENDER', AK_HOST);
28 defined('AK_LOGER_DEFAULT_MAIL_SUBJECT') ? null : define('AK_LOGER_DEFAULT_MAIL_SUBJECT', 'Log message');
30 // Default file logger settings
31 defined('AK_LOGER_DEFAULT_LOG_FILE') ? null : define('AK_LOGER_DEFAULT_LOG_FILE', AK_LOG_DIR.DS.AK_ENVIRONMENT.'.log');
33 // Loggin events for log types
34 defined('AK_LOGGER_DEBUG') ? null : define('AK_LOGGER_DEBUG', AK_MODE_FILE | AK_MODE_DISPLAY);
35 defined('AK_LOGGER_INFO') ? null : define('AK_LOGGER_INFO', AK_MODE_DISPLAY);
36 defined('AK_LOGGER_MESSAGE') ? null : define('AK_LOGGER_MESSAGE', AK_MODE_FILE);
37 defined('AK_LOGGER_NOTICE') ? null : define('AK_LOGGER_NOTICE', AK_MODE_DISPLAY | AK_MODE_FILE | AK_MODE_DIE);
38 defined('AK_LOGGER_WARNING') ? null : define('AK_LOGGER_WARNING', AK_MODE_DISPLAY | AK_MODE_FILE | AK_MODE_DIE);
39 defined('AK_LOGGER_ERROR') ? null : define('AK_LOGGER_ERROR', AK_MODE_DISPLAY | AK_MODE_FILE | AK_MODE_DIE);
40 defined('AK_LOGGER_CRITICAL') ? null : define('AK_LOGGER_CRITICAL', AK_MODE_FILE | AK_MODE_DIE);
42 // Error loggin settings
43 defined('AK_LOG_'.E_USER_ERROR) ? null : define('AK_LOG_'.E_USER_ERROR, AK_MODE_FILE | AK_MODE_DIE);
44 defined('AK_LOG_'.E_USER_WARNING) ? null : define('AK_LOG_'.E_USER_WARNING, AK_MODE_DISPLAY | AK_MODE_FILE | AK_MODE_DIE);
45 defined('AK_LOG_'.E_USER_NOTICE) ? null : define('AK_LOG_'.E_USER_NOTICE, AK_MODE_DISPLAY | AK_MODE_FILE | AK_MODE_DIE);
46 defined('AK_LOG_'.E_WARNING) ? null : define('AK_LOG_'.E_WARNING, AK_MODE_FILE);
47 defined('AK_LOG_'.E_NOTICE) ? null : define('AK_LOG_'.E_NOTICE, AK_MODE_FILE);
49 defined('AK_LOG_ENABLE_COLORING') ? null : define('AK_LOG_ENABLE_COLORING', true);
51 class AkLogger
53 var $_log_params = array();
54 var $print_display_message = true;
55 var $extended_details = false;
56 var $default_mail_destination = AK_LOGER_DEFAULT_MAIL_DESTINATION;
57 var $default_mail_sender = AK_LOGER_DEFAULT_MAIL_SENDER;
58 var $default_mail_subject = AK_LOGER_DEFAULT_MAIL_SUBJECT;
59 var $error_file = AK_LOGER_DEFAULT_LOG_FILE;
60 var $log_type;
62 function AkLogger($mode = AK_LOGGER_MESSAGE)
64 $this->default_log_settings = $mode;
67 function log($type, $message, $vars = array(), $event_code = null)
69 $type = strtoupper($type);
70 $event_code = empty ($event_code) ? (defined('AK_LOGGER_'.$type) ? 'AK_LOGGER_'.$type : AK_LOGGER_INFO) : $event_code;
71 $this->_log($type, $message, $vars, $event_code);
74 function debug($message, $vars = array(), $event_code = null)
76 $this->log(__FUNCTION__, $message, $vars, $event_code);
79 function info($message, $vars = array(), $event_code = null)
81 $this->log(__FUNCTION__, $message, $vars, $event_code);
84 function message($message, $vars = array(), $event_code = null)
86 $this->log(__FUNCTION__, $message, $vars, $event_code);
89 function notice($message, $vars = array(), $event_code = null)
91 $this->log(__FUNCTION__, $message, $vars, $event_code);
94 function warning($message, $vars = array(), $event_code = null)
96 $this->log(__FUNCTION__, $message, $vars, $event_code);
99 function error($message, $vars = array(), $event_code = null)
101 $this->log(__FUNCTION__, $message, $vars, $event_code);
104 function critical($message, $vars = array(), $event_code = null)
106 $this->log(__FUNCTION__, $message, $vars, $event_code);
109 function _log($error_mode, $error_message, $vars=array(), $event_code = null)
111 $this->setLogParams($vars);
112 $this->mode = defined('AK_LOG_'.$error_mode) ? constant('AK_LOG_'.$error_mode) : $this->default_log_settings;
113 $type = $this->log_type;
114 $this->mode & AK_MODE_DISPLAY ? $this->_displayLog($type, $error_mode, $error_message) : null;
115 $this->mode & AK_MODE_FILE ? $this->_appendLogToFile($type, $error_mode, $error_message) : null;
116 $this->mode & AK_MODE_DATABASE ? $this->_saveLogInDatabase($type, $error_mode, $error_message) : null;
117 $this->mode & AK_MODE_MAIL ? $this->_mailLog($type, $error_mode, $error_message) : null;
118 $this->mode & AK_MODE_DIE ? exit : null;
121 function _displayLog($type, $error_mode, $error_message)
123 $message = $this->_getLogFormatedAsHtml($type, $error_mode, $error_message);
124 if($this->print_display_message){
125 Ak::trace($message);
127 return $message;
129 function _mailLog($type, $error_mode, $error_message)
131 if(!empty($this->default_mail_destination)){
132 $message = $this->_getLogFormatedAsString($type, $error_mode, $error_message);
133 $message = strip_tags(str_replace('<li>',' - ',$message));
134 Ak::mail($this->default_mail_sender, $this->default_mail_destination, $this->default_mail_subject, $message);
137 function _appendLogToFile($type, $error_mode, $error_message)
139 $filename = $this->error_file;
140 if(!is_writable($filename)){
141 clearstatcache();
142 Ak::file_put_contents($filename,'');
143 if(!is_writable($filename)){
144 trigger_error($this->internalError($this->t('Error writing file: %filename Description:',array('%filename'=>$filename)).$error_message,__FILE__,__LINE__), E_USER_NOTICE);
145 return ;
149 $message = $this->_getLogFormatedAsString($type, $error_mode, $error_message);
150 if(!$fp = fopen($filename, 'a')) {
151 die($this->internalError($this->t('Cannot open file (%file)', array('%file'=>$filename)),__FILE__,__LINE__));
153 @flock($fp, LOCK_EX);
154 if (@fwrite($fp, $message) === FALSE) {
155 @flock ($fp, LOCK_UN);
156 die($this->internalError($this->t('Error writing file: %filename Description:',array('%filename'=>$filename)).$error_message,__FILE__,__LINE__));
158 @flock ($fp, LOCK_UN);
159 @fclose($fp);
162 function _saveLogInDatabase($type, $error_mode, $error_message)
164 $db =& Ak::db();
165 $message = $this->_getLogFormatedAsRawText($type, $error_mode, $error_message);
166 $sql = 'INSERT INTO log (user_id, type, message, severity, location, hostname, created) '.
167 " VALUES (0, ".$db->quote_string($type).", ".$db->quote_string($message).', '.($this->mode & AK_MODE_DIE ? 100 : 0).', '.
168 $db->quote_string(AK_CURRENT_URL).', '.$db->quote_string($_SERVER['REMOTE_ADDR']).', '.$db->quote_string(Ak::getTimestamp()).');';
169 if ($db->execute($sql) === false) {
170 die($this->internalError($this->t('Error inserting: ').$db->ErrorMsg(),__FILE__,__LINE__));
174 function _getLogFormatedAsHtml($type, $error_mode, $error_message)
176 $error_type = $error_mode ? 'error' : 'info';
177 $message = "\n<div id='logger_$error_type'>\n<p>".$this->t(ucfirst($error_type)).": [$error_mode] - $error_message</p>\n";
178 $params = array_merge($this->_log_params, ($this->extended_details ? array('remote_address'=>$_SERVER['REMOTE_ADDR'], 'browser'=>$_SERVER['HTTP_USER_AGENT']) : array() ));
179 $details = '';
180 foreach ($params as $k=>$v){
181 $details .= "<li><span>".$k.":</span> $v</li>\n";
183 return empty($details) ? $message.'</div>' : $message."<ul>\n$details\n</ul>\n</div>";
186 function _getLogFormatedAsString($type, $error_mode, $error_message, $serialized = false)
188 $message = date('r')."\t[$error_mode]\t$error_message";
189 $params = array_merge($this->_log_params, ($this->extended_details ? array('remote_address'=>$_SERVER['REMOTE_ADDR'], 'browser'=>$_SERVER['HTTP_USER_AGENT']) : array() ));
191 if($serialized){
192 $message .= (count($params) ? "\t".serialize($params) : '');
193 }else{
194 $details = '';
195 foreach ($params as $k=>$v){
196 $details .= "\n\t\t- ".$k.": $v";
198 $message .= empty($details) ? "\n" : "\n\t".'PARAMS{'.$details."\t\n}\n";
200 return $message;
203 function _getLogFormatedAsRawText($type, $error_mode, $error_message)
205 return $this->_getLogFormatedAsString($type, $error_mode, $error_message, $filename, $line_number, true);
209 function setLogParams($log_params)
211 $this->_log_params = $log_params;
214 function getLogParams()
216 return is_array($this->_log_params) ? $this->_log_params : array();
220 function internalError($message, $file, $line)
222 return "<div id='internalError'><p><b>Error:</b> [internal] - $message<br /><b>File:</b> $file at line $line</p></div>";
225 function t($string, $array = null)
227 return Ak::t($string, $array, 'error');
230 function formatText($text, $color = 'normal')
232 if(!AK_LOG_ENABLE_COLORING){
233 return $text;
236 $colors = array(
237 'light_red ' => '[1;31m',
238 'light_green' => '[1;32m',
239 'yellow' => '[1;33m',
240 'light_blue' => '[1;34m',
241 'magenta' => '[1;35m',
242 'light_cyan' => '[1;36m',
243 'white' => '[1;37m',
244 'normal' => '[0m',
245 'black' => '[0;30m',
246 'red' => '[0;31m',
247 'green' => '[0;32m',
248 'brown' => '[0;33m',
249 'blue' => '[0;34m',
250 'cyan' => '[0;36m',
251 'bold' => '[1m',
252 'underscore' => '[4m',
253 'reverse' => '[7m'
256 return "\033".(isset($colors[$color]) ? $colors[$color] : '[0m').$text."\033[0m";