XOOPS  2.6.0
Logger.php
Go to the documentation of this file.
1 <?php
2 /*
3  You may not change or alter any portion of this comment or credits
4  of supporting developers from this source code or any supporting source code
5  which is considered copyrighted (c) material of the original comment or credit authors.
6 
7  This program is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 */
11 
12 namespace Xoops\Core;
13 
16 
46 class Logger implements LoggerInterface
47 {
51  private $loggers = array();
52 
56  private $logging_active = false;
57 
61  //public $activated = false;
62 
68  public static function getInstance()
69  {
70  static $instance;
71  if (!isset($instance)) {
72  $class = __CLASS__;
73  $instance = new $class();
74  // Always catch errors, for security reasons
75  set_error_handler(array($instance, 'handleError'));
76  // grab any uncaught exception
77  set_exception_handler(array($instance, 'handleException'));
78  }
79 
80  return $instance;
81  }
82 
95  public function handleError($errno, $errstr, $errfile, $errline)
96  {
97  if ($this->logging_active && ($errno & error_reporting())) {
98 
99  // if an error occurs before a locale is established,
100  // we still need messages, so check and deal with it
101 
102  $msg = ': ' . sprintf(
103  (class_exists('\XoopsLocale', false) ? \XoopsLocale::EF_LOGGER_FILELINE : "%s in file %s line %s"),
104  $this->sanitizePath($errstr),
105  $this->sanitizePath($errfile),
106  $errline
107  );
108 
109  switch ($errno) {
110  case E_USER_NOTICE:
111  $msg = (class_exists('\XoopsLocale', false) ? \XoopsLocale::E_LOGGER_ERROR : '*Error') . $msg;
112  $this->log(LogLevel::NOTICE, $msg);
113  break;
114  case E_NOTICE:
115  $msg = (class_exists('\XoopsLocale', false) ? \XoopsLocale::E_LOGGER_NOTICE : '*Notice') . $msg;
116  $this->log(LogLevel::NOTICE, $msg);
117  break;
118  case E_WARNING:
119  $msg = (class_exists('\XoopsLocale', false) ? \XoopsLocale::E_LOGGER_WARNING : '*Warning') . $msg;
120  $this->log(LogLevel::WARNING, $msg);
121  break;
122  case E_STRICT:
123  $msg = (class_exists('\XoopsLocale', false) ? \XoopsLocale::E_LOGGER_STRICT : '*Strict') . $msg;
124  $this->log(LogLevel::WARNING, $msg);
125  break;
126  case E_USER_ERROR:
127  $msg = (class_exists('\XoopsLocale', false) ? \XoopsLocale::E_LOGGER_ERROR : '*Error') . $msg;
128  @$this->log(LogLevel::CRITICAL, $msg);
129  break;
130  default:
131  $msg = (class_exists('\XoopsLocale', false) ? \XoopsLocale::E_LOGGER_UNKNOWN : '*Unknown') . $msg;
132  $this->log(LogLevel::ERROR, $msg);
133  break;
134  }
135  }
136 
137  if ($errno == E_USER_ERROR) {
138  $trace = true;
139  if (substr($errstr, 0, '8') == 'notrace:') {
140  $trace = false;
141  $errstr = substr($errstr, 8);
142  }
143  $this->reportFatalError($errstr);
144  if ($trace) {
145  $trace = debug_backtrace();
146  array_shift($trace);
147  if ('cli' == php_sapi_name()) {
148  foreach ($trace as $step) {
149  if (isset($step['file'])) {
150  fprintf(STDERR, "%s (%d)\n", $this->sanitizePath($step['file']), $step['line']);
151  }
152  }
153  } else {
154  echo "<div style='color:#f0f0f0;background-color:#f0f0f0'>" . _XOOPS_FATAL_BACKTRACE . ":<br />";
155  foreach ($trace as $step) {
156  if (isset($step['file'])) {
157  printf("%s (%d)\n<br />", $this->sanitizePath($step['file']), $step['line']);
158  }
159  }
160  echo '</div>';
161  }
162  }
163  exit();
164  }
165  }
166 
176  public function handleException($e)
177  {
178  $msg = $e->__toString();
179  $this->reportFatalError($msg);
180  }
181 
182  private function reportFatalError($msg)
183  {
184  $msg=$this->sanitizePath($msg);
185  if ('cli' == php_sapi_name()) {
186  fprintf(STDERR, "\nError : %s\n", $msg);
187  } else {
188  printf(_XOOPS_FATAL_MESSAGE, XOOPS_URL, $msg);
189  }
190  @$this->log(LogLevel::CRITICAL, $msg);
191  }
192 
200  public function sanitizePath($path)
201  {
202  $path = str_replace(
203  array(
204  '\\',
205  \XoopsBaseConfig::get('var-path'),
206  str_replace('\\', '/', realpath(\XoopsBaseConfig::get('var-path'))),
207  \XoopsBaseConfig::get('lib-path'),
208  str_replace('\\', '/', realpath(\XoopsBaseConfig::get('lib-path'))),
209  \XoopsBaseConfig::get('root-path'),
210  str_replace('\\', '/', realpath(\XoopsBaseConfig::get('root-path'))),
211  ),
212  array(
213  '/',
214  'VAR',
215  'VAR',
216  'LIB',
217  'LIB',
218  'ROOT',
219  'ROOT',
220  ),
221  $path
222  );
223 
224  return $path;
225  }
226 
234  public function addLogger($logger)
235  {
236  if (is_object($logger) && method_exists($logger, 'log')) {
237  $this->loggers[] = $logger;
238  $this->logging_active = true;
239  }
240  }
241 
250  public function emergency($message, array $context = array())
251  {
252  $this->log(LogLevel::EMERGENCY, $message, $context);
253  }
254 
266  public function alert($message, array $context = array())
267  {
268  $this->log(LogLevel::ALERT, $message, $context);
269  }
270 
281  public function critical($message, array $context = array())
282  {
283  $this->log(LogLevel::CRITICAL, $message, $context);
284  }
285 
295  public function error($message, array $context = array())
296  {
297  $this->log(LogLevel::ERROR, $message, $context);
298  }
299 
311  public function warning($message, array $context = array())
312  {
313  $this->log(LogLevel::WARNING, $message, $context);
314  }
315 
324  public function notice($message, array $context = array())
325  {
326  $this->log(LogLevel::NOTICE, $message, $context);
327  }
328 
339  public function info($message, array $context = array())
340  {
341  $this->log(LogLevel::INFO, $message, $context);
342  }
343 
352  public function debug($message, array $context = array())
353  {
354  $this->log(LogLevel::DEBUG, $message, $context);
355  }
356 
366  public function log($level, $message, array $context = array())
367  {
368  if (!empty($this->loggers)) {
369  foreach ($this->loggers as $logger) {
370  if (is_object($logger)) {
371  try {
372  $logger->log($level, $message, $context);
373  } catch (\Exception $e) {
374  // just ignore, as we can't do anything, not even log it.
375  }
376  }
377  }
378  }
379  }
380 
391  public function quiet()
392  {
393  if (!empty($this->loggers)) {
394  foreach ($this->loggers as $logger) {
395  if (is_object($logger) && method_exists($logger, 'quiet')) {
396  try {
397  $logger->quiet();
398  } catch (\Exception $e) {
399  // just ignore, as we can't do anything, not even log it.
400  }
401  }
402  }
403  }
404  }
405 
406  // Deprecated uses
407 
418  public function __set($var, $val)
419  {
420  $this->deprecatedMessage();
421  // legacy compatibility: turn off logger display for $xoopsLogger->activated = false; usage
422  if ($var=='activated' && !$val) {
423  $this->quiet();
424  }
425 
426  }
427 
437  public function __get($var)
438  {
439  $this->deprecatedMessage();
440  }
441 
452  public function __call($method, $args)
453  {
454  $this->deprecatedMessage();
455  }
456 
462  private function deprecatedMessage()
463  {
465  $xoops->deprecated('This use of XoopsLogger is deprecated since 2.6.0.');
466  }
467 }
log($level, $message, array $context=array())
Definition: Logger.php:366
$path
Definition: execute.php:31
emergency($message, array $context=array())
Definition: Logger.php:250
__set($var, $val)
Definition: Logger.php:418
static getInstance()
Definition: Xoops.php:160
const E_LOGGER_ERROR
Definition: en_US.php:340
warning($message, array $context=array())
Definition: Logger.php:311
reportFatalError($msg)
Definition: Logger.php:182
handleError($errno, $errstr, $errfile, $errline)
Definition: Logger.php:95
error($message, array $context=array())
Definition: Logger.php:295
const E_LOGGER_NOTICE
Definition: en_US.php:341
const _XOOPS_FATAL_BACKTRACE
Definition: defines.php:109
exit
Definition: browse.php:104
const EF_LOGGER_FILELINE
Definition: en_US.php:257
const E_LOGGER_STRICT
Definition: en_US.php:342
$xoops
Definition: admin.php:25
debug($message, array $context=array())
Definition: Logger.php:352
addLogger($logger)
Definition: Logger.php:234
alert($message, array $context=array())
Definition: Logger.php:266
const E_LOGGER_UNKNOWN
Definition: en_US.php:343
static get($name)
__call($method, $args)
Definition: Logger.php:452
const WARNING
Definition: install.php:39
sanitizePath($path)
Definition: Logger.php:200
if(!is_object($module)||!$module->getVar('isactive')) $msg
Definition: groupperm.php:38
$var
Definition: userinfo.php:125
info($message, array $context=array())
Definition: Logger.php:339
static getInstance()
Definition: Logger.php:68
const _XOOPS_FATAL_MESSAGE
Definition: defines.php:65
notice($message, array $context=array())
Definition: Logger.php:324
critical($message, array $context=array())
Definition: Logger.php:281
const E_LOGGER_WARNING
Definition: en_US.php:344