Skip to content

Commit

Permalink
feature #36455 [Cache] Added context to log messages (Nyholm)
Browse files Browse the repository at this point in the history
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
-------

a4d9e0f [Cache] Added context to log messages
  • Loading branch information
fabpot committed Apr 17, 2020
2 parents 5369aff + a4d9e0f commit a85545f
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/Symfony/Component/Cache/Adapter/AbstractAdapter.php
Expand Up @@ -166,7 +166,7 @@ public function commit()
$v = $values[$id];
$type = get_debug_type($v);
$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 {
foreach ($values as $id => $v) {
Expand All @@ -189,7 +189,7 @@ public function commit()
$ok = false;
$type = get_debug_type($v);
$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)]);
}
}

Expand Down
Expand Up @@ -196,7 +196,7 @@ public function commit(): bool
$v = $values[$id];
$type = get_debug_type($v);
$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 {
foreach ($values as $id => $v) {
Expand All @@ -220,7 +220,7 @@ public function commit(): bool
$ok = false;
$type = get_debug_type($v);
$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)]);
}
}

Expand Down Expand Up @@ -272,7 +272,7 @@ public function deleteItems(array $keys): bool
} catch (\Exception $e) {
}
$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;
}

Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/Cache/Adapter/ArrayAdapter.php
Expand Up @@ -353,7 +353,7 @@ private function freeze($value, $key)
} catch (\Exception $e) {
$type = get_debug_type($value);
$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;
}
Expand All @@ -375,7 +375,7 @@ private function unfreeze(string $key, bool &$isHit)
try {
$value = unserialize($value);
} 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;
}
if (false === $value) {
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/Cache/CHANGELOG.md
Expand Up @@ -6,6 +6,7 @@ CHANGELOG

* added max-items + LRU + max-lifetime capabilities to `ArrayCache`
* added `CouchbaseBucketAdapter`
* added context `cache-adapter` to log messages

5.0.0
-----
Expand Down
12 changes: 6 additions & 6 deletions src/Symfony/Component/Cache/Traits/AbstractAdapterTrait.php
Expand Up @@ -107,7 +107,7 @@ public function hasItem($key)
try {
return $this->doHave($id);
} 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;
}
Expand Down Expand Up @@ -145,7 +145,7 @@ public function clear(string $prefix = '')
try {
return $this->doClear($namespaceToClear) || $cleared;
} 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;
}
Expand Down Expand Up @@ -194,7 +194,7 @@ public function deleteItems(array $keys)
} catch (\Exception $e) {
}
$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;
}

Expand Down Expand Up @@ -222,7 +222,7 @@ public function getItem($key)

return $f($key, $value, $isHit);
} 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);
Expand All @@ -244,7 +244,7 @@ public function getItems(array $keys = [])
try {
$items = $this->doFetch($ids);
} 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 = [];
}
$ids = array_combine($ids, $keys);
Expand Down Expand Up @@ -347,7 +347,7 @@ private function generateItems(iterable $items, array &$keys): iterable
yield $key => $f($key, $value, true);
}
} 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) {
Expand Down

0 comments on commit a85545f

Please sign in to comment.