Skip to content
This repository was archived by the owner on May 31, 2024. It is now read-only.

Commit e26eec7

Browse files
Iltar van der Bergfabpot
authored andcommitted
[DX] Attempt to improve logging messages with parameters
1 parent 000c29c commit e26eec7

18 files changed

+74
-66
lines changed

Acl/Voter/AclVoter.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public function vote(TokenInterface $token, $object, array $attributes)
6464

6565
if (null === $object) {
6666
if (null !== $this->logger) {
67-
$this->logger->debug(sprintf('Object identity unavailable. Voting to %s', $this->allowIfObjectIdentityUnavailable ? 'grant access' : 'abstain'));
67+
$this->logger->debug(sprintf('Object identity unavailable. Voting to %s.', $this->allowIfObjectIdentityUnavailable ? 'grant access' : 'abstain'));
6868
}
6969

7070
return $this->allowIfObjectIdentityUnavailable ? self::ACCESS_GRANTED : self::ACCESS_ABSTAIN;
@@ -79,7 +79,7 @@ public function vote(TokenInterface $token, $object, array $attributes)
7979
$oid = $object;
8080
} elseif (null === $oid = $this->objectIdentityRetrievalStrategy->getObjectIdentity($object)) {
8181
if (null !== $this->logger) {
82-
$this->logger->debug(sprintf('Object identity unavailable. Voting to %s', $this->allowIfObjectIdentityUnavailable ? 'grant access' : 'abstain'));
82+
$this->logger->debug(sprintf('Object identity unavailable. Voting to %s.', $this->allowIfObjectIdentityUnavailable ? 'grant access' : 'abstain'));
8383
}
8484

8585
return $this->allowIfObjectIdentityUnavailable ? self::ACCESS_GRANTED : self::ACCESS_ABSTAIN;
@@ -96,13 +96,13 @@ public function vote(TokenInterface $token, $object, array $attributes)
9696

9797
if (null === $field && $acl->isGranted($masks, $sids, false)) {
9898
if (null !== $this->logger) {
99-
$this->logger->debug('ACL found, permission granted. Voting to grant access');
99+
$this->logger->debug('ACL found, permission granted. Voting to grant access.');
100100
}
101101

102102
return self::ACCESS_GRANTED;
103103
} elseif (null !== $field && $acl->isFieldGranted($field, $masks, $sids, false)) {
104104
if (null !== $this->logger) {
105-
$this->logger->debug('ACL found, permission granted. Voting to grant access');
105+
$this->logger->debug('ACL found, permission granted. Voting to grant access.');
106106
}
107107

108108
return self::ACCESS_GRANTED;

Http/Authentication/DefaultAuthenticationFailureHandler.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public function onAuthenticationFailure(Request $request, AuthenticationExceptio
9292

9393
if ($this->options['failure_forward']) {
9494
if (null !== $this->logger) {
95-
$this->logger->debug(sprintf('Forwarding to %s', $this->options['failure_path']));
95+
$this->logger->debug('Authentication failure, forward triggered.', array('failure_path' => $this->options['failure_path']));
9696
}
9797

9898
$subRequest = $this->httpUtils->createRequest($request, $this->options['failure_path']);
@@ -102,7 +102,7 @@ public function onAuthenticationFailure(Request $request, AuthenticationExceptio
102102
}
103103

104104
if (null !== $this->logger) {
105-
$this->logger->debug(sprintf('Redirecting to %s', $this->options['failure_path']));
105+
$this->logger->debug('Authentication failure, redirect triggered.', array('failure_path' => $this->options['failure_path']));
106106
}
107107

108108
$request->getSession()->set(Security::AUTHENTICATION_ERROR, $exception);

Http/Authentication/SimpleAuthenticationHandler.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public function onAuthenticationSuccess(Request $request, TokenInterface $token)
5757
{
5858
if ($this->simpleAuthenticator instanceof AuthenticationSuccessHandlerInterface) {
5959
if ($this->logger) {
60-
$this->logger->debug(sprintf('Using the %s object as authentication success handler', get_class($this->simpleAuthenticator)));
60+
$this->logger->debug('Selected an authentication success handler.', array('handler' => get_class($this->simpleAuthenticator)));
6161
}
6262

6363
$response = $this->simpleAuthenticator->onAuthenticationSuccess($request, $token);
@@ -71,7 +71,7 @@ public function onAuthenticationSuccess(Request $request, TokenInterface $token)
7171
}
7272

7373
if ($this->logger) {
74-
$this->logger->debug('Fallback to the default authentication success handler');
74+
$this->logger->debug('Fallback to the default authentication success handler.');
7575
}
7676

7777
return $this->successHandler->onAuthenticationSuccess($request, $token);
@@ -84,7 +84,7 @@ public function onAuthenticationFailure(Request $request, AuthenticationExceptio
8484
{
8585
if ($this->simpleAuthenticator instanceof AuthenticationFailureHandlerInterface) {
8686
if ($this->logger) {
87-
$this->logger->debug(sprintf('Using the %s object as authentication failure handler', get_class($this->simpleAuthenticator)));
87+
$this->logger->debug('Selected an authentication failure handler.', array('handler' => get_class($this->simpleAuthenticator)));
8888
}
8989

9090
$response = $this->simpleAuthenticator->onAuthenticationFailure($request, $exception);
@@ -98,7 +98,7 @@ public function onAuthenticationFailure(Request $request, AuthenticationExceptio
9898
}
9999

100100
if ($this->logger) {
101-
$this->logger->debug('Fallback to the default authentication failure handler');
101+
$this->logger->debug('Fallback to the default authentication failure handler.');
102102
}
103103

104104
return $this->failureHandler->onAuthenticationFailure($request, $exception);

Http/EntryPoint/DigestAuthenticationEntryPoint.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function start(Request $request, AuthenticationException $authException =
5454
}
5555

5656
if (null !== $this->logger) {
57-
$this->logger->debug(sprintf('WWW-Authenticate header sent to user agent: "%s"', $authenticateHeader));
57+
$this->logger->debug('WWW-Authenticate header sent.', array('header' => $authenticateHeader));
5858
}
5959

6060
$response = new Response();

Http/Firewall/AbstractAuthenticationListener.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ abstract protected function attemptAuthentication(Request $request);
193193
private function onFailure(Request $request, AuthenticationException $failed)
194194
{
195195
if (null !== $this->logger) {
196-
$this->logger->info(sprintf('Authentication request failed: %s', $failed->getMessage()));
196+
$this->logger->info('Authentication request failed.', array('exception' => $failed));
197197
}
198198

199199
$token = $this->tokenStorage->getToken();
@@ -213,7 +213,7 @@ private function onFailure(Request $request, AuthenticationException $failed)
213213
private function onSuccess(Request $request, TokenInterface $token)
214214
{
215215
if (null !== $this->logger) {
216-
$this->logger->info(sprintf('User "%s" has been authenticated successfully', $token->getUsername()));
216+
$this->logger->info('User has been authenticated successfully.', array('username' => $token->getUsername()));
217217
}
218218

219219
$this->tokenStorage->setToken($token);

Http/Firewall/AbstractPreAuthenticatedListener.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,6 @@ final public function handle(GetResponseEvent $event)
5656
{
5757
$request = $event->getRequest();
5858

59-
if (null !== $this->logger) {
60-
$this->logger->debug(sprintf('Checking secure context token: %s', $this->tokenStorage->getToken()));
61-
}
62-
6359
try {
6460
list($user, $credentials) = $this->getPreAuthenticatedData($request);
6561
} catch (BadCredentialsException $exception) {
@@ -68,21 +64,25 @@ final public function handle(GetResponseEvent $event)
6864
return;
6965
}
7066

67+
if (null !== $this->logger) {
68+
$this->logger->debug('Checking current security token.', array('token' => (string) $this->tokenStorage->getToken()));
69+
}
70+
7171
if (null !== $token = $this->tokenStorage->getToken()) {
7272
if ($token instanceof PreAuthenticatedToken && $this->providerKey == $token->getProviderKey() && $token->isAuthenticated() && $token->getUsername() === $user) {
7373
return;
7474
}
7575
}
7676

7777
if (null !== $this->logger) {
78-
$this->logger->debug(sprintf('Trying to pre-authenticate user "%s"', $user));
78+
$this->logger->debug('Trying to pre-authenticate user.', array('username' => (string) $user));
7979
}
8080

8181
try {
8282
$token = $this->authenticationManager->authenticate(new PreAuthenticatedToken($user, $credentials, $this->providerKey));
8383

8484
if (null !== $this->logger) {
85-
$this->logger->info(sprintf('Authentication success: %s', $token));
85+
$this->logger->info('Pre-authentication successful.', array('token' => (string) $token));
8686
}
8787
$this->tokenStorage->setToken($token);
8888

@@ -107,7 +107,7 @@ private function clearToken(AuthenticationException $exception)
107107
$this->tokenStorage->setToken(null);
108108

109109
if (null !== $this->logger) {
110-
$this->logger->info(sprintf("Cleared security context due to exception: %s", $exception->getMessage()));
110+
$this->logger->info('Cleared security token due to an exception.', array('exception' => $exception));
111111
}
112112
}
113113
}

Http/Firewall/AnonymousAuthenticationListener.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,11 @@ public function handle(GetResponseEvent $event)
5959
$this->tokenStorage->setToken($token);
6060

6161
if (null !== $this->logger) {
62-
$this->logger->info('Populated TokenStorage with an anonymous Token');
62+
$this->logger->info('Populated the TokenStorage with an anonymous Token.');
6363
}
6464
} catch (AuthenticationException $failed) {
6565
if (null !== $this->logger) {
66-
$this->logger->info(sprintf('Anonymous authentication failed: %s', $failed->getMessage()));
66+
$this->logger->info('Anonymous authentication failed.', array('exception' => $failed));
6767
}
6868
}
6969
}

Http/Firewall/BasicAuthenticationListener.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public function handle(GetResponseEvent $event)
6767
}
6868

6969
if (null !== $this->logger) {
70-
$this->logger->info(sprintf('Basic Authentication Authorization header found for user "%s"', $username));
70+
$this->logger->info('Basic authentication Authorization header found for user.', array('username' => $username));
7171
}
7272

7373
try {
@@ -80,7 +80,7 @@ public function handle(GetResponseEvent $event)
8080
}
8181

8282
if (null !== $this->logger) {
83-
$this->logger->info(sprintf('Authentication request failed for user "%s": %s', $username, $failed->getMessage()));
83+
$this->logger->info('Basic authentication failed for user.', array('username' => $username, 'exception' => $failed));
8484
}
8585

8686
if ($this->ignoreFailure) {

Http/Firewall/ChannelListener.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@ public function handle(GetResponseEvent $event)
4444
{
4545
$request = $event->getRequest();
4646

47-
list($attributes, $channel) = $this->map->getPatterns($request);
47+
list(, $channel) = $this->map->getPatterns($request);
4848

4949
if ('https' === $channel && !$request->isSecure()) {
5050
if (null !== $this->logger) {
51-
$this->logger->info('Redirecting to HTTPS');
51+
$this->logger->info('Redirecting to HTTPS.');
5252
}
5353

5454
$response = $this->authenticationEntryPoint->start($request);
@@ -60,7 +60,7 @@ public function handle(GetResponseEvent $event)
6060

6161
if ('http' === $channel && $request->isSecure()) {
6262
if (null !== $this->logger) {
63-
$this->logger->info('Redirecting to HTTP');
63+
$this->logger->info('Redirecting to HTTP.');
6464
}
6565

6666
$response = $this->authenticationEntryPoint->start($request);

Http/Firewall/ContextListener.php

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class ContextListener implements ListenerInterface
3434
{
3535
private $tokenStorage;
3636
private $contextKey;
37+
private $sessionKey;
3738
private $logger;
3839
private $userProviders;
3940
private $dispatcher;
@@ -54,12 +55,13 @@ public function __construct(TokenStorageInterface $tokenStorage, array $userProv
5455
$this->tokenStorage = $tokenStorage;
5556
$this->userProviders = $userProviders;
5657
$this->contextKey = $contextKey;
58+
$this->sessionKey = '_security_'.$contextKey;
5759
$this->logger = $logger;
5860
$this->dispatcher = $dispatcher;
5961
}
6062

6163
/**
62-
* Reads the SecurityContext from the session.
64+
* Reads the Security Token from the session.
6365
*
6466
* @param GetResponseEvent $event A GetResponseEvent instance
6567
*/
@@ -73,7 +75,7 @@ public function handle(GetResponseEvent $event)
7375
$request = $event->getRequest();
7476
$session = $request->hasPreviousSession() ? $request->getSession() : null;
7577

76-
if (null === $session || null === $token = $session->get('_security_'.$this->contextKey)) {
78+
if (null === $session || null === $token = $session->get($this->sessionKey)) {
7779
$this->tokenStorage->setToken(null);
7880

7981
return;
@@ -82,14 +84,14 @@ public function handle(GetResponseEvent $event)
8284
$token = unserialize($token);
8385

8486
if (null !== $this->logger) {
85-
$this->logger->debug('Read SecurityContext from the session');
87+
$this->logger->debug('Read existing security token from the session.', array('key' => $this->sessionKey));
8688
}
8789

8890
if ($token instanceof TokenInterface) {
8991
$token = $this->refreshUser($token);
9092
} elseif (null !== $token) {
9193
if (null !== $this->logger) {
92-
$this->logger->warning(sprintf('Session includes a "%s" where a security token is expected', is_object($token) ? get_class($token) : gettype($token)));
94+
$this->logger->warning('Expected a security token from the session, got something else.', array('key' => $this->sessionKey, 'received' => $token));
9395
}
9496

9597
$token = null;
@@ -113,10 +115,6 @@ public function onKernelResponse(FilterResponseEvent $event)
113115
return;
114116
}
115117

116-
if (null !== $this->logger) {
117-
$this->logger->debug('Write SecurityContext in the session');
118-
}
119-
120118
$request = $event->getRequest();
121119
$session = $request->getSession();
122120

@@ -126,10 +124,14 @@ public function onKernelResponse(FilterResponseEvent $event)
126124

127125
if ((null === $token = $this->tokenStorage->getToken()) || ($token instanceof AnonymousToken)) {
128126
if ($request->hasPreviousSession()) {
129-
$session->remove('_security_'.$this->contextKey);
127+
$session->remove($this->sessionKey);
130128
}
131129
} else {
132-
$session->set('_security_'.$this->contextKey, serialize($token));
130+
$session->set($this->sessionKey, serialize($token));
131+
132+
if (null !== $this->logger) {
133+
$this->logger->debug('Stored the security token in the session.', array('key' => $this->sessionKey));
134+
}
133135
}
134136
}
135137

@@ -149,25 +151,21 @@ protected function refreshUser(TokenInterface $token)
149151
return $token;
150152
}
151153

152-
if (null !== $this->logger) {
153-
$this->logger->debug(sprintf('Reloading user from user provider.'));
154-
}
155-
156154
foreach ($this->userProviders as $provider) {
157155
try {
158156
$refreshedUser = $provider->refreshUser($user);
159157
$token->setUser($refreshedUser);
160158

161159
if (null !== $this->logger) {
162-
$this->logger->debug(sprintf('Username "%s" was reloaded from user provider.', $refreshedUser->getUsername()));
160+
$this->logger->debug('User was reloaded from a user provider.', array('username' => $refreshedUser->getUsername(), 'provider' => get_class($provider)));
163161
}
164162

165163
return $token;
166164
} catch (UnsupportedUserException $unsupported) {
167165
// let's try the next user provider
168166
} catch (UsernameNotFoundException $notFound) {
169167
if (null !== $this->logger) {
170-
$this->logger->warning(sprintf('Username "%s" could not be found.', $notFound->getUsername()));
168+
$this->logger->warning('Username could not be found in the selected user provider.', array('username' => $notFound->getUsername(), 'provider' => get_class($provider)));
171169
}
172170

173171
return;

0 commit comments

Comments
 (0)