コード例 #1
0
ファイル: Logger.php プロジェクト: SysBind/simplesamlphp
 private static function createLoggingHandler($handler = null)
 {
     // set to false to indicate that it is being initialized
     self::$loggingHandler = false;
     // a set of known logging handlers
     $known_handlers = array('syslog' => 'SimpleSAML\\Logger\\SyslogLoggingHandler', 'file' => 'SimpleSAML\\Logger\\FileLoggingHandler', 'errorlog' => 'SimpleSAML\\Logger\\ErrorLogLoggingHandler');
     // get the configuration
     $config = \SimpleSAML_Configuration::getInstance();
     assert($config instanceof \SimpleSAML_Configuration);
     // setting minimum log_level
     self::$logLevel = $config->getInteger('logging.level', self::INFO);
     // get the metadata handler option from the configuration
     if (is_null($handler)) {
         $handler = $config->getString('logging.handler', 'syslog');
     }
     if (class_exists($handler)) {
         if (!in_array('SimpleSAML\\Logger\\LoggingHandlerInterface', class_implements($handler))) {
             throw new \Exception("The logging handler '{$handler}' is invalid.");
         }
     } else {
         $handler = strtolower($handler);
         if (!array_key_exists($handler, $known_handlers)) {
             throw new \Exception("Invalid value for the 'logging.handler' configuration option. Unknown handler '" . $handler . "''.");
         }
         $handler = $known_handlers[$handler];
     }
     self::$loggingHandler = new $handler($config);
     self::$format = $config->getString('logging.format', self::$format);
     self::$loggingHandler->setLogFormat(self::$format);
 }
コード例 #2
0
 private static function log($level, $string, $statsLog = false)
 {
     if (self::$loggingHandler === false) {
         // some error occurred while initializing logging
         self::defer($level, $string, $statsLog);
         return;
     } elseif (php_sapi_name() === 'cli' || defined('STDIN')) {
         // we are being executed from the CLI, nowhere to log
         if (is_null(self::$loggingHandler)) {
             self::createLoggingHandler('SimpleSAML\\Logger\\StandardErrorLoggingHandler');
         }
         $_SERVER['REMOTE_ADDR'] = "CLI";
         if (self::$trackid === self::NO_TRACKID) {
             self::$trackid = 'CL' . bin2hex(openssl_random_pseudo_bytes(4));
         }
     } elseif (self::$loggingHandler === null) {
         // Initialize logging
         self::createLoggingHandler();
         if (!empty(self::$earlyLog)) {
             // output messages which were logged before we properly initialized logging
             foreach (self::$earlyLog as $msg) {
                 self::log($msg['level'], $msg['string'], $msg['statsLog']);
             }
         }
     }
     if (self::$captureLog) {
         $ts = microtime(true);
         $msecs = (int) (($ts - (int) $ts) * 1000);
         $ts = gmdate('H:i:s', $ts) . sprintf('.%03d', $msecs) . 'Z';
         self::$capturedLog[] = $ts . ' ' . $string;
     }
     if (self::$logLevel >= $level || $statsLog) {
         if (is_array($string)) {
             $string = implode(",", $string);
         }
         $formats = array('%trackid', '%msg', '%srcip', '%stat');
         $replacements = array(self::$trackid, $string, $_SERVER['REMOTE_ADDR']);
         $stat = '';
         if ($statsLog) {
             $stat = 'STAT ';
         }
         array_push($replacements, $stat);
         if (self::$trackid === self::NO_TRACKID && !self::$shuttingDown) {
             // we have a log without track ID and we are not still shutting down, so defer logging
             self::defer($level, $string, $statsLog);
             return;
         } elseif (self::$trackid === self::NO_TRACKID) {
             // shutting down without a track ID, prettify it
             array_shift($replacements);
             array_unshift($replacements, 'N/A');
         }
         // we either have a track ID or we are shutting down, so just log the message
         $string = str_replace($formats, $replacements, self::$format);
         self::$loggingHandler->log($level, $string);
     }
 }