Forked from bonny/simple-history-log-http-requests-time.php
Created
May 6, 2016 19:19
-
-
Save mgibbs189/1b1530fc0e5f263356f95b76c7fcdc9d to your computer and use it in GitHub Desktop.
Snippet for WordPress plugin Simple History Log that logs https request made and how long each request took
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* To use this you first need to install the free plugin Simple History | |
* https://wordpress.org/plugins/simple-history/ | |
*/ | |
// pre_http_request is fired close before the actual request | |
add_filter( 'pre_http_request', function( $retval, $r, $url ) { | |
$key = md5( $url ); | |
$GLOBALS["sh_http_log_{$key}"] = microtime(true); | |
return $retval; | |
}, 10, 3); | |
// http_api_debug is fired directly after the request | |
add_action( 'http_api_debug', function( $response, $type, $class, $args, $url ) { | |
$key = md5( $url ); | |
$globals_key = "sh_http_log_{$key}"; | |
if ( empty( $GLOBALS[$globals_key] ) ) { | |
return; | |
} | |
$time_taken = microtime(true) - $GLOBALS[$globals_key]; | |
$request_method = isset( $args["method"] ) ? $args["method"] : "Unknown"; | |
$context = [ | |
"url" => $url, | |
"time_taken" => $time_taken, | |
"request_method" => $request_method, | |
"_response" => SimpleHistory::json_encode($response), | |
"_type" => SimpleHistory::json_encode($type), | |
"_class" => SimpleHistory::json_encode($type), | |
"_args" => SimpleHistory::json_encode($args), | |
"_url" => SimpleHistory::json_encode($url) | |
]; | |
SimpleLogger()->debug( "http_api_debug: '{request_method}' request to '{url}' took {time_taken} seconds", $context ); | |
}, 10, 5); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment