LogoPhly, boy, phly
the weblog and site of Matthew Weier O'Phinney

Thursday, December 10. 2009

Real-time ZF Monitoring via Zend Server

When keeping tabs on your ZF applications, it's often difficult to separate application errors from general PHP errors, and if you aggregate them in the same location as your web server errors, this can become more difficult still.

Additionally, PHP's error reporting doesn't provide a ton of context, even when reporting uncaught exceptions -- typically you'll only get a cryptic exception message, and what file and line emitted it.

Zend Server's Monitor extension has some capabilities for providing more context, and does much of this by default: request and environment settings available when the error was logged, the function name and arguments provided, and a full backtrace are available for you to inspect. Additionally, the Monitor extension includes an API that allows you to trigger custom Monitor events, and you can provide additional context when doing so -- such as passing objects or arrays that may help provide context when debugging.

To tie into this feature, we've developed a new Zend_Log writer, Zend_Log_Writer_ZendMonitor, that will emit such custom events. In addition, some modifications were made to the Zend_Log API to allow passing extra contextual information to log writers.

As an example, you could do the following:


$log = new Zend_Log(new Zend_Log_Writer_ZendMonitor());
$log->crit('Exception occurred processing login', $e);

// or:
$log->crit('Exception occurred processing login', array(
    'request'   => $request, // Request object
    'exception' => $e,       // Exception
));
 

Zend Server's GUI would then present a tab, "Custom", that includes the extra arguments passed; by default, if just an object is passed, the information will be returned under the key "info". Passing an associative array is incredibly useful, as it allows you to provide detailed contextual information.

One use case for this feature is to report application exceptions via the ErrorController. This can then provide some great feedback via your Zend Server GUI; you can filter based on a "Rule Name" of "Custom Event", and further on criteria such as Severity to determine when and why your application is hitting the ErrorController -- and hopefully reduce such occurrences.

We thought this might make a good default use case, and have provided some code generation surrounding it in Zend_Tool. The ErrorController will now check to see if a Log resource is available, and if so, write to it.

To make this happen, we've also written a new Log bootstrap resource that piggy-backs on some additional new functionality: a new factory() method in Zend_Log. This allows you to create Zend_Log instances from configuration, with one or more writers and configured filters. Enabling Zend Monitor logging via the ErrorController is now as simple as adding a single line to your configuration:


resources.log.zendmonitor.writerName = "ZendMonitor"
 

Note: you can log to any logger, or multiple loggers if desired.

If, within your controllers, you want to log other events, you can do so by simply grabbing the bootstrap object and then the Log resource:


$bootstrap = $this->getInvokeArg('bootstrap')
if ($bootstrap->hasResource('Log')) {
    $log = $bootstrap->getResource('Log');
    $log->info(/* ... */);
}
 

This kind of simple integration leads to some fantastic benefits for Zend Framework users that are using Zend Server, and it's incredibly cheap to implement (the ZendMonitor logger acts as a null logger when the Monitor extension is not present). What other uses can you find to put it to?

Note: this functionality is available now via the Zend Framework subversion repository, in trunk. It will be made available in a stable release with the upcoming 1.10 release.

Update: I'd like to point out that the bootstrap resource was made possible by contributions of two ibuildings contributors, Martin Roest and Mark van der Velden, who contributed code that makes it possible to instantiate log instances via a new factory() method.

Posted by Matthew Weier O'Phinney in PHP at 15:51 | Comments (8) | Trackback (1)
Defined tags for this entry: php, zend framework, zend server
Related entries by tags:
Autoloading Benchmarks
Applying FilterIterator to Directory Iteration
Running mod_php and FastCGI side-by-side
Creating Zend_Tool Providers
State of Zend Framework 2.0

Trackbacks
Trackback specific URI for this entry

Dutch PHP Conference 2009 wrapup
A couple of days ago I came home from one of the best PHP conferences in the world, Dutch PHP Conference 2009 with again a very elite list of speakers.Day 0: Tutorial Day Thursday, June 11 was tutorial day. A whole day dedicated at learning new things by
Weblog: All news about PHP
Tracked: Apr 17, 10:18

Comments
Display comments as (Linear | Threaded)

