Skip to content

Commit c05def9

Browse files
committed
Merge pull request #33 from Nyholm/logging
Logging
2 parents 8aa3ba3 + df22354 commit c05def9

File tree

13 files changed

+409
-187
lines changed

13 files changed

+409
-187
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,19 @@ dynamic routes. If you just have a few routes your performance will actually be
127127
Use [Blackfire](https://blackfire.io/) to profile your application to see if you should enable routing cache or not.
128128

129129

130+
#### Logging
131+
132+
If you want to log all the interaction with the cache you may do so with the following configuration.
133+
134+
```yml
135+
cache:
136+
logging:
137+
enabled: true
138+
logger: 'logger' # Default service id to use for logging
139+
level: 'info' # Default logging level
140+
```
141+
142+
130143
### Clearing the cache
131144

132145
If you want to clear the cache you can run the following commands.

src/Cache/LoggingCachePool.php

Lines changed: 55 additions & 136 deletions
Original file line numberDiff line numberDiff line change
@@ -11,173 +11,92 @@
1111

1212
namespace Cache\CacheBundle\Cache;
1313

14-
use Cache\Taggable\TaggablePoolInterface;
15-
use Psr\Cache\CacheItemInterface;
16-
use Psr\Cache\CacheItemPoolInterface;
14+
use Monolog\Logger;
15+
use Psr\Log\LoggerInterface;
1716

1817
/**
19-
* @author Aaron Scherer <aequasi@gmail.com>
18+
* Logg all calls to the cache.
19+
*
20+
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
2021
*/
21-
class LoggingCachePool implements CacheItemPoolInterface, TaggablePoolInterface
22+
class LoggingCachePool extends RecordingCachePool
2223
{
2324
/**
24-
* @type array
25+
* @type Logger
2526
*/
26-
private $calls = [];
27+
private $logger;
2728

2829
/**
29-
* @type CacheItemPoolInterface
30+
* @type string
3031
*/
31-
private $cachePool;
32+
private $name;
3233

3334
/**
34-
* LoggingCachePool constructor.
35-
*
36-
* @param CacheItemPoolInterface $cachePool
35+
* @type string
3736
*/
38-
public function __construct(CacheItemPoolInterface $cachePool)
39-
{
40-
$this->cachePool = $cachePool;
41-
}
37+
private $level = 'info';
4238

4339
/**
44-
* @param string $name
45-
* @param array $arguments
40+
* @param LoggerInterface $logger
4641
*
47-
* @return object
42+
* @return LoggingCachePool
4843
*/
49-
private function timeCall($name, array $arguments = null)
50-
{
51-
$start = microtime(true);
52-
$result = call_user_func_array([$this->cachePool, $name], $arguments);
53-
$time = microtime(true) - $start;
54-
55-
$object = (object) compact('name', 'arguments', 'start', 'time', 'result');
56-
57-
return $object;
58-
}
59-
60-
public function getItem($key, array $tags = [])
61-
{
62-
$call = $this->timeCall(__FUNCTION__, [$key, $tags]);
63-
$result = $call->result;
64-
$call->isHit = $result->isHit();
65-
66-
// Display the result in a good way depending on the data type
67-
if ($call->isHit) {
68-
$call->result = $this->getValueRepresentation($result->get());
69-
} else {
70-
$call->result = null;
71-
}
72-
73-
$this->calls[] = $call;
74-
75-
return $result;
76-
}
77-
78-
public function hasItem($key, array $tags = [])
79-
{
80-
$call = $this->timeCall(__FUNCTION__, [$key, $tags]);
81-
$this->calls[] = $call;
82-
83-
return $call->result;
84-
}
85-
86-
public function deleteItem($key, array $tags = [])
87-
{
88-
$call = $this->timeCall(__FUNCTION__, [$key, $tags]);
89-
$this->calls[] = $call;
90-
91-
return $call->result;
92-
}
93-
94-
public function save(CacheItemInterface $item)
95-
{
96-
$key = $item->getKey();
97-
$value = $this->getValueRepresentation($item->get());
98-
99-
$call = $this->timeCall(__FUNCTION__, [$item]);
100-
$call->arguments = ['<CacheItem>', $key, $value];
101-
$this->calls[] = $call;
102-
103-
return $call->result;
104-
}
105-
106-
public function getItems(array $keys = [], array $tags = [])
107-
{
108-
$call = $this->timeCall(__FUNCTION__, [$keys, $tags]);
109-
$result = $call->result;
110-
$call->result = sprintf('<DATA:%s>', gettype($result));
111-
112-
$this->calls[] = $call;
113-
114-
return $result;
115-
}
116-
117-
public function clear(array $tags = [])
118-
{
119-
$call = $this->timeCall(__FUNCTION__, [$tags]);
120-
$this->calls[] = $call;
121-
122-
return $call->result;
123-
}
124-
125-
public function deleteItems(array $keys, array $tags = [])
126-
{
127-
$call = $this->timeCall(__FUNCTION__, [$keys, $tags]);
128-
$this->calls[] = $call;
129-
130-
return $call->result;
131-
}
132-
133-
public function saveDeferred(CacheItemInterface $item)
44+
public function setLogger($logger)
13445
{
135-
$itemClone = clone $item;
136-
$itemClone->set(sprintf('<DATA:%s', gettype($item->get())));
137-
138-
$call = $this->timeCall(__FUNCTION__, [$item]);
139-
$call->arguments = ['<CacheItem>', $itemClone];
140-
$this->calls[] = $call;
46+
$this->logger = $logger;
14147

142-
return $call->result;
48+
return $this;
14349
}
14450

145-
public function commit()
51+
/**
52+
* @param string $name
53+
*
54+
* @return LoggingCachePool
55+
*/
56+
public function setName($name)
14657
{
147-
$call = $this->timeCall(__FUNCTION__);
148-
$this->calls[] = $call;
58+
$this->name = $name;
14959

150-
return $call->result;
60+
return $this;
15161
}
15262

15363
/**
154-
* @return array
64+
* @param string $level
65+
*
66+
* @return LoggingCachePool
15567
*/
156-
public function getCalls()
68+
public function setLevel($level)
15769
{
158-
return $this->calls;
70+
$this->level = $level;
71+
72+
return $this;
15973
}
16074

16175
/**
162-
* Get a string to represent the value.
163-
*
164-
* @param mixed $value
165-
*
166-
* @return string
76+
* @param $call
16777
*/
168-
private function getValueRepresentation($value)
78+
protected function addCall($call)
16979
{
170-
$type = gettype($value);
171-
if (in_array($type, ['boolean', 'integer', 'double', 'string', 'NULL'])) {
172-
$rep = $value;
173-
} elseif ($type === 'array') {
174-
$rep = json_encode($value);
175-
} elseif ($type === 'object') {
176-
$rep = get_class($value);
177-
} else {
178-
$rep = sprintf('<DATA:%s>', $type);
179-
}
180-
181-
return $rep;
80+
$data = [
81+
'name' => $this->name,
82+
'method' => $call->name,
83+
'arguments' => json_encode($call->arguments),
84+
'hit' => isset($call->isHit) ? $call->isHit ? 'True' : 'False' : 'Invalid',
85+
'time' => round($call->time * 1000, 2),
86+
'result' => $call->result,
87+
];
88+
89+
$this->logger->log(
90+
$this->level,
91+
sprintf('[Cache] Provider: %s. Method: %s(%s). Hit: %s. Time: %sms. Result: %s',
92+
$data['name'],
93+
$data['method'],
94+
$data['arguments'],
95+
$data['hit'],
96+
$data['time'],
97+
$data['result']
98+
),
99+
$data
100+
);
182101
}
183102
}

0 commit comments

Comments
 (0)