1 |
<? |
2 |
// --------------------------------------------------------------------------- |
3 |
// $Id: Logger.php,v 1.3 2003/02/09 17:14:49 joko Exp $ |
4 |
// --------------------------------------------------------------------------- |
5 |
// $Log: Logger.php,v $ |
6 |
// Revision 1.3 2003/02/09 17:14:49 joko |
7 |
// + now able to redirect errors raised by PEAR to logfile |
8 |
// |
9 |
// Revision 1.2 2003/02/03 14:47:49 joko |
10 |
// + some code from DesignPattern::Bridge |
11 |
// |
12 |
// Revision 1.1 2003/02/03 03:33:48 joko |
13 |
// + initial commit |
14 |
// |
15 |
// --------------------------------------------------------------------------- |
16 |
|
17 |
|
18 |
class DesignPattern_Logger { |
19 |
|
20 |
var $logger; |
21 |
var $logfile; |
22 |
var $enabled; |
23 |
|
24 |
/* |
25 |
function DesignPattern_Logger() { |
26 |
$this->constructor(); |
27 |
} |
28 |
*/ |
29 |
|
30 |
function constructor() { |
31 |
// FIXME: this is hardcoded! |
32 |
$logfile = 'logfile.txt'; |
33 |
if (defined('LOGFILE')) { |
34 |
$logfile = LOGFILE; |
35 |
} |
36 |
$this->_init_logger($logfile, 1); |
37 |
} |
38 |
|
39 |
// TODO: split by loglevel here to make seperated logfiles possible (debug, normal, errors) |
40 |
// TODO: optional: transmit logging messages via tcp - don't write them to disk (a handler for PEAR::Log) |
41 |
function log($msg, $level = PEAR_LOG_DEBUG) { |
42 |
//print "log: $msg<br>"; |
43 |
if ($this->logger) { |
44 |
$this->logger->log($msg, $level); |
45 |
} else { |
46 |
// TODO: how are these type of errors handled? |
47 |
//print "error-message: $msg<br>"; |
48 |
} |
49 |
} |
50 |
|
51 |
function _init_logger($logfile, $enable = 0) { |
52 |
$this->logfile = $logfile; |
53 |
$this->enabled = $enable; |
54 |
//print Dumper($this); |
55 |
$this->_call_PEAR_Log_singleton(); |
56 |
$this->_pear_errors_redirect(); |
57 |
} |
58 |
|
59 |
function _call_PEAR_Log_singleton() { |
60 |
|
61 |
// $logfile = 'log.txt'; |
62 |
// $logfile = $this->config[path][base] . "core/var/log/logfile.txt"; |
63 |
|
64 |
// TODO: maybe include userid here? |
65 |
//$log_ident = substr(session_id(), 10, 6); |
66 |
$log_ident = session_id(); |
67 |
$outkey = 'dummy'; |
68 |
if ($this->enabled) { |
69 |
$outkey = 'file'; |
70 |
} |
71 |
$this->logger = &Log::singleton($outkey, $this->logfile, $log_ident, array( timestampFormat => "%Y-%m-%d %H:%M:%S" )); |
72 |
//$this->log( get_class($this) . "->_init_logger: ready\t\t--------------------", LOG_DEBUG ); |
73 |
} |
74 |
|
75 |
function _pear_errors_redirect() { |
76 |
// 1. use PEAR::setErrorHandling |
77 |
//PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, "my_function_handler"); |
78 |
// ... this doesn't work since the method thinks it's called oo-style, |
79 |
// which is *not* the case here |
80 |
|
81 |
// 2. use $GLOBALS to set global PEAR variables |
82 |
$GLOBALS['_PEAR_default_error_mode'] = PEAR_ERROR_CALLBACK; |
83 |
$GLOBALS['_PEAR_default_error_options'] = array($this, "_pear_error_receive"); |
84 |
} |
85 |
|
86 |
function _pear_error_receive($errorObject) { |
87 |
// trace |
88 |
//print Dumper($errorObject); |
89 |
$this->log( "PEAR_Error->message: " . $errorObject->message, PEAR_LOG_ERR); |
90 |
} |
91 |
|
92 |
} |
93 |
|
94 |
?> |