ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Logger.php
Go to the documentation of this file.
1 <?php
2 
3 namespace SimpleSAML;
4 
13 class Logger
14 {
15 
19  private static $loggingHandler = null;
20 
24  private static $logLevel = null;
25 
29  private static $captureLog = false;
30 
34  private static $capturedLog = array();
35 
41  private static $earlyLog = array();
42 
50  private static $logLevelStack = array();
51 
59  private static $logMask = 0;
60 
61 
66  const NO_TRACKID = '_NOTRACKIDYET_';
67 
72  private static $trackid = self::NO_TRACKID;
73 
102  private static $format = '%date{%b %d %H:%M:%S} %process %level %stat[%trackid] %msg';
103 
109  private static $shutdownRegistered = false;
110 
116  private static $shuttingDown = false;
117 
118  const EMERG = 0;
119  const ALERT = 1;
120  const CRIT = 2;
121  const ERR = 3;
122  const WARNING = 4;
123  const NOTICE = 5;
124  const INFO = 6;
125  const DEBUG = 7;
126 
127 
133  public static function emergency($string)
134  {
135  self::log(self::EMERG, $string);
136  }
137 
138 
144  public static function critical($string)
145  {
146  self::log(self::CRIT, $string);
147  }
148 
149 
155  public static function alert($string)
156  {
157  self::log(self::ALERT, $string);
158  }
159 
160 
166  public static function error($string)
167  {
168  self::log(self::ERR, $string);
169  }
170 
171 
177  public static function warning($string)
178  {
179  self::log(self::WARNING, $string);
180  }
181 
182 
188  public static function notice($string)
189  {
190  self::log(self::NOTICE, $string);
191  }
192 
193 
199  public static function info($string)
200  {
201  self::log(self::INFO, $string);
202  }
203 
204 
211  public static function debug($string)
212  {
213  self::log(self::DEBUG, $string);
214  }
215 
216 
222  public static function stats($string)
223  {
224  self::log(self::NOTICE, $string, true);
225  }
226 
227 
233  public static function setCaptureLog($val = true)
234  {
235  self::$captureLog = $val;
236  }
237 
238 
242  public static function getCapturedLog()
243  {
244  return self::$capturedLog;
245  }
246 
247 
253  public static function setTrackId($trackId)
254  {
255  self::$trackid = $trackId;
256  }
257 
258 
266  public static function flush()
267  {
268  try {
270  } catch (\Exception $e) {
271  // loading session failed. We don't care why, at this point we have a transient session, so we use that
272  self::error('Cannot load or create session: '.$e->getMessage());
274  }
275  self::$trackid = $s->getTrackID();
276 
277  self::$shuttingDown = true;
278  foreach (self::$earlyLog as $msg) {
279  self::log($msg['level'], $msg['string'], $msg['statsLog']);
280  }
281  }
282 
283 
291  public static function isErrorMasked($errno)
292  {
293  return ($errno & self::$logMask) || !($errno & error_reporting());
294  }
295 
296 
304  public static function maskErrors($mask)
305  {
306  assert(is_int($mask));
307 
308  $currentEnabled = error_reporting();
309  self::$logLevelStack[] = array($currentEnabled, self::$logMask);
310 
311  $currentEnabled &= ~$mask;
312  error_reporting($currentEnabled);
313  self::$logMask |= $mask;
314  }
315 
316 
322  public static function popErrorMask()
323  {
324  $lastMask = array_pop(self::$logLevelStack);
325  error_reporting($lastMask[0]);
326  self::$logMask = $lastMask[1];
327  }
328 
329 
337  private static function defer($level, $message, $stats)
338  {
339  // save the message for later
340  self::$earlyLog[] = array('level' => $level, 'string' => $message, 'statsLog' => $stats);
341 
342  // register a shutdown handler if needed
343  if (!self::$shutdownRegistered) {
344  register_shutdown_function(array('SimpleSAML\Logger', 'flush'));
345  self::$shutdownRegistered = true;
346  }
347  }
348 
349 
350  private static function createLoggingHandler($handler = null)
351  {
352  // set to false to indicate that it is being initialized
353  self::$loggingHandler = false;
354 
355  // a set of known logging handlers
356  $known_handlers = array(
357  'syslog' => 'SimpleSAML\Logger\SyslogLoggingHandler',
358  'file' => 'SimpleSAML\Logger\FileLoggingHandler',
359  'errorlog' => 'SimpleSAML\Logger\ErrorLogLoggingHandler',
360  );
361 
362  // get the configuration
364  assert($config instanceof \SimpleSAML_Configuration);
365 
366  // setting minimum log_level
367  self::$logLevel = $config->getInteger('logging.level', self::INFO);
368 
369  // get the metadata handler option from the configuration
370  if (is_null($handler)) {
371  $handler = $config->getString('logging.handler', 'syslog');
372  }
373 
374  if (!array_key_exists($handler, $known_handlers) && class_exists($handler)) {
375  if (!in_array('SimpleSAML\Logger\LoggingHandlerInterface', class_implements($handler), true)) {
376  throw new \Exception("The logging handler '$handler' is invalid.");
377  }
378  } else {
379  $handler = strtolower($handler);
380  if (!array_key_exists($handler, $known_handlers)) {
381  throw new \Exception(
382  "Invalid value for the 'logging.handler' configuration option. Unknown handler '".$handler."''."
383  );
384  }
385  $handler = $known_handlers[$handler];
386  }
387  self::$loggingHandler = new $handler($config);
388 
389  self::$format = $config->getString('logging.format', self::$format);
390  self::$loggingHandler->setLogFormat(self::$format);
391  }
392 
393 
394  private static function log($level, $string, $statsLog = false)
395  {
396  if (self::$loggingHandler === false) {
397  // some error occurred while initializing logging
398  self::defer($level, $string, $statsLog);
399  return;
400  } elseif (php_sapi_name() === 'cli' || defined('STDIN')) {
401  // we are being executed from the CLI, nowhere to log
402  if (is_null(self::$loggingHandler)) {
403  self::createLoggingHandler('SimpleSAML\Logger\StandardErrorLoggingHandler');
404  }
405  $_SERVER['REMOTE_ADDR'] = "CLI";
406  if (self::$trackid === self::NO_TRACKID) {
407  self::$trackid = 'CL'.bin2hex(openssl_random_pseudo_bytes(4));
408  }
409  } elseif (self::$loggingHandler === null) {
410  // Initialize logging
411  self::createLoggingHandler();
412 
413  if (!empty(self::$earlyLog)) {
414  // output messages which were logged before we properly initialized logging
415  foreach (self::$earlyLog as $msg) {
416  self::log($msg['level'], $msg['string'], $msg['statsLog']);
417  }
418  }
419  }
420 
421  if (self::$captureLog) {
422  $ts = microtime(true);
423  $msecs = (int) (($ts - (int) $ts) * 1000);
424  $ts = gmdate('H:i:s', $ts).sprintf('.%03d', $msecs).'Z';
425  self::$capturedLog[] = $ts.' '.$string;
426  }
427 
428  if (self::$logLevel >= $level || $statsLog) {
429  if (is_array($string)) {
430  $string = implode(",", $string);
431  }
432 
433  $formats = array('%trackid', '%msg', '%srcip', '%stat');
434  $replacements = array(self::$trackid, $string, $_SERVER['REMOTE_ADDR']);
435 
436  $stat = '';
437  if ($statsLog) {
438  $stat = 'STAT ';
439  }
440  array_push($replacements, $stat);
441 
442  if (self::$trackid === self::NO_TRACKID && !self::$shuttingDown) {
443  // we have a log without track ID and we are not still shutting down, so defer logging
444  self::defer($level, $string, $statsLog);
445  return;
446  } elseif (self::$trackid === self::NO_TRACKID) {
447  // shutting down without a track ID, prettify it
448  array_shift($replacements);
449  array_unshift($replacements, 'N/A');
450  }
451 
452  // we either have a track ID or we are shutting down, so just log the message
453  $string = str_replace($formats, $replacements, self::$format);
454  self::$loggingHandler->log($level, $string);
455  }
456  }
457 }
if((!isset($_SERVER['DOCUMENT_ROOT'])) OR(empty($_SERVER['DOCUMENT_ROOT']))) $_SERVER['DOCUMENT_ROOT']
static popErrorMask()
Pop an error mask.
Definition: Logger.php:322
$format
Definition: metadata.php:141
static createLoggingHandler($handler=null)
Definition: Logger.php:350
$config
Definition: bootstrap.php:15
static flush()
Flush any pending log messages to the logging handler.
Definition: Logger.php:266
static setCaptureLog($val=true)
Definition: Logger.php:233
static debug($string)
Definition: Logger.php:211
$s
Definition: pwgen.php:45
const DEBUG
static setTrackId($trackId)
Set the track identifier to use in all logs.
Definition: Logger.php:253
static notice($string)
Definition: Logger.php:188
static stats($string)
Definition: Logger.php:222
Attribute-related utility methods.
static info($string)
Definition: Logger.php:199
catch(Exception $e) $message
$mask
Definition: example_042.php:90
static warning($string)
Definition: Logger.php:177
static getCapturedLog()
Get the captured log.
Definition: Logger.php:242
static error($string)
Definition: Logger.php:166
static defer($level, $message, $stats)
Defer a message for later logging.
Definition: Logger.php:337
static maskErrors($mask)
Disable error reporting for the given log levels.
Definition: Logger.php:304
static critical($string)
Definition: Logger.php:144
static emergency($string)
Definition: Logger.php:133
static isErrorMasked($errno)
Evaluate whether errors of a certain error level are masked or not.
Definition: Logger.php:291
static alert($string)
Definition: Logger.php:155
$handler
static getSessionFromRequest()
Retrieves the current session.
Definition: Session.php:241
static getInstance($instancename='simplesaml')
Get a configuration file by its instance name.
$formats
Definition: date.php:77
static log($level, $string, $statsLog=false)
Definition: Logger.php:394