Skip to content

Commit 25e963e

Browse files
authored
Merge pull request #18 from crowdsecurity/improve-logging
improve logging
2 parents ba3d4da + 80ab92c commit 25e963e

File tree

3 files changed

+49
-22
lines changed

3 files changed

+49
-22
lines changed

docs/contribute.md

+6-4
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,16 @@ We use the git workflow [Github Flow](https://guides.github.com/introduction/flo
6262
#### New feature
6363

6464
```bash
65-
git checkout -b feature/<the-feature>
66-
git commit # as mush as necessary
67-
git push origin feature/<the-feature>
65+
git checkout -b new-feature # the name is not important now.
66+
git commit # as mush as necessary.
67+
git branch -m <name-of-the-branch> # to rename the branch to what has really be done
68+
git push -u origin <name-of-the-branch>
6869
gh pr create --fill
6970
```
7071

7172
#### New release
7273

7374
```bash
74-
gh release create --draft vx.x.x
75+
git describe --tags # to verify what is the current tag
76+
gh release create --draft vx.x.x --title vx.x.x
7577
```

src/ApiCache.php

+26-18
Original file line numberDiff line numberDiff line change
@@ -239,13 +239,7 @@ private function saveRemediations(array $decisions): bool
239239
}
240240
}
241241
}
242-
243-
$warmedUp = $this->adapter->commit();
244-
245-
// Store the fact that the cache has been warmed up.
246-
$this->defferUpdateCacheConfig(['warmed_up' => $warmedUp]);
247-
248-
return $warmedUp;
242+
return $this->adapter->commit();
249243
}
250244

251245
private function removeRemediations(array $decisions): bool
@@ -263,7 +257,7 @@ private function removeRemediations(array $decisions): bool
263257
if (!$success) {
264258
// The API may return stale deletion events due to API design.
265259
// Ignoring them is therefore not a problem.
266-
$this->logger->debug("Decision " . $decision['id'] . " not found in cache for one or more items.");
260+
$this->logger->debug("Decision " . $decision['id'] . " not found in cache for one or more items");
267261
}
268262
}
269263
}
@@ -297,7 +291,10 @@ private function saveRemediationsForIp(array $decisions, string $ip): string
297291

298292
public function clear(): bool
299293
{
300-
return $this->adapter->clear();
294+
$cleared = $this->adapter->clear();
295+
$this->warmedUp = false;
296+
$this->logger->info("Cache cleared");
297+
return $cleared;
301298
}
302299

303300
/**
@@ -306,20 +303,27 @@ public function clear(): bool
306303
* Used when the stream mode has just been activated.
307304
*
308305
*/
309-
private function warmUp(): void
306+
public function warmUp(): void
310307
{
311-
$this->logger->info('Warming the cache up');
308+
$this->logger->debug('Warming the cache up');
312309
$startup = true;
313310
$decisionsDiff = $this->apiClient->getStreamedDecisions($startup);
314311
$newDecisions = $decisionsDiff['new'];
315312

313+
$nbNew = 0;
316314
if ($newDecisions) {
317315
$this->warmedUp = $this->saveRemediations($newDecisions);
318316
if (!$this->warmedUp) {
319317
throw new BouncerException("Unable to warm the cache up");
320318
}
319+
$nbNew = count($newDecisions);
321320
}
322-
$this->logger->debug('Cache warmed up');
321+
322+
// Store the fact that the cache has been warmed up.
323+
$this->logger->info("Cache warmed up with $nbNew decision(s)");
324+
$this->defferUpdateCacheConfig(['warmed_up' => true]);
325+
$this->adapter->commit();
326+
323327
}
324328

325329
/**
@@ -331,26 +335,30 @@ private function warmUp(): void
331335
*/
332336
public function pullUpdates(): void
333337
{
334-
$this->logger->info('Pulling updates from API');
335338
if (!$this->warmedUp) {
336339
$this->warmUp();
340+
return;
337341
}
338342

343+
$this->logger->debug('Pulling updates from API');
339344
$decisionsDiff = $this->apiClient->getStreamedDecisions();
340345
$newDecisions = $decisionsDiff['new'];
341346
$deletedDecisions = $decisionsDiff['deleted'];
342347

348+
$nbDeleted = 0;
343349
if ($deletedDecisions) {
344350
$this->removeRemediations($deletedDecisions);
351+
$nbDeleted = count($deletedDecisions);
345352
}
346-
353+
354+
$nbNew = 0;
347355
if ($newDecisions) {
348356
$this->saveRemediations($newDecisions);
349-
if (!$this->warmedUp) {
350-
throw new BouncerException("Unable to warm the cache up");
351-
}
357+
$nbNew = count($newDecisions);
352358
}
353-
$this->logger->debug('Updates pulled from API');
359+
360+
361+
$this->logger->info("Updates pulled from API (-$nbDeleted+$nbNew)");
354362
}
355363

356364
/**

src/Bouncer.php

+17
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,15 @@ public function getDefault403Template(): string
124124
'Please contact our technical support if you think it is an error.</p></body></html>';
125125
}
126126

127+
/**
128+
* Used in stream mode only.
129+
* This method should be called only to force a cache warm up.
130+
*/
131+
public function warmBlocklistCacheUp(): void
132+
{
133+
$this->apiCache->warmUp();
134+
}
135+
127136
/**
128137
* Used in stream mode only.
129138
* This method should be called periodically (ex: crontab) in a asynchronous way to update the bouncer cache.
@@ -149,6 +158,14 @@ public function pruneCache(): bool
149158
return $this->apiCache->clear();
150159
}
151160

161+
/**
162+
* Returns the logger instance.
163+
*/
164+
public function getLogger(): LoggerInterface
165+
{
166+
return $this->logger;
167+
}
168+
152169
/**
153170
* Browse the remediations cache.
154171
*/

0 commit comments

Comments
 (0)