nice, it works with zend server community edition?
#1 Juan Felipe Alvarez Saldarriaga on 2009-12-10 20:19 (Reply)
Zend Server CE doesn't have Zend_Monitor for Events.
#1.1 Ladislav Prskavec (Link) on 2009-12-11 10:20 (Reply)
Oh nooooo ... just finished installing Zend Server CE and was now trying to get Zend Framework 1.10 from Trunk, to test the new feature. I think this feature is really great, but i will not buy Zend Server just to get a slightly better debugging tool. Ok this means i can uninstall Zend Server again and reinstall apache as it was before :-(
#1.1.1 stef on 2009-12-17 17:51 (Reply)
Slightly better debugging tool? You may want to try the Debugging and Profiling support in Zend Server CE which you can use in combination with Zend Studio or Eclipse PDT. It's the best PHP debugging I've ever seen!

And don't forget the GUI which allows you to visually set php.ini parameters, or restart the server, or check the php error log or phpinfo(). And the performance improvement you get from the Optimizer+ extension. All in one place.

As you can tell, I'm very happy with it, even without the commercial license. I'd suggest giving CE a serious test-drive before writing it off.
#1.1.1.1 Martijn Heemels (Link) on 2009-12-18 12:30 (Reply)
So you tested the ZS CE version and are very happy with the Debugging / Profiling Events from Zend Server, if this is true i wonder why Ladislav Prskavec says there is no Events Module in the CE version.

As i said i did install the CE version and had no Events Window, but perhaps i missed something. I already use X-Debug, and i use Eclipse, so spending 1000$ for a Debugging tool which is perhaps a little bit better then X-Debug is not an option for me, mainly because my project is a private one, im not a big firm who can spend thousands of dollars per server ;-) Thats also why i have installed CentOs and not RedHat, i dont have the money.
#1.1.1.1.1 stef on 2009-12-20 17:52 (Reply)
Well, those are two different things. The Monitoring is a commercial feature, and not available in CE. It provides a GUI and API to log events, such as "This SQL query ran slow" or "PHP fatal error". This is quite useful, since the event includes data needed to reproduce the application state in Zend Studio. See http://static.zend.com/topics/zend-server-event-details-scr.jpg for an example. I agree it's pricey, so only interesting for commercial php servers.

The Debugging and Profiling is separate from Monitoring. It's similar to Xdebug though more polished. There's no GUI included with Zend Server, just the extensions that interact with the PHP engine. To actually use it you need Zend Studio or Eclipse PDT (http://www.zend.com/community/pdt). It provides interactive debugging (stepping through code) and very detailed timing information and execution flow for your application.

It took me some time to get it working, mostly because you need to allow the zend server to connect back to your PDT, but now that I know how it works it's great. The most streamlined debugging for PHP that I know of, and in the same IDE that you're programming in. I can't say if it's for you, but check out some videos about the combination.

Here's a comparison between the CE and commercial versions: http://www.zend.com/en/products/server/editions
#1.1.1.1.1.1 Martijn Heemels (Link) on 2009-12-20 19:09 (Reply)
I just downloaded Zend Framework 1.10alpha1 and used your code to see my Error controller messages. When I purposefully type wrong address I can see a Custom Event in my Zend Server Monitor but when I click for detail I have big red JavaScript errors:

An internal JavaScript error occurred, try to refresh the page.
If this problem persists, go to the Zend Support Center
Error details »

Error parsing in _ContentSetter#Setter_event-context_3, Tried to register widget with id==message-pane but that id is already registered

Do you have any idea what's wrong?
Adam
#2 Adam Sowinski on 2009-12-22 12:37 (Reply)
eee,thank you for your entry~i am studying for zend serve
#3 iwebearn (Link) on 2009-12-30 18:29 (Reply)

Add Comment

Standard emoticons like :-) and ;-) are converted to images.
E-Mail addresses will not be displayed and will only be used for E-Mail notifications

To prevent automated Bots from commentspamming, please enter the string you see in the image below in the appropriate input box. Your comment will only be submitted if the strings match. Please ensure that your browser supports and accepts cookies, or your comment cannot be verified correctly.
CAPTCHA

 
 
  • Home
  • Resume
  • Blog
  • Phly PEAR Channel
  • Twitter
  • Contact Me
  • About this site

ZCE

Zend Education Advisory Board Member

Add to Technorati Favorites

Calendar

Back September '10
Mon Tue Wed Thu Fri Sat Sun
    1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30      

Quicksearch

Links

  • PHLY - PHp LibrarY
  • Planet PHP
  • Zend Framework, where I'm project lead
  • Sebastian Bergmann
  • Cal Evans
  • Shahar Evron
  • Paul M. Jones
  • Bill Karwin
  • Mike Naberezny
  • Fabien Potencier
  • Ben Ramsey
  • Derick Rethans
  • Ralph Schindler
  • Marco Tabini

Archives

September 2010
August 2010
July 2010
Recent...
Older...

Categories

XML Linux
XML Personal
XML Aikido
XML Family
XML Programming
XML Dojo
XML Perl
XML PHP

All categories

Syndicate This Blog

XML RSS 0.91 feed
XML RSS 1.0 feed
XML RSS 2.0 feed
ATOM/XML ATOM 0.3 feed
ATOM/XML ATOM 1.0 feed
XML RSS 2.0 Comments

Show tagged entries

xml apache
xml best practices
xml books
xml conferences
xml cw09
xml decorators
xml dojo
xml dpc08
xml file_fortune
xml git
xml linux
xml mvc
xml oop
xml pear
xml perl
xml personal
xml php
xml phpworks08
xml programming
xml rest
xml ubuntu
xml vim
xml webinar
xml zendcon
xml zendcon08
xml zendcon09
xml zend framework
© 2004 - present, Matthew Weier O'Phinney
matthew-web <at> weierophinney.net