Skip to content

Instantly share code, notes, and snippets.

@Piskvor
Last active September 12, 2018 10:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Piskvor/fd9608a644b96b4812bc73a9cab6d779 to your computer and use it in GitHub Desktop.
Save Piskvor/fd9608a644b96b4812bc73a9cab6d779 to your computer and use it in GitHub Desktop.
Simple logger for requests
<?php
declare(strict_types=1);
/*
Usage:
start PHP devel server in this script's directory:
php -S [::]:8888
send requests to http://your-ip-address:8888/
JSON events are logged to event.log
JSON request bodies are logged to timestamped files
*/
$entityBody = file_get_contents('php://input');
$data = [
'b' => $entityBody,
'g' => $_GET,
'p' => $_POST,
's' => $_SERVER
];
$dump = var_export($data, true);
file_put_contents(__DIR__.'/'.time().'.json', json_encode($data),FILE_APPEND);
if ($entityBody) {
file_put_contents(__DIR__.'/'.time().'-body.json', $entityBody, FILE_APPEND); // we expect application/json here
$json = @json_decode($entityBody, true);
if (!empty($json)) {
$log = date('Y-m-d H:i:s') . ' - ' . $_SERVER['HTTP_HOST'] . ' ' . $json['entity']
. ' ' . $json['eventName'] . ' ' . $json['entityId'] . ' ' . $json['changeType']
. ' uuid: ' . $json['uuid'] ."\n";
@file_put_contents(__DIR__.'/event.log', $log, FILE_APPEND);
}
}
@file_put_contents(__DIR__.'/log.html', $dump, FILE_APPEND);
echo $dump;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment