diff --git a/classes/Log.php b/classes/Log.php index 10a0a3a..5245379 100644 --- a/classes/Log.php +++ b/classes/Log.php @@ -42,8 +42,6 @@ public function logFile() /** * Logs the token callback. * - * @param object $logger The logFile() method object. - * * @return Monolog\Logger Log cache key receiving new token. */ public function getTokenCallback() @@ -57,4 +55,44 @@ public function getTokenCallback() return $callback; } + + /** + * Log headers to file. + * + * @return void + */ + public function logHeaders() + { + $myFile = "logs/headers.log"; + $date = date('Y-m-d H:i:s'); + $fh = fopen($myFile, 'a') or die("can't open file"); + fwrite($fh, "\n----------------------------------------------------\n"); + fwrite($fh, $date); + fwrite($fh, "\n"); + foreach ($_SERVER as $h => $v) { + if (preg_match('/HTTP_(.+)/', $h, $hp)) { + fwrite($fh, "$h = $v\n"); + } + } + fwrite($fh, "\r\n"); + fwrite($fh, file_get_contents('php://input')); + fclose($fh); + } + + /** + * Log request body. + * + * @return void + */ + public function logRequest() + { + $myFile = "logs/request.log"; + $date = date('Y-m-d H:i:s'); + $fh = fopen($myFile, 'a') or die("can't open file"); + fwrite($fh, "\n----------------------------------------------------\n"); + fwrite($fh, $date); + fwrite($fh, "\n"); + fwrite($fh, file_get_contents('php://input')); + fclose($fh); + } } diff --git a/index.php b/index.php index 67f92ee..514ac5b 100644 --- a/index.php +++ b/index.php @@ -51,10 +51,6 @@ function payload() $json = file_get_contents('php://input'); $data = json_decode($json, true); - $logger = (new Log)->logFile(); - $logdata = json_encode(json_decode($json)); - $logger->info($logdata); - return $data; } @@ -80,26 +76,6 @@ function dbInsert($event, $itemName, $itemUrl, $created) return $insert; } -/** - * Log headers to file. - * - * @return void - */ -function logHeaders() -{ - $myFile = "./logs/request_log_file.log"; - $fh = fopen($myFile, 'a') or die("can't open file"); - fwrite($fh, "\n\n------------------------------------------------------\n"); - foreach ($_SERVER as $h => $v) - if (preg_match('/HTTP_(.+)/', $h, $hp)) - fwrite($fh, "$h = $v\n"); - fwrite($fh, "\r\n"); - fwrite($fh, file_get_contents('php://input')); - fclose($fh); -} -// Log headers to file. -logHeaders(); - // Setup the log file. $log = new Log; $logger = $log->logFile(); @@ -142,6 +118,14 @@ function logHeaders() $payloadUrl = $payload['payload']['item_url']; $payloadDate = $payload['payload']['created_at']; +/** + * Log raw headers to file. + * + * @todo Uncomment the catch Exception below. Remove this. + */ +$log->logHeaders(); +$log->logRequest(); + try { dbInsert($payloadEvent, $payloadName, $payloadUrl, $payloadDate); $logger->info( @@ -153,6 +137,11 @@ function logHeaders() ) ); } catch (Exception $e) { + // Log raw headers to file. + // $log->logHeaders(); + // $log->logRequest(); + + // Log error to Monolog. $logger->error( sprintf( 'Index dbInsert error code %s. Error: %s',