Skip to content
Snippets Groups Projects
Select Git revision
  • 9202b3655ff921ea3cdae28b5104a226823cb225
  • main default protected
  • register-logging-channel
  • expr-lang
  • ci-82
  • attr-events
  • locale-wip
  • custom-routes
  • v0.1.85
  • v0.1.84
  • v0.1.83
  • v0.1.82
  • v0.1.81
  • v0.1.80
  • v0.1.79
  • v0.1.78
  • v0.1.77
  • v0.1.76
  • v0.1.75
  • v0.1.74
  • v0.1.73
  • v0.1.72
  • v0.1.71
  • v0.1.70
  • v0.1.69
  • v0.1.68
  • v0.1.67
  • v0.1.65
28 results

LoggingProcessor.php

Blame
  • Christoph Reiter's avatar
    Reiter, Christoph authored
    and remove roles and oidc specifics. Instead we provide the session
    in the core and forward requests to a oidc specific backend.
    
    This also means the session can provide useful values even in case
    it is used from the CLI and unauthenticated.
    7430fbcf
    History
    LoggingProcessor.php 1.94 KiB
    <?php
    
    declare(strict_types=1);
    
    namespace Dbp\Relay\CoreBundle\Logging;
    
    use Dbp\Relay\CoreBundle\API\UserSessionInterface;
    use Dbp\Relay\CoreBundle\Helpers\Tools as CoreTools;
    use Symfony\Component\HttpFoundation\RequestStack;
    use Symfony\Component\Uid\Uuid;
    
    final class LoggingProcessor
    {
        private $userDataProvider;
        private $requestStack;
    
        public function __construct(UserSessionInterface $userDataProvider, RequestStack $requestStack)
        {
            $this->userDataProvider = $userDataProvider;
            $this->requestStack = $requestStack;
        }
    
        private function maskUserId(array &$record)
        {
            try {
                $userId = $this->userDataProvider->getUserIdentifier();
            } catch (\Throwable $error) {
                // pre-auth
                $userId = null;
            }
    
            if ($userId !== null) {
                Tools::maskValues($record, [$userId], '*****');
            }
        }
    
        public function __invoke(array $record)
        {
            // Try to avoid information leaks (users should still not log sensitive information though...)
            $record['message'] = CoreTools::filterErrorMessage($record['message']);
    
            // Mask the user identifier
            $this->maskUserId($record);
    
            // Add a session ID (the same during multiple requests for the same user session)
            $record['context']['relay-session-id'] = $this->userDataProvider->getSessionLoggingId();
    
            // Add a request ID (the same during the same client request)
            $request = $this->requestStack->getMainRequest();
            if ($request !== null) {
                $requestAttributeKey = 'relay-request-id';
                $requestId = $request->attributes->get($requestAttributeKey);
                if ($requestId === null) {
                    $requestId = Uuid::v4()->toRfc4122();
                    $request->attributes->set($requestAttributeKey, $requestId);
                }
                $record['context']['relay-request-id'] = $requestId;
            }
    
            return $record;
        }
    }