| 1 |
<? |
| 2 |
/** |
| 3 |
* This file contains the Class::Logger class. |
| 4 |
* |
| 5 |
* @author Andreas Motl <andreas.motl@ilo.de> |
| 6 |
* @package org.netfrag.glib |
| 7 |
* @name Class::Logger |
| 8 |
* |
| 9 |
*/ |
| 10 |
|
| 11 |
|
| 12 |
/** |
| 13 |
* <b>Cvs-Log:</b> |
| 14 |
* |
| 15 |
* <pre> |
| 16 |
* |
| 17 |
* $Id: Logger.php,v 1.7 2003/03/11 02:14:23 joko Exp $ |
| 18 |
* |
| 19 |
* $Log: Logger.php,v $ |
| 20 |
* Revision 1.7 2003/03/11 02:14:23 joko |
| 21 |
* + fixed metadata for phpDocumentor |
| 22 |
* |
| 23 |
* Revision 1.6 2003/03/11 02:04:36 joko |
| 24 |
* + fixed metadata for phpDocumentor |
| 25 |
* |
| 26 |
* Revision 1.5 2003/03/11 01:42:59 joko |
| 27 |
* + fixed metadata for phpDocumentor |
| 28 |
* |
| 29 |
* Revision 1.4 2003/03/11 01:22:24 joko |
| 30 |
* + fixed metadata for phpDocumentor |
| 31 |
* |
| 32 |
* Revision 1.3 2003/03/11 01:12:53 joko |
| 33 |
* + fixed metadata for phpDocumentor |
| 34 |
* |
| 35 |
* Revision 1.2 2003/03/05 18:54:43 joko |
| 36 |
* updated docu - phpDocumentor is very strict about its 'blocks'... |
| 37 |
* |
| 38 |
* Revision 1.1 2003/03/03 21:26:43 joko |
| 39 |
* refactored from DesignPattern::Logger |
| 40 |
* |
| 41 |
* Revision 1.4 2003/02/13 00:43:41 joko |
| 42 |
* + logfile name now can get passed via defined constant |
| 43 |
* |
| 44 |
* Revision 1.3 2003/02/09 17:14:49 joko |
| 45 |
* + now able to redirect errors raised by PEAR to logfile |
| 46 |
* |
| 47 |
* Revision 1.2 2003/02/03 14:47:49 joko |
| 48 |
* + some code from DesignPattern::Bridge |
| 49 |
* |
| 50 |
* Revision 1.1 2003/02/03 03:33:48 joko |
| 51 |
* + initial commit |
| 52 |
* |
| 53 |
* </pre> |
| 54 |
* |
| 55 |
*/ |
| 56 |
|
| 57 |
|
| 58 |
/** |
| 59 |
* This requires Class::Abstract as a base class |
| 60 |
* |
| 61 |
*/ |
| 62 |
php::loadModule('Class::Abstract'); |
| 63 |
|
| 64 |
|
| 65 |
/** |
| 66 |
* --- Class::Inner |
| 67 |
* |
| 68 |
* @author Andreas Motl <andreas.motl@ilo.de> |
| 69 |
* @copyright (c) 2003 - All Rights reserved. |
| 70 |
* @license GNU LGPL (GNU Lesser General Public License) |
| 71 |
* |
| 72 |
* @link http://www.netfrag.org/~joko/ |
| 73 |
* @link http://www.gnu.org/licenses/lgpl.txt |
| 74 |
* |
| 75 |
* @package org.netfrag.glib |
| 76 |
* @subpackage Class |
| 77 |
* @name Class::Logger |
| 78 |
* |
| 79 |
* |
| 80 |
*/ |
| 81 |
class Class_Logger extends Class_Abstract { |
| 82 |
|
| 83 |
var $logger; |
| 84 |
var $logfile; |
| 85 |
var $enabled; |
| 86 |
|
| 87 |
function Class_Logger() { |
| 88 |
$this->constructor(); |
| 89 |
} |
| 90 |
|
| 91 |
function constructor() { |
| 92 |
// FIXME: this is hardcoded! |
| 93 |
$logfile = 'logfile.txt'; |
| 94 |
if (defined('LOGFILE')) { |
| 95 |
$logfile = LOGFILE; |
| 96 |
} |
| 97 |
$this->_init_logger($logfile, 1); |
| 98 |
} |
| 99 |
|
| 100 |
// TODO: split by loglevel here to make seperated logfiles possible (debug, normal, errors) |
| 101 |
// TODO: optional: transmit logging messages via tcp - don't write them to disk (a handler for PEAR::Log) |
| 102 |
function log($msg, $level = PEAR_LOG_DEBUG) { |
| 103 |
//print "log: $msg<br>"; |
| 104 |
if ($this->logger) { |
| 105 |
$this->logger->log($msg, $level); |
| 106 |
} else { |
| 107 |
// TODO: how are these type of errors handled? |
| 108 |
//print "error-message: $msg<br>"; |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
function _init_logger($logfile, $enable = 0) { |
| 113 |
$this->logfile = $logfile; |
| 114 |
$this->enabled = $enable; |
| 115 |
//print Dumper($this); |
| 116 |
$this->_call_PEAR_Log_singleton(); |
| 117 |
$this->_pear_errors_redirect(); |
| 118 |
} |
| 119 |
|
| 120 |
function _call_PEAR_Log_singleton() { |
| 121 |
|
| 122 |
// $logfile = 'log.txt'; |
| 123 |
// $logfile = $this->config[path][base] . "core/var/log/logfile.txt"; |
| 124 |
|
| 125 |
// TODO: maybe include userid here? |
| 126 |
//$log_ident = substr(session_id(), 10, 6); |
| 127 |
$log_ident = session_id(); |
| 128 |
$outkey = 'dummy'; |
| 129 |
if ($this->enabled) { |
| 130 |
$outkey = 'file'; |
| 131 |
} |
| 132 |
|
| 133 |
if (class_exists('Log')) { |
| 134 |
$this->logger = &Log::singleton($outkey, $this->logfile, $log_ident, array( timestampFormat => "%Y-%m-%d %H:%M:%S" )); |
| 135 |
} |
| 136 |
//$this->log( get_class($this) . "->_init_logger: ready\t\t--------------------", LOG_DEBUG ); |
| 137 |
|
| 138 |
} |
| 139 |
|
| 140 |
function _pear_errors_redirect() { |
| 141 |
// 1. use PEAR::setErrorHandling |
| 142 |
//PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, "my_function_handler"); |
| 143 |
// ... this doesn't work since the method thinks it's called oo-style, |
| 144 |
// which is *not* the case here |
| 145 |
|
| 146 |
// 2. use $GLOBALS to set global PEAR variables |
| 147 |
$GLOBALS['_PEAR_default_error_mode'] = PEAR_ERROR_CALLBACK; |
| 148 |
$GLOBALS['_PEAR_default_error_options'] = array($this, "_pear_error_receive"); |
| 149 |
} |
| 150 |
|
| 151 |
function _pear_error_receive($errorObject) { |
| 152 |
// trace |
| 153 |
//print Dumper($errorObject); |
| 154 |
$this->log( "PEAR_Error->message: " . $errorObject->message, PEAR_LOG_ERR); |
| 155 |
} |
| 156 |
|
| 157 |
} |
| 158 |
|
| 159 |
?> |