core-bundle/src/EventListener/SwitchUserListener.php
public function onSwitchUser(SwitchUserEvent $event): void
    {
        $token = $this->tokenStorage->getToken();

        if (null === $token) {
            throw new \RuntimeException('The token storage did not contain a token.');
        }

        $sourceUser = $token->getUser();

        if ($sourceUser instanceof UserInterface) {
            $sourceUser = $sourceUser->getUsername();
        }

        $targetUser = $event->getTargetUser();

        if ($targetUser instanceof UserInterface) {
            $targetUser = $targetUser->getUsername();
        }

        $this->logger->info(
            sprintf('User "%s" has switched to user "%s"', $sourceUser, $targetUser),
            ['contao' => new ContaoContext(__METHOD__, ContaoContext::ACCESS, $sourceUser)]
        );
    }
public function onSwitchUser(SwitchUserEvent $event): void
    {
        $token = $this->tokenStorage->getToken();

        if (null === $token) {
            throw new \RuntimeException('The token storage did not contain a token.');
        }

        $sourceUser = $token->getUser();

        if ($sourceUser instanceof UserInterface) {
            $sourceUser = $sourceUser->getUsername();
        }

        $targetUser = $event->getTargetUser();

        if ($targetUser instanceof UserInterface) {
            $targetUser = $targetUser->getUsername();
        }

        $this->logger->info(
            sprintf('User "%s" has switched to user "%s"', $sourceUser, $targetUser),
            ['contao' => new ContaoContext(__METHOD__, ContaoContext::ACCESS, $sourceUser)]
        );
    }
Logs successful user impersonations.

@throws \RuntimeException