How do I create system logs during authentication?

I was following the guide here and implemented basic authentication system:
https://symfony.com/doc/current/security.html

However, I would like to add system logs to my application. To be precise, I would like:

  • to be able to insert a record with who successfully logged at what hour
  • to be able to record when someone tried to log and failed

I know that I can put logs in my SecurityController, like this:

public function login(AuthenticationUtils $authenticationUtils, Request $request, EntityManagerInterface $em): Response
{
    // if ($this->getUser()) {
    //     return $this->redirectToRoute('target_path');
    // }

    $log = new Logs();
    $em->persist($log);

    $log->setAction('auth')
        ->setDate(new DateTime())
        ->setIp($request->getClientIp());

    $em->flush();

    // get the login error if there is one
    $error = $authenticationUtils->getLastAuthenticationError();
    // last username entered by the user
    $lastUsername = $authenticationUtils->getLastUsername();

    return $this->render('security/login.html.twig', ['last_username' => $lastUsername, 'error' => $error]);
}

but it only gives me the information that someone was on the login page. What do I modify or add to get the other info?

Source: Symfony4 Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *