Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

 

Running like clockwork: Better developer tools with the Clockwork module

A common request on the SilverStripe mailing list, forums, and user voice site over the...

Read post

A common request on the SilverStripe mailing list, forums, and user voice site over the years has been the desire for an improved developers toolbar, similar to what other frameworks such as Symfony have. These toolbars often make available extensive information about an http request - from the contents of $_SERVER, $_REQUEST, $_COOKIES, etc to logging and database queries. Many of these features are available with SilverStripe through url parameters such as ?showqueries=1 but are neither pretty nor easy to use.

Enter Clockwork

Our team at Adair Creative Group uses SilverStripe for 95% of the work we do. A few months ago I was working on a project using the popular Laravel framework when I came across a wonderful Chrome extension called Clockwork. It extends Chrome's built-in developer tools by adding a tab with information specific to php and the application framework including routing, execution timeline, database queries, logging - everything in fact that I'd seen in Symfony, Kohana, etc. It comes bundled with support for Laravel and Slim, and is easy enough to integrate with any framework.

clockwork module blog 

With just a few hours hacking and a some help from a few others in the SilverStripe community I was able to put together a SilverStripe module utilizing most of the features of Clockwork.

How to get started

The best way to install the module is with Composer, which insures that clockworks server side components and its dependencies. 

composer require markguinn/silverstripe-clockwork dev-master

You'll also need to install the Chrome extension And put your site in dev mode via _ss_environment.php or ?isDev=1. If your site is in live or test mode the extension will automatically disable itself. 

Open your site in Chrome with the developer tools enabled and the Clockwork tab visible. Every request will now log:

  • basic timing of framework initialization and controller routing
  • any calls to SS_log::log
  • all database queries, including query times
  • php request, cookie, and server information 

Advanced usage

Logging

Any use of SilverStripe's built in logging (with the exception of Debug::log) will go to Clockwork as well as whatever files or email writers you have configured. For example:

SS_Log::log("well, that happened...", SS_Log::INFO);

If you use Monolog instead there is a ClockworkWriter class that comes bundled but you'll need to set it up yourself manually or via the injector.

Adding custom timeline entries

Basic framework, routing and controller events are added to the execution timeline automatically and it's often useful to add your own events to keep an eye on how long certain sections of code are taking and find bottlenecks. This is easy to do:

Injector::inst()->get('ClockworkTimeline')->startEvent('myevent1', 'Description of the event');

// ... do the things ... //

// this will happen automatically when the request ends
Injector::inst()->get('ClockworkTimeline')->endEvent('myevent1');

Limitations

  • Only works with Chrome. There is an embeddable web app version and a Firebug plugin however.
  • Doesn't register failed requests
  • Isn't aware of site structure. I'd suggest using this module in concert with something like Better Navigator.

How to help

About the author
Mark Guinn

Mark Guinn is one of the senior developers at Adair Creative Group in Bentonville, AR (USA) and has been working primarily with SilverStripe since 2009. He is one of the maintainers of the SilverStripe Shop e-commerce module and its ecosystem of submodules.

Post your comment

Comments

  • Great timing for a large scale project! Installed and really loved it. Thank you so much for a well written write up. :)

    Posted by sktzoootech, 01/03/2015 11:14pm (10 years ago)

  • Great write up Mark! Installing now.

    Posted by Jeremy Shipman, 24/02/2015 1:13pm (10 years ago)

RSS feed for comments on this page | RSS feed for all comments