feat: integrate Monolog for enhanced logging across providers and controller
This commit is contained in:
@@ -39,18 +39,24 @@ class GraphController
|
||||
public function graph(Request $request): Response
|
||||
{
|
||||
if ($this->allowedHosts !== [] && !in_array($request->getHost(), $this->allowedHosts, true)) {
|
||||
$this->logger->warning('GraphController: rejected request from disallowed host', ['host' => $request->getHost()]);
|
||||
|
||||
return new Response('Forbidden', 403);
|
||||
}
|
||||
|
||||
$theme = $request->query->get('theme', 'dark');
|
||||
$cacheKey = 'graph_' . $theme;
|
||||
|
||||
$svg = $this->cache->get($cacheKey, function (ItemInterface $item) use ($theme): string {
|
||||
$cacheMiss = false;
|
||||
$svg = $this->cache->get($cacheKey, function (ItemInterface $item) use ($theme, &$cacheMiss): string {
|
||||
$cacheMiss = true;
|
||||
$item->expiresAfter(3600);
|
||||
|
||||
return $this->renderer->render($this->fetchAllContributions(), $theme);
|
||||
});
|
||||
|
||||
$this->logger->debug('GraphController: cache ' . ($cacheMiss ? 'miss' : 'hit'), ['theme' => $theme]);
|
||||
|
||||
return new Response($svg, 200, [
|
||||
'Content-Type' => 'image/svg+xml',
|
||||
'Cache-Control' => 'public, max-age=3600',
|
||||
@@ -86,7 +92,7 @@ class GraphController
|
||||
try {
|
||||
$contributions = $this->merge($contributions, $provider->fetch());
|
||||
} catch (\Throwable $e) {
|
||||
$this->logger->warning(sprintf('%s fetch failed: %s', $provider::class, $e->getMessage()));
|
||||
$this->logger->warning(sprintf('%s fetch failed: %s', $provider::class, $e->getMessage()), ['exception' => $e]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ namespace App\Service;
|
||||
|
||||
use IDCI\Bundle\GraphQLClientBundle\Client\GraphQLApiClient;
|
||||
use IDCI\Bundle\GraphQLClientBundle\Client\GraphQLApiClientRegistryInterface;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
||||
|
||||
/**
|
||||
@@ -20,6 +21,7 @@ class GitHubProvider implements ProviderInterface
|
||||
private readonly GraphQLApiClientRegistryInterface $registry,
|
||||
private readonly string $username,
|
||||
private readonly string $token,
|
||||
private readonly LoggerInterface $logger,
|
||||
) {}
|
||||
|
||||
public function isConfigured(): bool
|
||||
@@ -32,6 +34,8 @@ class GitHubProvider implements ProviderInterface
|
||||
*/
|
||||
public function fetch(): array
|
||||
{
|
||||
$this->logger->debug('GitHubProvider: fetching contributions', ['user' => $this->username]);
|
||||
|
||||
$from = (new \DateTimeImmutable('-365 days'))->format('Y-m-d\T00:00:00\Z');
|
||||
$to = (new \DateTimeImmutable())->format('Y-m-d\T23:59:59\Z');
|
||||
|
||||
@@ -79,6 +83,12 @@ class GitHubProvider implements ProviderInterface
|
||||
}
|
||||
}
|
||||
|
||||
$this->logger->info('GitHubProvider: fetched contributions', [
|
||||
'user' => $this->username,
|
||||
'days' => count($result),
|
||||
'total' => array_sum($result),
|
||||
]);
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
||||
|
||||
/**
|
||||
@@ -16,6 +17,7 @@ class GitLabProvider implements ProviderInterface
|
||||
private readonly HttpClientInterface $client,
|
||||
private readonly string $username,
|
||||
private readonly string $token,
|
||||
private readonly LoggerInterface $logger,
|
||||
private readonly string $baseUrl = '',
|
||||
) {}
|
||||
|
||||
@@ -31,6 +33,8 @@ class GitLabProvider implements ProviderInterface
|
||||
{
|
||||
$baseUrl = rtrim($this->baseUrl !== '' ? $this->baseUrl : 'https://gitlab.com', '/');
|
||||
|
||||
$this->logger->debug('GitLabProvider: fetching contributions', ['user' => $this->username, 'url' => $baseUrl]);
|
||||
|
||||
// Resolve numeric user ID from username
|
||||
$userResponse = $this->client->request('GET', "$baseUrl/api/v4/users", [
|
||||
'headers' => ['PRIVATE-TOKEN' => $this->token],
|
||||
@@ -67,6 +71,13 @@ class GitLabProvider implements ProviderInterface
|
||||
$page++;
|
||||
} while (count($events) === 100);
|
||||
|
||||
$this->logger->info('GitLabProvider: fetched contributions', [
|
||||
'user' => $this->username,
|
||||
'pages' => $page - 1,
|
||||
'days' => count($result),
|
||||
'total' => array_sum($result),
|
||||
]);
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
||||
|
||||
/**
|
||||
@@ -19,6 +20,7 @@ class GiteaProvider implements ProviderInterface
|
||||
private readonly string $username,
|
||||
private readonly string $token,
|
||||
private readonly string $baseUrl,
|
||||
private readonly LoggerInterface $logger,
|
||||
) {}
|
||||
|
||||
public function isConfigured(): bool
|
||||
@@ -32,6 +34,9 @@ class GiteaProvider implements ProviderInterface
|
||||
public function fetch(): array
|
||||
{
|
||||
$baseUrl = rtrim($this->baseUrl, '/');
|
||||
|
||||
$this->logger->debug('GiteaProvider: fetching contributions', ['user' => $this->username, 'url' => $baseUrl]);
|
||||
|
||||
$response = $this->client->request('GET', "$baseUrl/api/v1/users/{$this->username}/heatmap", [
|
||||
'headers' => ['Authorization' => "token {$this->token}"],
|
||||
]);
|
||||
@@ -48,6 +53,12 @@ class GiteaProvider implements ProviderInterface
|
||||
$result[$date] = ($result[$date] ?? 0) + (int) $entry['contributions'];
|
||||
}
|
||||
|
||||
$this->logger->info('GiteaProvider: fetched contributions', [
|
||||
'user' => $this->username,
|
||||
'days' => count($result),
|
||||
'total' => array_sum($result),
|
||||
]);
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user