Skip to content
This repository has been archived by the owner. It is now read-only.

Commit

Permalink
Added log for headers and request in log class.
Browse files Browse the repository at this point in the history
  • Loading branch information
justinhartman committed Mar 31, 2020
1 parent d3b8063 commit 0d1bf74
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 26 deletions.
42 changes: 40 additions & 2 deletions classes/Log.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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);
}
}
37 changes: 13 additions & 24 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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();
Expand Down Expand Up @@ -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(
Expand All @@ -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',
Expand Down

0 comments on commit 0d1bf74

Please sign in to comment.