Skip to content

Commit 4aaf871

Browse files
committed
feature #1297 Add example and documentation of logging (94noni)
This PR was merged into the main branch. Discussion ---------- Add example and documentation of logging I found no example of log inside the demo app, I think it could be wise to showcase it Ref https://symfony.com/doc/current/index.html in the `The Basics` toc Commits ------- 26bd476 Add example and documentation of logging
2 parents 612c3db + 26bd476 commit 4aaf871

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

src/Command/DeleteUserCommand.php

+12-3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use App\Repository\UserRepository;
1616
use App\Utils\Validator;
1717
use Doctrine\ORM\EntityManagerInterface;
18+
use Psr\Log\LoggerInterface;
1819
use Symfony\Component\Console\Attribute\AsCommand;
1920
use Symfony\Component\Console\Command\Command;
2021
use Symfony\Component\Console\Exception\RuntimeException;
@@ -49,7 +50,8 @@ class DeleteUserCommand extends Command
4950
public function __construct(
5051
private EntityManagerInterface $entityManager,
5152
private Validator $validator,
52-
private UserRepository $users
53+
private UserRepository $users,
54+
private LoggerInterface $logger
5355
) {
5456
parent::__construct();
5557
}
@@ -114,15 +116,22 @@ protected function execute(InputInterface $input, OutputInterface $output): int
114116
throw new RuntimeException(sprintf('User with username "%s" not found.', $username));
115117
}
116118

117-
// After an entity has been removed its in-memory state is the same
119+
// After an entity has been removed, its in-memory state is the same
118120
// as before the removal, except for generated identifiers.
119121
// See https://www.doctrine-project.org/projects/doctrine-orm/en/latest/reference/working-with-objects.html#removing-entities
120122
$userId = $user->getId();
121123

122124
$this->entityManager->remove($user);
123125
$this->entityManager->flush();
124126

125-
$this->io->success(sprintf('User "%s" (ID: %d, email: %s) was successfully deleted.', $user->getUsername(), $userId, $user->getEmail()));
127+
$userUsername = $user->getUsername();
128+
$userEmail = $user->getEmail();
129+
130+
$this->io->success(sprintf('User "%s" (ID: %d, email: %s) was successfully deleted.', $userUsername, $userId, $userEmail));
131+
132+
// Logging is helpful and important to keep a trace of what happened in the software runtime flow.
133+
// See https://symfony.com/doc/current/logging.html
134+
$this->logger->info('User "{username}" (ID: {id}, email: {email}) was successfully deleted.', ['username' => $userUsername, 'id' => $userId, 'email' => $userEmail]);
126135

127136
return Command::SUCCESS;
128137
}

0 commit comments

Comments
 (0)