A
A
Anton2015-12-14 11:53:42
Laravel
Anton, 2015-12-14 11:53:42

How to make Guzzle logging work in Laravel?

Hello everyone, there is such a situation - an application on Laravel 4.2, I put gasl 6.0 into it.
I tried to do it through Middleware, but nothing happens:

use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;
use GuzzleHttp\MessageFormatter;
use Monolog\Logger;

$stack = HandlerStack::create();
$stack->push(
    Middleware::log(
        new Logger('Logger'),
        new MessageFormatter('{req_body} - {res_body}')
    )
);
$client = new \GuzzleHttp\Client(
    [
        'base_uri' => 'some-url.com',
        'handler' => $stack,
    ]
);

What's my mistake?
PS I also tried using "rtheunissen/guzzle-log-middleware": "^0.4.0" the result is the same...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
D', 2015-12-14
@brud

If you need to write a Guzzle log file to the general log, then:

use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;
use GuzzleHttp\MessageFormatter;
use Monolog\Logger;

$stack = HandlerStack::create();
$stack->push(
    Middleware::log(
        \Illuminate\Support\Facades\Log::getMonolog(),
        new MessageFormatter('{req_body} - {res_body}')
    )
);
$client = new \GuzzleHttp\Client(
    [
        'base_uri' => 'some-url.com',
        'handler' => $stack,
    ]
);

It will write to the general log file.
If you need to write to a custom log file, then:
use Monolog\Handler\StreamHandler;
// ....
$logger = new Logger('GuzzleLogger');
$logger->pushHandler(new StreamHandler(storage_path('logs/guzzle.log')));
$stack->push(
    Middleware::log(
       $logger,
        new MessageFormatter('{req_body} - {res_body}')
    )
);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question