ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
Logger.php
Go to the documentation of this file.
1 <?php
2 
3 namespace SimpleSAML;
4 
5 
15 class Logger
16 {
17 
21  private static $loggingHandler = null;
22 
26  private static $logLevel = null;
27 
31  private static $captureLog = false;
32 
36  private static $capturedLog = array();
37 
43  private static $earlyLog = array();
44 
52  private static $logLevelStack = array();
53 
61  private static $logMask = 0;
62 
63 
68  const NO_TRACKID = '_NOTRACKIDYET_';
69 
74  private static $trackid = self::NO_TRACKID;
75 
104  private static $format = '%date{%b %d %H:%M:%S} %process %level %stat[%trackid] %msg';
105 
111  private static $shutdownRegistered = false;
112 
118  private static $shuttingDown = false;
119 
120  const EMERG = 0;
121  const ALERT = 1;
122  const CRIT = 2;
123  const ERR = 3;
124  const WARNING = 4;
125  const NOTICE = 5;
126  const INFO = 6;
127  const DEBUG = 7;
128 
129 
135  public static function emergency($string)
136  {
137  self::log(self::EMERG, $string);
138  }
139 
140 
146  public static function critical($string)
147  {
148  self::log(self::CRIT, $string);
149  }
150 
151 
157  public static function alert($string)
158  {
159  self::log(self::ALERT, $string);
160  }
161 
162 
168  public static function error($string)
169  {
170  self::log(self::ERR, $string);
171  }
172 
173 
179  public static function warning($string)
180  {
181  self::log(self::WARNING, $string);
182  }
183 
184 
190  public static function notice($string)
191  {
192  self::log(self::NOTICE, $string);
193  }
194 
195 
201  public static function info($string)
202  {
203  self::log(self::INFO, $string);
204  }
205 
206 
213  public static function debug($string)
214  {
215  self::log(self::DEBUG, $string);
216  }
217 
218 
224  public static function stats($string)
225  {
226  self::log(self::NOTICE, $string, true);
227  }
228 
229 
235  public static function setCaptureLog($val = true)
236  {
237  self::$captureLog = $val;
238  }
239 
240 
244  public static function getCapturedLog()
245  {
246  return self::$capturedLog;
247  }
248 
249 
255  public static function setTrackId($trackId)
256  {
257  self::$trackid = $trackId;
258  }
259 
260 
268  public static function flush()
269  {
270  try {
272  } catch (\Exception $e) {
273  // loading session failed. We don't care why, at this point we have a transient session, so we use that
274  self::error('Cannot load or create session: '.$e->getMessage());
276  }
277  self::$trackid = $s->getTrackID();
278 
279  self::$shuttingDown = true;
280  foreach (self::$earlyLog as $msg) {
281  self::log($msg['level'], $msg['string'], $msg['statsLog']);
282  }
283  }
284 
285 
293  public static function isErrorMasked($errno)
294  {
295  return ($errno & self::$logMask) || !($errno & error_reporting());
296  }
297 
298 
306  public static function maskErrors($mask)
307  {
308  assert('is_int($mask)');
309 
310  $currentEnabled = error_reporting();
311  self::$logLevelStack[] = array($currentEnabled, self::$logMask);
312 
313  $currentEnabled &= ~$mask;
314  error_reporting($currentEnabled);
315  self::$logMask |= $mask;
316  }
317 
318 
324  public static function popErrorMask()
325  {
326  $lastMask = array_pop(self::$logLevelStack);
327  error_reporting($lastMask[0]);
328  self::$logMask = $lastMask[1];
329  }
330 
331 
339  private static function defer($level, $message, $stats)
340  {
341  // save the message for later
342  self::$earlyLog[] = array('level' => $level, 'string' => $message, 'statsLog' => $stats);
343 
344  // register a shutdown handler if needed
345  if (!self::$shutdownRegistered) {
346  register_shutdown_function(array('SimpleSAML\Logger', 'flush'));
347  self::$shutdownRegistered = true;
348  }
349  }
350 
351 
352  private static function createLoggingHandler($handler = null)
353  {
354  // set to false to indicate that it is being initialized
355  self::$loggingHandler = false;
356 
357  // a set of known logging handlers
358  $known_handlers = array(
359  'syslog' => 'SimpleSAML\Logger\SyslogLoggingHandler',
360  'file' => 'SimpleSAML\Logger\FileLoggingHandler',
361  'errorlog' => 'SimpleSAML\Logger\ErrorLogLoggingHandler',
362  );
363 
364  // get the configuration
366  assert($config instanceof \SimpleSAML_Configuration);
367 
368  // setting minimum log_level
369  self::$logLevel = $config->getInteger('logging.level', self::INFO);
370 
371  // get the metadata handler option from the configuration
372  if (is_null($handler)) {
373  $handler = $config->getString('logging.handler', 'syslog');
374  }
375 
376  if (!array_key_exists($handler, $known_handlers) && class_exists($handler)) {
377  if (!in_array('SimpleSAML\Logger\LoggingHandlerInterface', class_implements($handler), true)) {
378  throw new \Exception("The logging handler '$handler' is invalid.");
379  }
380  } else {
381  $handler = strtolower($handler);
382  if (!array_key_exists($handler, $known_handlers)) {
383  throw new \Exception(
384  "Invalid value for the 'logging.handler' configuration option. Unknown handler '".$handler."''."
385  );
386  }
387  $handler = $known_handlers[$handler];
388  }
389  self::$loggingHandler = new $handler($config);
390 
391  self::$format = $config->getString('logging.format', self::$format);
392  self::$loggingHandler->setLogFormat(self::$format);
393  }
394 
395 
396  private static function log($level, $string, $statsLog = false)
397  {
398  if (self::$loggingHandler === false) {
399  // some error occurred while initializing logging
400  self::defer($level, $string, $statsLog);
401  return;
402  } elseif (php_sapi_name() === 'cli' || defined('STDIN')) {
403  // we are being executed from the CLI, nowhere to log
404  if (is_null(self::$loggingHandler)) {
405  self::createLoggingHandler('SimpleSAML\Logger\StandardErrorLoggingHandler');
406  }
407  $_SERVER['REMOTE_ADDR'] = "CLI";
408  if (self::$trackid === self::NO_TRACKID) {
409  self::$trackid = 'CL'.bin2hex(openssl_random_pseudo_bytes(4));
410  }
411  } elseif (self::$loggingHandler === null) {
412  // Initialize logging
413  self::createLoggingHandler();
414 
415  if (!empty(self::$earlyLog)) {
416  // output messages which were logged before we properly initialized logging
417  foreach (self::$earlyLog as $msg) {
418  self::log($msg['level'], $msg['string'], $msg['statsLog']);
419  }
420  }
421  }
422 
423  if (self::$captureLog) {
424  $ts = microtime(true);
425  $msecs = (int) (($ts - (int) $ts) * 1000);
426  $ts = gmdate('H:i:s', $ts).sprintf('.%03d', $msecs).'Z';
427  self::$capturedLog[] = $ts.' '.$string;
428  }
429 
430  if (self::$logLevel >= $level || $statsLog) {
431  if (is_array($string)) {
432  $string = implode(",", $string);
433  }
434 
435  $formats = array('%trackid', '%msg', '%srcip', '%stat');
436  $replacements = array(self::$trackid, $string, $_SERVER['REMOTE_ADDR']);
437 
438  $stat = '';
439  if ($statsLog) {
440  $stat = 'STAT ';
441  }
442  array_push($replacements, $stat);
443 
444  if (self::$trackid === self::NO_TRACKID && !self::$shuttingDown) {
445  // we have a log without track ID and we are not still shutting down, so defer logging
446  self::defer($level, $string, $statsLog);
447  return;
448  } elseif (self::$trackid === self::NO_TRACKID) {
449  // shutting down without a track ID, prettify it
450  array_shift($replacements);
451  array_unshift($replacements, 'N/A');
452  }
453 
454  // we either have a track ID or we are shutting down, so just log the message
455  $string = str_replace($formats, $replacements, self::$format);
456  self::$loggingHandler->log($level, $string);
457  }
458  }
459 }
if((!isset($_SERVER['DOCUMENT_ROOT'])) OR(empty($_SERVER['DOCUMENT_ROOT']))) $_SERVER['DOCUMENT_ROOT']
static popErrorMask()
Pop an error mask.
Definition: Logger.php:324
$format
Definition: metadata.php:141
static createLoggingHandler($handler=null)
Definition: Logger.php:352
static flush()
Flush any pending log messages to the logging handler.
Definition: Logger.php:268
static setCaptureLog($val=true)
Definition: Logger.php:235
static debug($string)
Definition: Logger.php:213
$s
Definition: pwgen.php:45
const DEBUG
static setTrackId($trackId)
Set the track identifier to use in all logs.
Definition: Logger.php:255
static notice($string)
Definition: Logger.php:190
static stats($string)
Definition: Logger.php:224
Attribute-related utility methods.
static info($string)
Definition: Logger.php:201
catch(Exception $e) $message
$mask
Definition: example_042.php:90
$stats
static warning($string)
Definition: Logger.php:179
static getCapturedLog()
Get the captured log.
Definition: Logger.php:244
static error($string)
Definition: Logger.php:168
static defer($level, $message, $stats)
Defer a message for later logging.
Definition: Logger.php:339
Create styles array
The data for the language used.
static maskErrors($mask)
Disable error reporting for the given log levels.
Definition: Logger.php:306
static critical($string)
Definition: Logger.php:146
static emergency($string)
Definition: Logger.php:135
static isErrorMasked($errno)
Evaluate whether errors of a certain error level are masked or not.
Definition: Logger.php:293
static alert($string)
Definition: Logger.php:157
defined( 'APPLICATION_ENV')||define( 'APPLICATION_ENV'
Definition: bootstrap.php:27
$handler
static getSessionFromRequest()
Retrieves the current session.
Definition: Session.php:243
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:396