feature #36455 [Cache] Added context to log messages (Nyholm)

This PR was squashed before being merged into the 5.1-dev branch.

Discussion
----------

[Cache] Added context to log messages

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| Deprecations? | no
| Tickets       |
| License       | MIT
| Doc PR        | n/a

In my application logs, I've got many entries like:

> Failed to save key "foobar" of type string.

I know it is related to the cache, But I dont know from what adapter. I use a chain of Array, Apcu and Redis. This PR adds some context to that log entry so I know which one of my cache adapter that fails.

Commits
-------

a4d9e0fc94 [Cache] Added context to log messages
This commit is contained in:
Fabien Potencier 2020-04-17 05:32:08 +02:00
commit a85545f3e9
5 changed files with 14 additions and 13 deletions

View File

@ -166,7 +166,7 @@ abstract class AbstractAdapter implements AdapterInterface, CacheInterface, Logg
$v = $values[$id]; $v = $values[$id];
$type = get_debug_type($v); $type = get_debug_type($v);
$message = sprintf('Failed to save key "{key}" of type %s%s', $type, $e instanceof \Exception ? ': '.$e->getMessage() : '.'); $message = sprintf('Failed to save key "{key}" of type %s%s', $type, $e instanceof \Exception ? ': '.$e->getMessage() : '.');
CacheItem::log($this->logger, $message, ['key' => substr($id, \strlen($this->namespace)), 'exception' => $e instanceof \Exception ? $e : null]); CacheItem::log($this->logger, $message, ['key' => substr($id, \strlen($this->namespace)), 'exception' => $e instanceof \Exception ? $e : null, 'cache-adapter' => get_debug_type($this)]);
} }
} else { } else {
foreach ($values as $id => $v) { foreach ($values as $id => $v) {
@ -189,7 +189,7 @@ abstract class AbstractAdapter implements AdapterInterface, CacheInterface, Logg
$ok = false; $ok = false;
$type = get_debug_type($v); $type = get_debug_type($v);
$message = sprintf('Failed to save key "{key}" of type %s%s', $type, $e instanceof \Exception ? ': '.$e->getMessage() : '.'); $message = sprintf('Failed to save key "{key}" of type %s%s', $type, $e instanceof \Exception ? ': '.$e->getMessage() : '.');
CacheItem::log($this->logger, $message, ['key' => substr($id, \strlen($this->namespace)), 'exception' => $e instanceof \Exception ? $e : null]); CacheItem::log($this->logger, $message, ['key' => substr($id, \strlen($this->namespace)), 'exception' => $e instanceof \Exception ? $e : null, 'cache-adapter' => get_debug_type($this)]);
} }
} }

View File

@ -196,7 +196,7 @@ abstract class AbstractTagAwareAdapter implements TagAwareAdapterInterface, TagA
$v = $values[$id]; $v = $values[$id];
$type = get_debug_type($v); $type = get_debug_type($v);
$message = sprintf('Failed to save key "{key}" of type %s%s', $type, $e instanceof \Exception ? ': '.$e->getMessage() : '.'); $message = sprintf('Failed to save key "{key}" of type %s%s', $type, $e instanceof \Exception ? ': '.$e->getMessage() : '.');
CacheItem::log($this->logger, $message, ['key' => substr($id, \strlen($this->namespace)), 'exception' => $e instanceof \Exception ? $e : null]); CacheItem::log($this->logger, $message, ['key' => substr($id, \strlen($this->namespace)), 'exception' => $e instanceof \Exception ? $e : null, 'cache-adapter' => get_debug_type($this)]);
} }
} else { } else {
foreach ($values as $id => $v) { foreach ($values as $id => $v) {
@ -220,7 +220,7 @@ abstract class AbstractTagAwareAdapter implements TagAwareAdapterInterface, TagA
$ok = false; $ok = false;
$type = get_debug_type($v); $type = get_debug_type($v);
$message = sprintf('Failed to save key "{key}" of type %s%s', $type, $e instanceof \Exception ? ': '.$e->getMessage() : '.'); $message = sprintf('Failed to save key "{key}" of type %s%s', $type, $e instanceof \Exception ? ': '.$e->getMessage() : '.');
CacheItem::log($this->logger, $message, ['key' => substr($id, \strlen($this->namespace)), 'exception' => $e instanceof \Exception ? $e : null]); CacheItem::log($this->logger, $message, ['key' => substr($id, \strlen($this->namespace)), 'exception' => $e instanceof \Exception ? $e : null, 'cache-adapter' => get_debug_type($this)]);
} }
} }
@ -272,7 +272,7 @@ abstract class AbstractTagAwareAdapter implements TagAwareAdapterInterface, TagA
} catch (\Exception $e) { } catch (\Exception $e) {
} }
$message = 'Failed to delete key "{key}"'.($e instanceof \Exception ? ': '.$e->getMessage() : '.'); $message = 'Failed to delete key "{key}"'.($e instanceof \Exception ? ': '.$e->getMessage() : '.');
CacheItem::log($this->logger, $message, ['key' => $key, 'exception' => $e]); CacheItem::log($this->logger, $message, ['key' => $key, 'exception' => $e, 'cache-adapter' => get_debug_type($this)]);
$ok = false; $ok = false;
} }

View File

