I like to logging request and response in my project.
I can able to log the response details.
But I need to log the details of the request.
How to do that?
I am using the following code.
<?php
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
use Monolog\Formatter\LineFormatter;
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
use Interop\Container\ContainerInterface;
require 'vendor/autoload.php';
$config['displayErrorDetails'] = true;
$config['addContentLengthHeader'] = false;
$config['db']['host'] = "localhost";
$config['db']['user'] = "user";
$config['db']['pass'] = "user";
$config['db']['dbname'] = "slim";
$app = new \Slim\App(["settings" => $config]);
$app->get('/', function (Request $req, Response $res, $args = []) {
$dateFormat = "y-m-d H:i:s";
$output = "[%datetime%] %level_name% : %message%\n";
$formatter = new LineFormatter($output, $dateFormat);
$stream = new StreamHandler("logfiles/".date('m-d')."_file.log", Logger::INFO);
$stream->setFormatter($formatter);
$log = new Logger('Request');
$log->pushHandler($stream);
$action = $req->getQueryParams()['action']; //checks _GET [IS PSR-7 compliant]
foreach($req->getQueryParams() as $params)
{
$log->info($params);
}
switch($action)
{
case "ListEvents":
$userid = $req->getQueryParams()['userid'];
$details = array('key'=>'Test', 'value'=>'Success', 'userid'=>$userid);
$res = $res->withJSON($details);
$log->info($res);
return $res;
break;
}
});
// Run app
$app->run();
?>
I am getting following result in log
17-02-14 16:13:08] INFO : ListEvents
[17-02-14 16:13:08] INFO : 90
[17-02-14 16:13:08] INFO : HTTP/1.1 200 OK Content-Type: application/json;charset=utf-8 {“key”:“Test”,“value”:“Success”,“userid”:“90”}
But I need request log as
Request: “action=ListEvents&userid=90”
Response: {“action”:“ListEvents”, “key”:“Test”, “value”:“Success”, “userid”:“90”}
How to do that? Any ideas?