Esempio n. 1
0
 public function testLogException()
 {
     $test = $this;
     // offset is the line-distance to the throw statement!
     $line = __LINE__ + 3;
     try {
         throw new Exception("test-message", 123);
     } catch (Exception $e) {
         $this->validatePublish(function (MessageInterface $message) use($e, $line, $test) {
             $test->assertContains($e->getMessage(), $message->getFullMessage());
             $test->assertContains(get_class($e), $message->getFullMessage());
             $test->assertEquals($line, $message->getLine());
             $test->assertEquals(__FILE__, $message->getFile());
         });
         $this->logger->log(LogLevel::ALERT, $e->getMessage(), array('exception' => $e));
     }
 }
 /**
  * @param \Exception $exception
  * @return void
  */
 public function logException(\Exception $exception)
 {
     if (!isset($this->settings['host']) || strlen($this->settings['host']) === 0) {
         return;
     }
     $statusCode = NULL;
     if ($exception instanceof FlowException) {
         $statusCode = $exception->getStatusCode();
     }
     // skip exceptions with status codes matching "skipStatusCodes" setting
     if (isset($this->settings['skipStatusCodes']) && in_array($statusCode, $this->settings['skipStatusCodes'])) {
         return;
     }
     $host = $this->settings['host'];
     $port = isset($this->settings['port']) ? $this->settings['port'] : UdpTransport::DEFAULT_PORT;
     // set chunk size option to wan (default) or lan
     if (isset($this->settings['chunksize']) && strtolower($this->settings['chunksize']) === 'lan') {
         $chunkSize = UdpTransport::CHUNK_SIZE_LAN;
     } else {
         $chunkSize = UdpTransport::CHUNK_SIZE_WAN;
     }
     // setup connection to graylog server
     $transport = new UdpTransport($host, $port, $chunkSize);
     $publisher = new Publisher();
     $publisher->addTransport($transport);
     // set logLevel depending on http status code
     $logLevel = 4;
     // warning
     if ($statusCode === 500) {
         $logLevel = 3;
         // error
     }
     // build message context
     $messageContext = array('full_message' => $exception->getTraceAsString(), 'reference_code' => $exception instanceof FlowException ? $exception->getReferenceCode() : NULL, 'response_status' => $statusCode, 'short_message' => sprintf('%d %s', $statusCode, Response::getStatusMessageByCode($statusCode)), 'code' => $exception->getCode(), 'file' => $exception->getFile(), 'line' => $exception->getLine());
     if ($this->securityContext !== NULL && $this->securityContext->isInitialized()) {
         $account = $this->securityContext->getAccount();
         if ($account !== NULL) {
             $messageContext['authenticated_account'] = $account->getAccountIdentifier() . ' (' . $this->persistenceManager->getIdentifierByObject($account) . ')';
             $messageContext['authenticated_roles'] = implode(', ', array_keys($this->securityContext->getRoles()));
             if ($this->objectManager->isRegistered(PartyService::class)) {
                 /** @var PartyService $partyService */
                 $partyService = $this->objectManager->get(PartyService::class);
                 $person = $partyService->getAssignedPartyOfAccount($account);
                 if ($person instanceof Person) {
                     $messageContext['authenticated_person'] = (string) $person->getName() . ' (' . $this->persistenceManager->getIdentifierByObject($person) . ')';
                 }
             }
         }
     }
     // prepare request details
     if (Bootstrap::$staticObjectManager instanceof ObjectManagerInterface) {
         $bootstrap = Bootstrap::$staticObjectManager->get('TYPO3\\Flow\\Core\\Bootstrap');
         /* @var Bootstrap $bootstrap */
         $requestHandler = $bootstrap->getActiveRequestHandler();
         if ($requestHandler instanceof HttpRequestHandlerInterface) {
             $request = $requestHandler->getHttpRequest();
             $requestData = array('request_domain' => $request->getHeader('Host'), 'request_remote_addr' => $request->getClientIpAddress(), 'request_path' => $request->getRelativePath(), 'request_uri' => $request->getUri()->getPath(), 'request_user_agent' => $request->getHeader('User-Agent'), 'request_method' => $request->getMethod(), 'request_port' => $request->getPort());
             $messageContext = array_merge($messageContext, $requestData);
         }
     }
     // send message to graylog server
     $logger = new Logger($publisher);
     $logger->log($logLevel, $exception->getMessage(), $messageContext);
 }