Core  3.2
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
Class.Log.php
Go to the documentation of this file.
1 <?php
2 /*
3  * @author Anakeen
4  * @package FDL
5 */
6 /**
7  * Log information class
8  *
9  * @author Anakeen
10  * @version $Id: Class.Log.php,v 1.15 2008/10/31 16:57:18 jerome Exp $
11  * @package FDL
12  * @subpackage CORE
13  */
14 /**
15  */
16 /**
17  * Log manager
18  * log message according to CORE_LOGLEVEL parameter
19  * @class Log
20  *
21  */
22 class Log
23 {
24  public $loghead;
25  public $application;
26  public $function;
27  private $deb;
28  private $fin;
29  private $tic;
30  private $ptext;
31  /**
32  * @var string Level to log
33  */
34  protected $logLevel = null;
35  /**
36  * Constant to set log to debug level
37  * Debug level is used by Core.
38  * It's used to assert taht Core works properly
39  */
40  const DEBUG = "D";
41  /**
42  * Constant to set log to callstack level
43  */
44  const CALLSTACK = "C";
45  /**
46  * Constant to set log to trace level
47  * The trace level is a level reserved for user usage.
48  * Core will never log with this level
49  */
50  const TRACE = "T";
51  /**
52  * Constant to set log to info level
53  */
54  const INFO = "I";
55  /**
56  * Constant to set log to warning level
57  */
58  const WARNING = "W";
59  /**
60  * Constant to set log to error level
61  */
62  const ERROR = "E";
63  /**
64  * Constant to set log to fatal level
65  */
66  const FATAL = "F";
67  /**
68  * Constant to set log to deprecated level
69  */
70  const DEPRECATED = "O";
71  // ------------------------------------------------------------------------
72 
73  /**
74  * @api initialize log manager
75  * @param string $logfile
76  * @param string $application
77  * @param string $function
78  */
79  public function __construct($logfile = "", $application = "", $function = "")
80  {
81  $this->usesyslog = 0;
82  if ($logfile == "") {
83  $this->usesyslog = 1;
84  } else {
85  $fd = fopen($logfile, "a");
86  if (!$fd) {
87  $this->usesyslog = 1;
88  $this->error("Can't access $logfile, using syslog");
89  } else {
90  $this->logfile = $logfile;
91  fclose($fd);
92  }
93  }
94  $this->application = $application;
95  $this->function = $function;
96  }
97  /**
98  * log with debug level
99  * @api log with debug level
100  * @param string $string message text
101  */
102  public function debug($string)
103  {
104  $this->wlog(Log::DEBUG, $string);
105  }
106  /**
107  * @param string $string message text
108  */
109  public function callstack($string)
110  {
111  $this->wlog(Log::CALLSTACK, $string);
112  }
113  /**
114  * log with trace level
115  * @api log with trace level
116  * @param string $string mesage text
117  */
118  public function trace($string)
119  {
120  $this->wlog(Log::TRACE, $string);
121  }
122  /**
123  * log with info level
124  * @api log with info level
125  * @param string $string message text
126  */
127  public function info($string)
128  {
129  $this->wlog(Log::INFO, $string);
130  }
131  /**
132  * log with warning level
133  * @api log with warning level
134  * @param string $string message text
135  */
136  public function warning($string)
137  {
138  $this->wlog(Log::WARNING, $string);
139  }
140  /**
141  * log with error level
142  * @api log with error level
143  * @param string $string message text
144  */
145  public function error($string)
146  {
147  $this->wlog(Log::ERROR, $string);
148  }
149  /**
150  * log with fatal level
151  * @api log with fatal level
152  * @param string $string message text
153  */
154  public function fatal($string)
155  {
156  $this->wlog(Log::FATAL, $string);
157  }
158  /**
159  * log with deprecated level
160  * add callstack
161  * @api log with deprecated level
162  * @see Log
163  * @param string $string message text
164  */
165  public function deprecated($string)
166  {
167  $this->wlog(Log::DEPRECATED, $string);
168  }
169  /**
170  * @param string $logLevel
171  */
172  public function setLogLevel($logLevel)
173  {
174  $this->logLevel = $logLevel;
175  }
176  /**
177  * @return string
178  */
179  public function getLogLevel()
180  {
181  if ($this->logLevel === null) {
182  $this->logLevel = getParam("CORE_LOGLEVEL", "IWEF");
183  }
184  return $this->logLevel;
185  }
186  /**
187  * to set start time
188  * @param string $text prefix text to set for next tic/end
189  */
190  public function start($text = "")
191  {
192  $deb = gettimeofday();
193  $this->deb = $deb["sec"] + $deb["usec"] / 1000000;
194  $this->tic = $this->deb;
195  $this->ptext = $text; // prefix
196 
197  }
198  /**
199  * log partial time
200  * @see start
201  * @param string $text text to log
202  */
203  public function tic($text)
204  {
205  $tic = gettimeofday();
206  $now = $tic["sec"] + $tic["usec"] / 1000000;
207  $duree = round($now - $this->tic, 3);
208  $this->info("CHRONO-INT [$this->ptext]/[$text] : $duree");
209  $this->tic = $now;
210  }
211  /**
212  * log end time from last start
213  * @param string $text text to log
214  */
215  public function end($text)
216  {
217  $fin = gettimeofday();
218  $this->fin = $fin["sec"] + $fin["usec"] / 1000000;
219  $duree = round($this->fin - $this->deb, 3);
220  $this->info("CHRONO [$this->ptext]/[$text] : $duree");
221  }
222 
223  public function push($string)
224  {
225  if (is_int(strpos($this->getLogLevel() , "C"))) {
226  global $call_ind, $call_stack, $call_pre, $call_reqid;
227  if (!isset($call_ind)) $call_ind = 0;
228  if (!isset($call_pre)) $call_pre = "-";
229  if (!isset($call_reqid)) $call_reqid = rand(1, 100);
230  $this->callstack("($call_reqid) $call_pre : entering $string");
231  $call_stack[$call_ind] = $string;
232  $call_ind+= 1;
233  $call_pre = $call_pre . "-";
234  }
235  }
236 
237  public function pop()
238  {
239  if (is_int(strpos($this->getLogLevel() , "C"))) {
240  global $call_ind, $call_stack, $call_pre, $call_reqid;
241  $call_pre = substr($call_pre, 0, strlen($call_pre) - 1);
242  $call_ind-= 1;
243  $this->callstack("($call_reqid) $call_pre : exiting {$call_stack[$call_ind]}");
244  }
245  }
246  /**
247  * main log function
248  * @param string $sta log code (one character : IWEFDOT)
249  * @param string $str message to log
250  * @param null $args unused
251  * @param int $facility syslog level
252  */
253  public function wlog($sta, $str, $args = NULL, $facility = LOG_LOCAL6)
254  {
255  global $_SERVER;
256 
257  if (!$str) return;
258  if (is_array($str)) $str = implode(", ", $str);
259  if ($sta == "S" || (is_int(strpos($this->getLogLevel() , $sta)))) {
260  $addr = isset($_SERVER["REMOTE_ADDR"]) ? $_SERVER["REMOTE_ADDR"] : '';
261  $appf = "[{$sta}] Dynacase";
262  $appf.= ($this->application != "" ? ":" . $this->application : "");
263  $appf.= ($this->function != "" ? ":" . $this->function : "");
264  $str = ' ' . $this->loghead . ': ' . $str;
265  if (!$this->usesyslog) {
266  $xx = date("d/m/Y H:i:s", time()) . " {$appf} [{$addr}] ";
267  $xx = $xx . $str . "\n";
268  $fd = fopen($this->logfile, "a");
269  fputs($fd, $xx);
270  fclose($fd);
271  } else {
272  switch ($sta) {
273  case Log::DEBUG:
274  $pri = LOG_DEBUG;
275  break;
276 
277  case Log::DEPRECATED:
278  $class = (isset($td[4]["class"])) ? $td[4]["class"] : '';
279  $td = @debug_backtrace(false);
280  if ($str) {
281  $str.= ' ';
282  }
283  $str.= sprintf("%s called in %s%s%s(), file %s:%s", isset($td[3]["function"]) ? $td[3]["function"] : '', $class, $class ? '::' : '', isset($td[4]["function"]) ? $td[4]["function"] : '', isset($td[3]["file"]) ? $td[3]["file"] : '', isset($td[3]["line"]) ? $td[3]["line"] : '');
284  $pri = LOG_INFO;
285  break;
286 
287  case Log::INFO:
288  $pri = LOG_INFO;
289  break;
290 
291  case Log::WARNING:
292  $pri = LOG_WARNING;
293  break;
294 
295  case Log::ERROR:
296  $pri = LOG_ERR;
297  break;
298 
299  case Log::FATAL:
300  $pri = LOG_ALERT;
301  break;
302 
303  case Log::TRACE:
304  $pri = LOG_DEBUG;
305  break;
306 
307  default:
308  $pri = LOG_NOTICE;
309  }
310  if (empty($_SERVER['HTTP_HOST'])) {
311  error_log(sprintf("%s LOG::$appf %s", date("d/m/Y H:i:s", time()) , $str));
312  }
313  openlog("{$appf}", 0, $facility);
314  syslog($pri, "[{$addr}] " . $str);
315  closelog();
316 
317  if ($sta == "E") {
318  error_log($str); // use apache syslog also
319 
320  }
321  }
322  }
323  }
324 } // Class.Log
325 
326 ?>
fatal($string)
Definition: Class.Log.php:154
const WARNING
Definition: Class.Log.php:58
end($text)
Definition: Class.Log.php:215
if(substr($wsh, 0, 1)!= '/') $args
info($string)
Definition: Class.Log.php:127
deprecated($string)
Definition: Class.Log.php:165
const CALLSTACK
Definition: Class.Log.php:44
const TRACE
Definition: Class.Log.php:50
push($string)
Definition: Class.Log.php:223
$application
Definition: Class.Log.php:25
$logLevel
Definition: Class.Log.php:34
__construct($logfile="", $application="", $function="")
Definition: Class.Log.php:79
const INFO
Definition: Class.Log.php:54
debug($string)
Definition: Class.Log.php:102
const ERROR
Definition: Class.Log.php:62
const DEPRECATED
Definition: Class.Log.php:70
getParam($name, $def="")
must be in core or global type
Definition: Lib.Common.php:193
$function
Definition: Class.Log.php:26
getLogLevel()
Definition: Class.Log.php:179
callstack($string)
Definition: Class.Log.php:109
start($text="")
Definition: Class.Log.php:190
global $_SERVER
pop()
Definition: Class.Log.php:237
setLogLevel($logLevel)
Definition: Class.Log.php:172
$logfile
$loghead
Definition: Class.Log.php:24
trace($string)
Definition: Class.Log.php:118
warning($string)
Definition: Class.Log.php:136
const FATAL
Definition: Class.Log.php:66
tic($text)
Definition: Class.Log.php:203
$class
Definition: updateclass.php:38
error($string)
Definition: Class.Log.php:145
wlog($sta, $str, $args=NULL, $facility=LOG_LOCAL6)
Definition: Class.Log.php:253
const DEBUG
Definition: Class.Log.php:40
← centre documentaire © anakeen