/[cvs]/nfo/php/libs/org.netfrag.glib/Class/Logger.php
ViewVC logotype

Contents of /nfo/php/libs/org.netfrag.glib/Class/Logger.php

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.10 - (show annotations)
Fri Mar 28 03:04:26 2003 UTC (21 years, 3 months ago) by joko
Branch: MAIN
Changes since 1.9: +19 -5 lines
enhanced conditions for debugging

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

MailToCvsAdmin">MailToCvsAdmin
ViewVC Help
Powered by ViewVC 1.1.26 RSS 2.0 feed