Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Errors, Exceptions & Logging
by James Titcumb
at PHPNW13 Uncon
Errors
● Something broke… :)
● e.g.
○ Can’t connect to MySQL (mysqli_connect)
○ No such file or directory (fopen)
● Usually from ...
Types of PHP Errors
● E_ERROR
● E_WARNING
● E_NOTICE
● E_PARSE
● Others (E_STRICT, E_DEPRECATED) etc.
● User errors (E_USE...
E_ERROR
<?php
foo();
// Fatal error: Call to undefined
function foo() in /in//in/N2gbL on
line 3
E_WARNING
<?php
fopen('foo', 'r');
// Warning: fopen(foo): failed to
open stream: No such file or
directory in /in//in/tZH...
E_NOTICE
<?php
$a = $b;
// Notice: Undefined variable: b in
/in//in/9dPC5 on line 3
E_PARSE
<?php
x c
// Parse error: syntax error,
unexpected 'c' (T_STRING) in
/in//in/fkEaj on line 3
Problems?
source: http://www.dpaddbags.com/blog/episode-119-technology-sucks/
Problems.
● Depends on “error_reporting” php.ini setting
● Displaying errors is UGLY
● Existence of “@” operator
● Only lo...
Ways Around
// Will raise E_NOTICE
fopen($somefile, 'r');
// No error! :)
if (file_exists($somefile)) {
fopen($somefile, '...
Exceptions
● Something still broke
● Wider scope:
○ Logic errors
○ Flow control (for errors)
● “Catchable”
● Turn into fatal errors i...
Jargon Buster
● throw
Triggering an exception
● try
Try to run a piece of code which *may* throw an
exception
● catch
Hand...
● Built in to PHP
● More descriptive than just “Exception”, e.g.:
○ InvalidArgumentException
○ LogicException
○ OutOfBound...
Example (throw)
class Division
{
public function divide($a, $b)
{
if ($b == 0) {
throw new Exception(‘div by zero’);
}
ret...
Example (catch)
$division = new Division();
try
{
$result = $division->divide(5, 0);
}
catch (Exception $exception)
{
$log...
Logging
Why use logging?
● Easier to find problems
● More detail
● “paper trail” for code
● Log where you want
What about Apache’s error_log?
source: http://up-ship.com/blog/?p=20903
Why?
● error_log is too basic (message, file, line)
● difficult to read / parse
● depends on “error_reporting” setting
● monolog
● phpconsole
● log4php
● RavenPHP + Sentry
● FirePHP (dev environment)
● Roll your own
Logging Options
Requirements (for everyone)
● Fire & forget
● Minimum or zero latency
● Highly available
● Should be PSR-3 compatible
● Lo...
How they work...
source: http://mirror-sg-wv1.gallery.hd.org/_c/natural-science/cogs-with-meshed-teeth-AJHD.jpg.html
Capture Method
Data Storage
Logger (PSR-3)
Handler / Adapter
Typical PSR-3 Compatible Design
MonologErrorHandler
->handleException()
MongoDB
MonologLogger
->log()
MonologHandler
->handle()
Monolog
EdLogHandlerErrorHandler
->handleException()
RabbitMQ
EdLogLogger
->log()
EdLogPublisherAmqpPublisher
->publish()
Logging ...
Capturing Logging
Use these and send output to $logger
● set_exception_handler()
○ Handles all uncaught exceptions
● set_e...
Sending Log Messages
● PSR-3 makes it easy
● However you want…
● Monolog has loads:
○ syslog-compatible / error_log
○ Emai...
Summary
● PHP generates errors usually
● Exceptions are great in OOP context
● More control with exceptions
● Logging is i...
Questions?
Thanks!
@asgrim
github.com/asgrim
Errors, Exceptions & Logging (PHPNW13 Uncon)
Upcoming SlideShare
Loading in …5
×

Errors, Exceptions & Logging (PHPNW13 Uncon)

650 views

Published on

Published in: Technology
  • Be the first to comment

Errors, Exceptions & Logging (PHPNW13 Uncon)

  1. 1. Errors, Exceptions & Logging by James Titcumb at PHPNW13 Uncon
  2. 2. Errors
  3. 3. ● Something broke… :) ● e.g. ○ Can’t connect to MySQL (mysqli_connect) ○ No such file or directory (fopen) ● Usually from PHP core ● Sometimes fatal (stop execution) What are errors?
  4. 4. Types of PHP Errors ● E_ERROR ● E_WARNING ● E_NOTICE ● E_PARSE ● Others (E_STRICT, E_DEPRECATED) etc. ● User errors (E_USER_ERROR etc.)
  5. 5. E_ERROR <?php foo(); // Fatal error: Call to undefined function foo() in /in//in/N2gbL on line 3
  6. 6. E_WARNING <?php fopen('foo', 'r'); // Warning: fopen(foo): failed to open stream: No such file or directory in /in//in/tZHGY on line 3
  7. 7. E_NOTICE <?php $a = $b; // Notice: Undefined variable: b in /in//in/9dPC5 on line 3
  8. 8. E_PARSE <?php x c // Parse error: syntax error, unexpected 'c' (T_STRING) in /in//in/fkEaj on line 3
  9. 9. Problems? source: http://www.dpaddbags.com/blog/episode-119-technology-sucks/
  10. 10. Problems. ● Depends on “error_reporting” php.ini setting ● Displaying errors is UGLY ● Existence of “@” operator ● Only logs to file or syslog ● Easily ignored ● Not very “OO”
  11. 11. Ways Around // Will raise E_NOTICE fopen($somefile, 'r'); // No error! :) if (file_exists($somefile)) { fopen($somefile, 'r'); } else { // nice handling... }
  12. 12. Exceptions
  13. 13. ● Something still broke ● Wider scope: ○ Logic errors ○ Flow control (for errors) ● “Catchable” ● Turn into fatal errors if not caught ● They are classes (can make your own) ● Common in other OO languages What are exceptions?
  14. 14. Jargon Buster ● throw Triggering an exception ● try Try to run a piece of code which *may* throw an exception ● catch Handle an exception ● finally Always run some code after a try/catch block
  15. 15. ● Built in to PHP ● More descriptive than just “Exception”, e.g.: ○ InvalidArgumentException ○ LogicException ○ OutOfBoundsException ○ RuntimeException ○ see PHP manual for more SPL Exceptions
  16. 16. Example (throw) class Division { public function divide($a, $b) { if ($b == 0) { throw new Exception(‘div by zero’); } return ($a / $b); } }
  17. 17. Example (catch) $division = new Division(); try { $result = $division->divide(5, 0); } catch (Exception $exception) { $logger->warning($exception- >getMessage()); }
  18. 18. Logging
  19. 19. Why use logging? ● Easier to find problems ● More detail ● “paper trail” for code ● Log where you want
  20. 20. What about Apache’s error_log? source: http://up-ship.com/blog/?p=20903
  21. 21. Why? ● error_log is too basic (message, file, line) ● difficult to read / parse ● depends on “error_reporting” setting
  22. 22. ● monolog ● phpconsole ● log4php ● RavenPHP + Sentry ● FirePHP (dev environment) ● Roll your own Logging Options
  23. 23. Requirements (for everyone) ● Fire & forget ● Minimum or zero latency ● Highly available ● Should be PSR-3 compatible ● Log everything: ○ Exceptions ○ Errors ○ Fatal Errors
  24. 24. How they work... source: http://mirror-sg-wv1.gallery.hd.org/_c/natural-science/cogs-with-meshed-teeth-AJHD.jpg.html
  25. 25. Capture Method Data Storage Logger (PSR-3) Handler / Adapter Typical PSR-3 Compatible Design
  26. 26. MonologErrorHandler ->handleException() MongoDB MonologLogger ->log() MonologHandler ->handle() Monolog
  27. 27. EdLogHandlerErrorHandler ->handleException() RabbitMQ EdLogLogger ->log() EdLogPublisherAmqpPublisher ->publish() Logging Server Low Latency (using AMQP) JSON payload
  28. 28. Capturing Logging Use these and send output to $logger ● set_exception_handler() ○ Handles all uncaught exceptions ● set_error_handler() ○ Handles most errors ● register_shutdown_function() ○ Handles fatal errors
  29. 29. Sending Log Messages ● PSR-3 makes it easy ● However you want… ● Monolog has loads: ○ syslog-compatible / error_log ○ Email, HipChat ○ AMQP, Sentry, Zend Monitor, Graylog2 ○ Redis, MongoDB, CouchDB
  30. 30. Summary ● PHP generates errors usually ● Exceptions are great in OOP context ● More control with exceptions ● Logging is important ● Logging is easy ● Short term investment, long term benefit ● NO EXCUSES!
  31. 31. Questions?
  32. 32. Thanks! @asgrim github.com/asgrim

×