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