@ -353,7 +353,7 @@ class ArrayAdapter implements AdapterInterface, CacheInterface, LoggerAwareInter
} catch (\Exception $e) { } catch (\Exception $e) {
$type = get_debug_type($value); $type = get_debug_type($value);
$message = sprintf('Failed to save key "{key}" of type %s: %s', $type, $e->getMessage()); $message = sprintf('Failed to save key "{key}" of type %s: %s', $type, $e->getMessage());
CacheItem::log($this->logger, $message, ['key' => $key, 'exception' => $e]); CacheItem::log($this->logger, $message, ['key' => $key, 'exception' => $e, 'cache-adapter' => get_debug_type($this)]);
return; return;
} }
@ -375,7 +375,7 @@ class ArrayAdapter implements AdapterInterface, CacheInterface, LoggerAwareInter
try { try {
$value = unserialize($value); $value = unserialize($value);
} catch (\Exception $e) { } catch (\Exception $e) {
CacheItem::log($this->logger, 'Failed to unserialize key "{key}": '.$e->getMessage(), ['key' => $key, 'exception' => $e]); CacheItem::log($this->logger, 'Failed to unserialize key "{key}": '.$e->getMessage(), ['key' => $key, 'exception' => $e, 'cache-adapter' => get_debug_type($this)]);
$value = false; $value = false;
} }
if (false === $value) { if (false === $value) {

View File

@ -6,6 +6,7 @@ CHANGELOG
* added max-items + LRU + max-lifetime capabilities to `ArrayCache` * added max-items + LRU + max-lifetime capabilities to `ArrayCache`
* added `CouchbaseBucketAdapter` * added `CouchbaseBucketAdapter`
* added context `cache-adapter` to log messages
5.0.0 5.0.0
----- -----

View File

@ -107,7 +107,7 @@ trait AbstractAdapterTrait
try { try {
return $this->doHave($id); return $this->doHave($id);
} catch (\Exception $e) { } catch (\Exception $e) {
CacheItem::log($this->logger, 'Failed to check if key "{key}" is cached: '.$e->getMessage(), ['key' => $key, 'exception' => $e]); CacheItem::log($this->logger, 'Failed to check if key "{key}" is cached: '.$e->getMessage(), ['key' => $key, 'exception' => $e, 'cache-adapter' => get_debug_type($this)]);
return false; return false;
} }
@ -145,7 +145,7 @@ trait AbstractAdapterTrait
try { try {
return $this->doClear($namespaceToClear) || $cleared; return $this->doClear($namespaceToClear) || $cleared;
} catch (\Exception $e) { } catch (\Exception $e) {
CacheItem::log($this->logger, 'Failed to clear the cache: '.$e->getMessage(), ['exception' => $e]); CacheItem::log($this->logger, 'Failed to clear the cache: '.$e->getMessage(), ['exception' => $e, 'cache-adapter' => get_debug_type($this)]);
return false; return false;
} }
@ -194,7 +194,7 @@ trait AbstractAdapterTrait
} catch (\Exception $e) { } catch (\Exception $e) {
} }
$message = 'Failed to delete key "{key}"'.($e instanceof \Exception ? ': '.$e->getMessage() : '.'); $message = 'Failed to delete key "{key}"'.($e instanceof \Exception ? ': '.$e->getMessage() : '.');
CacheItem::log($this->logger, $message, ['key' => $key, 'exception' => $e]); CacheItem::log($this->logger, $message, ['key' => $key, 'exception' => $e, 'cache-adapter' => get_debug_type($this)]);
$ok = false; $ok = false;
} }
@ -222,7 +222,7 @@ trait AbstractAdapterTrait
return $f($key, $value, $isHit); return $f($key, $value, $isHit);
} catch (\Exception $e) { } catch (\Exception $e) {
CacheItem::log($this->logger, 'Failed to fetch key "{key}": '.$e->getMessage(), ['key' => $key, 'exception' => $e]); CacheItem::log($this->logger, 'Failed to fetch key "{key}": '.$e->getMessage(), ['key' => $key, 'exception' => $e, 'cache-adapter' => get_debug_type($this)]);
} }
return $f($key, null, false); return $f($key, null, false);
@ -244,7 +244,7 @@ trait AbstractAdapterTrait
try { try {
$items = $this->doFetch($ids); $items = $this->doFetch($ids);
} catch (\Exception $e) { } catch (\Exception $e) {
CacheItem::log($this->logger, 'Failed to fetch items: '.$e->getMessage(), ['keys' => $keys, 'exception' => $e]); CacheItem::log($this->logger, 'Failed to fetch items: '.$e->getMessage(), ['keys' => $keys, 'exception' => $e, 'cache-adapter' => get_debug_type($this)]);
$items = []; $items = [];
} }
$ids = array_combine($ids, $keys); $ids = array_combine($ids, $keys);
@ -347,7 +347,7 @@ trait AbstractAdapterTrait
yield $key => $f($key, $value, true); yield $key => $f($key, $value, true);
} }
} catch (\Exception $e) { } catch (\Exception $e) {
CacheItem::log($this->logger, 'Failed to fetch items: '.$e->getMessage(), ['keys' => array_values($keys), 'exception' => $e]); CacheItem::log($this->logger, 'Failed to fetch items: '.$e->getMessage(), ['keys' => array_values($keys), 'exception' => $e, 'cache-adapter' => get_debug_type($this)]);
} }
foreach ($keys as $key) { foreach ($keys as $key) {