Skip to content

Commit 1a476f9

Browse files
committed
Deprecate Factory::withHttpLogger() and Factory::withHttpDebugLogger()
1 parent 2c64b14 commit 1a476f9

File tree

6 files changed

+77
-53
lines changed

6 files changed

+77
-53
lines changed

CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,18 @@ If it saves you or your team time, please consider [sponsoring its development](
55

66
## [Unreleased]
77

8+
### Changed
9+
10+
* `Factory::withHttpLogger()` and `Factory::withHttpDebugLogger()` have been deprecated. If you're using these methods,
11+
you can use `HttpClientOptions` instead.
12+
([Documentation](https://firebase-php.readthedocs.io/en/latest/setup.html#logging))
13+
14+
### Deprecated methods
15+
16+
* `Kreait\Firebase\Factory::withHttpLogger()`
17+
* `Kreait\Firebase\Factory::withHttpDebugLogger()`
18+
* `Kreait\Firebase\Http\Middleware::log()`
19+
820
## [7.24.0] - 2025-11-27
921

1022
### Changed

docs/setup.rst

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -222,44 +222,36 @@ You can also add middlewares to the Guzzle HTTP Client:
222222
You can find more information about Guzzle Middlewares at
223223
`Guzzle: Handlers and Middleware <https://docs.guzzlephp.org/en/stable/handlers-and-middleware.html>`_
224224

225-
*******
226225
Logging
227-
*******
226+
=======
228227

229228
In order to log API requests to the Firebase APIs, you can provide the factory with loggers
230229
implementing ``Psr\Log\LoggerInterface``.
231230

232-
The following examples use the `Monolog <https://github.com/Seldaek/monolog>`_ logger, but
231+
The following example uses the `Monolog <https://github.com/Seldaek/monolog>`_ logger, but
233232
work with any `PSR-3 log implementation <https://packagist.org/providers/psr/log-implementation>`_.
234233

235234
.. code-block:: php
236235
237236
use GuzzleHttp\MessageFormatter;
237+
use GuzzleHttp\Middleware as GuzzleMiddleware;
238238
use Kreait\Firebase\Factory;
239-
use Monolog\Logger;
239+
use Kreait\Firebase\Http\HttpClientOptions;
240240
use Monolog\Handler\StreamHandler;
241+
use Monolog\Logger;
242+
use Psr\Log\LogLevel;
241243
242244
$httpLogger = new Logger('firebase_http_logs');
243-
$httpLogger->pushHandler(new StreamHandler('path/to/firebase_api.log', Logger::INFO));
245+
$httpLogger->pushHandler(new StreamHandler('path/to/firebase_api.log'));
244246
245-
// Without further arguments, requests and responses will be logged with basic
246-
// request and response information. Successful responses will be logged with
247-
// the 'info' log level, failures (Status code >= 400) with 'notice'
248-
$factory = $factory->withHttpLogger($httpLogger);
249-
250-
// You can configure the message format and log levels individually
251-
$messageFormatter = new MessageFormatter(MessageFormatter::SHORT);
252-
$factory = $factory->withHttpLogger(
253-
$httpLogger, $messageFormatter, $successes = 'debug', $errors = 'warning'
254-
);
255-
256-
// You can provide a separate logger for detailed HTTP message logs
257-
$httpDebugLogger = new Logger('firebase_http_debug_logs');
258-
$httpDebugLogger->pushHandler(
259-
new StreamHandler('path/to/firebase_api_debug.log',
260-
Logger::DEBUG)
247+
$logMiddleware = GuzzleMiddleware::log(
248+
logger: $httpLogger,
249+
formatter: new MessageFormatter(),
250+
logLevel: LogLevel::INFO,
261251
);
262252
263-
// Logs will include the full request and response headers and bodies
264-
$factory = $factory->withHttpDebugLogger($httpDebugLogger)
253+
$options = $options->withGuzzleMiddleware($logMiddleware, 'http_debug_logs');
265254
255+
$factory = (new Factory())
256+
->withHttpClientOptions($clientOptions)
257+
;

src/Firebase/Factory.php

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -100,16 +100,6 @@ final class Factory
100100

101101
private ClockInterface $clock;
102102

103-
/**
104-
* @var callable|null
105-
*/
106-
private $httpLogMiddleware;
107-
108-
/**
109-
* @var callable|null
110-
*/
111-
private $httpDebugLogMiddleware;
112-
113103
/**
114104
* @var callable|null
115105
*/
@@ -317,33 +307,55 @@ public function withHttpClientOptions(HttpClientOptions $options): self
317307
}
318308

319309
/**
310+
* @deprecated 7.25.0 Create the log middleware outside the factory and use `HttpClientOptions::withGuzzleMiddleware()` and `withClientOptions()` instead
311+
*
312+
* @see withHttpClientOptions()
313+
* @see HttpClientOptions::withGuzzleMiddleware()
314+
*
320315
* @param non-empty-string|null $logLevel
321316
* @param non-empty-string|null $errorLogLevel
322317
*/
323318
public function withHttpLogger(LoggerInterface $logger, ?MessageFormatter $formatter = null, ?string $logLevel = null, ?string $errorLogLevel = null): self
324319
{
325-
$formatter ??= new MessageFormatter();
326-
$logLevel ??= LogLevel::INFO;
327-
$errorLogLevel ??= LogLevel::NOTICE;
320+
$clientOptions = $this->httpClientOptions->withGuzzleMiddleware(
321+
middleware: Middleware::log(
322+
$logger,
323+
$formatter ?? new MessageFormatter(),
324+
$logLevel ?? LogLevel::INFO,
325+
$errorLogLevel ?? LogLevel::NOTICE,
326+
),
327+
name: 'http_logs'
328+
);
328329

329330
$factory = clone $this;
330-
$factory->httpLogMiddleware = Middleware::log($logger, $formatter, $logLevel, $errorLogLevel);
331+
$factory->httpClientOptions = $clientOptions;
331332

332333
return $factory;
333334
}
334335

335336
/**
337+
* @deprecated 7.25.0 Create the log middleware outside the factory and use `HttpClientOptions::withGuzzleMiddleware()` and `withClientOptions()` instead
338+
*
339+
* @see withHttpClientOptions()
340+
* @see HttpClientOptions::withGuzzleMiddleware()
341+
*
336342
* @param non-empty-string|null $logLevel
337343
* @param non-empty-string|null $errorLogLevel
338344
*/
339345
public function withHttpDebugLogger(LoggerInterface $logger, ?MessageFormatter $formatter = null, ?string $logLevel = null, ?string $errorLogLevel = null): self
340346
{
341-
$formatter ??= new MessageFormatter(MessageFormatter::DEBUG);
342-
$logLevel ??= LogLevel::INFO;
343-
$errorLogLevel ??= LogLevel::NOTICE;
347+
$clientOptions = $this->httpClientOptions->withGuzzleMiddleware(
348+
middleware: Middleware::log(
349+
$logger,
350+
$formatter ?? new MessageFormatter(MessageFormatter::DEBUG),
351+
$logLevel ?? LogLevel::INFO,
352+
$errorLogLevel ?? LogLevel::NOTICE,
353+
),
354+
name: 'http_debug_logs'
355+
);
344356

345357
$factory = clone $this;
346-
$factory->httpDebugLogMiddleware = Middleware::log($logger, $formatter, $logLevel, $errorLogLevel);
358+
$factory->httpClientOptions = $clientOptions;
347359

348360
return $factory;
349361
}
@@ -587,14 +599,6 @@ public function createApiClient(?array $config = null, ?array $middlewares = nul
587599

588600
$handler = HandlerStack::create($config['handler'] ?? null);
589601

590-
if ($this->httpLogMiddleware !== null) {
591-
$handler->push($this->httpLogMiddleware, 'http_logs');
592-
}
593-
594-
if ($this->httpDebugLogMiddleware !== null) {
595-
$handler->push($this->httpDebugLogMiddleware, 'http_debug_logs');
596-
}
597-
598602
foreach ($this->httpClientOptions->guzzleMiddlewares() as $middleware) {
599603
$handler->push($middleware['middleware'], $middleware['name']);
600604
}

src/Firebase/Http/Middleware.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,13 @@ public static function addDatabaseAuthVariableOverride(?array $override): callab
6565
}
6666

6767
/**
68+
* @deprecated 7.25.0 Use the Log Middleware provided by the GuzzleHTTP library instead
69+
*
6870
* @return callable(callable): Closure
6971
*/
7072
public static function log(LoggerInterface $logger, MessageFormatter $formatter, string $logLevel, string $errorLogLevel): callable
7173
{
72-
return static fn(callable $handler): Closure => static fn($request, array $options) => $handler($request, $options)->then(
74+
return static fn(callable $handler): Closure => static fn(RequestInterface $request, array $options) => $handler($request, $options)->then(
7375
static function (ResponseInterface $response) use ($logger, $request, $formatter, $logLevel, $errorLogLevel): ResponseInterface {
7476
$message = $formatter->format($request, $response);
7577
$messageLogLevel = $response->getStatusCode() >= StatusCode::STATUS_BAD_REQUEST ? $errorLogLevel : $logLevel;

tests/Integration/Http/HttpClientOptionsTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
/**
1515
* @internal
1616
*/
17-
class HttpClientOptionsTest extends IntegrationTestCase
17+
final class HttpClientOptionsTest extends IntegrationTestCase
1818
{
1919
#[Test]
2020
public function itWorksWithAFunctionMiddleware(): void

tests/Integration/HttpLoggingTest.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,16 @@
44

55
namespace Kreait\Firebase\Tests\Integration;
66

7+
use GuzzleHttp\MessageFormatter;
8+
use GuzzleHttp\Middleware;
79
use Kreait\Firebase\Contract\Auth;
810
use Kreait\Firebase\Exception\Auth\UserNotFound;
11+
use Kreait\Firebase\Http\HttpClientOptions;
912
use Kreait\Firebase\Tests\IntegrationTestCase;
1013
use PHPUnit\Framework\Attributes\Test;
1114
use PHPUnit\Framework\MockObject\MockObject;
1215
use Psr\Log\LoggerInterface;
16+
use Psr\Log\LogLevel;
1317
use Throwable;
1418

1519
/**
@@ -34,9 +38,19 @@ protected function setUp(): void
3438
$this->logger = $this->createMock(LoggerInterface::class);
3539
$this->debugLogger = $this->createMock(LoggerInterface::class);
3640

41+
$logMiddleware = Middleware::log($this->logger, new MessageFormatter(), LogLevel::INFO);
42+
$debugLogMiddleware = Middleware::log($this->debugLogger, new MessageFormatter(MessageFormatter::DEBUG), LogLevel::DEBUG);
43+
44+
$clientOptions = HttpClientOptions::default();
45+
3746
$this->auth = self::$factory->createAuth();
38-
$this->authWithLogger = self::$factory->withHttpLogger($this->logger)->createAuth();
39-
$this->authWithDebugLogger = self::$factory->withHttpDebugLogger($this->debugLogger)->createAuth();
47+
$this->authWithLogger = self::$factory
48+
->withHttpClientOptions($clientOptions->withGuzzleMiddleware($logMiddleware))
49+
->createAuth();
50+
51+
$this->authWithDebugLogger = self::$factory
52+
->withHttpClientOptions($clientOptions->withGuzzleMiddleware($debugLogMiddleware))
53+
->createAuth();
4054
}
4155

4256
#[Test]

0 commit comments

Comments
 (0)