PHP code example of vbukreyev / guzzle-log-middleware
1. Go to this page and download the library: Download vbukreyev/guzzle-log-middleware library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
vbukreyev / guzzle-log-middleware example snippets
$logger = new myLogger(); // should implements Psr\Log\LoggerInterface
$middleware = new Logger($logger);
$middleware->setLogLevel(LogLevel::DEBUG);
$middleware->setFormatter(new \GuzzleHttp\MessageFormatter(myLogger::MY_FORMAT));
$middleware->setRequestLoggingEnabled(false);
$stack = \GuzzleHttp\HandlerStack::create();
$stack->push($middleware->logHandler());
$client = new GuzzleHttp\Client([
'handler' => $stack,
]);
###
class myLogger extends \Psr\Log\AbstractLogger {
const MY_FORMAT = "{hostname} {req_header_User-Agent} - [{date_common_log}] \"{method} {target} HTTP/{version}\" {code} {res_header_Content-Length} :: {req_body}";
public function log($level, $message, array $context = array()) {
file_put_contents('/tmp/my-logger.log', $message . PHP_EOL, FILE_APPEND);
}
}
$middleware = new Logger(function ($level, $message, array $context) {
// Log the message
});
use Psr\Log\LogLevel;
$middleware->setLogLevel(LogLevel::DEBUG);