2

I've been adapting the very useful geo plugin to fetch currency and exchange rates based on a visitors IP address. [ see: https://github.com/lukeholder/craft-geo ]

The plugin uses Craft's vendor package "guzzle" to contact 3rd party APIs and request data which then gets cached in JSON form.

When its not cached i was wondering how can you tell if Guzzle is making the request - there are no headers in dev tools and no noticeable traffic for the requests. So how can you see if the Guzzle has guzzled when cache is off?

1

As Brad says, server side requests won't show up in your browser's dev tools. There's a few ways you can debug the calls:

First, if you simply want to know if the Guzzle request ran and completed successfully, the easiest option is to use Craft's built-in logging feature by calling MyPlugin::log (obviously, replace "MyPlugin" with the real handle for your plugin):

MyPlugin::log('For those about to guzzle.');

try {
    $response = $client->send($request);
    MyPlugin::log('I guzzled!');
} catch (\Exception $e) {
    MyPlugin::log('Omg guzzling failed: '.$e->getMessage(), LogLevel::Error);
}

MyPlugin::log will write whatever you feed it to a file called /craft/storage/runtime/logs/myplugin.log. On a Unix-based system (such as OS X) you can monitor this file for changes by writing $ tail -f myplugin.log in your Terminal.

Obviously, the log function will only accept string values. If you need to debug an object – e.g. the Guzzle response – you can simply JSON encode it:

MyPlugin::log('I guzzled, and all I got was '.json_encode($response));

For large/complex objects, this isn't really feasible, though – the log file will quickly become too large and/or unreadable. Beyond JSON, there's a few other options for object logging here.

As an alternative (or in addition) to Craft's built-in logging, you may want to look into remote logging tools as well. There's several, though I'm personally a big fan of Rollbar. Rollbar is designed to catch exceptions, but it also has a logging feature similar to Craft's – the difference being that the message isn't output to a local file, but sent off to your Rollbar account (and, optionally, to wherever you want – your email, a private Slack channel, whatever). There's even a Craft plugin.

Beyond simple logging: For real object inspection, you may want to look into XDebug, a debugging extension for PHP. By configuring an IDE like PHPStorm with XDebug, you'll be able to set breakpoints in your code, and debug/inspect variables at runtime.

Another option is to use a tool like Chrome Logger, which will let you log strings and objects to Chrome's console (basically just like you'd use console.log in JavaScript).

Edit I just realized that you're not actually writing your own plugin, which makes the above answer less useful.

|improve this answer|||||
  • Mats that is totally useful. I've adapted the GEo plugin to extra guzzle and do some more data merging. The data object is pretty small so i'll give craft logging a go first before. I'm using PhpStorm so adding xdebug will be the next port of call when and if i start getting more complexity – joomkit Jul 7 '16 at 10:36
  • @joomkit Awesome! – Mats Mikkel Rummelhoff Jul 7 '16 at 17:41
2

Update:

Turns out server side initiated remote calls won't be logged in your browser's Network tab (only client-side ones). Which now that I type that out, seems very obvious.


If Guzzle actually makes a request, then it will show up in your browser's inspector's "Network" tab.

|improve this answer|||||
  • Well its definitely making requests as the plugin is getting data back and caching it. But i never see a request in the network tab. I wonder if you need to explicitly set something in guzzle because i don't see anything. I wanted to use it to check that requested data was cached properly and not requesting on every page. Its working as expected in this respect but I just don't see the first before-cache requests. .... – joomkit Jul 5 '16 at 8:17
  • Updated answer. – Brad Bell Jul 5 '16 at 21:32
  • FWIW, I actually xDebugged this while testing and caching looks like it's working as you'd expect. – Brad Bell Jul 6 '16 at 22:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.