33
Errors, Exceptions & Logging by James Titcumb at PHPNW13 Uncon

Errors, Exceptions & Logging (PHPNW13 Uncon)

Embed Size (px)

Citation preview

Page 1: Errors, Exceptions & Logging (PHPNW13 Uncon)

Errors, Exceptions & Loggingby James Titcumb

at PHPNW13 Uncon

Page 2: Errors, Exceptions & Logging (PHPNW13 Uncon)

Errors

Page 3: Errors, Exceptions & Logging (PHPNW13 Uncon)

● 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?

Page 4: Errors, Exceptions & Logging (PHPNW13 Uncon)

Types of PHP Errors

● E_ERROR● E_WARNING● E_NOTICE● E_PARSE● Others (E_STRICT, E_DEPRECATED) etc.● User errors (E_USER_ERROR etc.)

Page 5: Errors, Exceptions & Logging (PHPNW13 Uncon)

E_ERROR

<?php

foo();

// Fatal error: Call to undefined function foo() in /in//in/N2gbL on line 3

Page 6: Errors, Exceptions & Logging (PHPNW13 Uncon)

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

Page 7: Errors, Exceptions & Logging (PHPNW13 Uncon)

E_NOTICE

<?php

$a = $b;

// Notice: Undefined variable: b in /in//in/9dPC5 on line 3

Page 8: Errors, Exceptions & Logging (PHPNW13 Uncon)

E_PARSE

<?php

x c

// Parse error: syntax error, unexpected 'c' (T_STRING) in /in//in/fkEaj on line 3

Page 9: Errors, Exceptions & Logging (PHPNW13 Uncon)

Problems?

source: http://www.dpaddbags.com/blog/episode-119-technology-sucks/

Page 10: Errors, Exceptions & Logging (PHPNW13 Uncon)

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”

Page 11: Errors, Exceptions & Logging (PHPNW13 Uncon)

Ways Around// Will raise E_NOTICEfopen($somefile, 'r');

// No error! :)if (file_exists($somefile)) { fopen($somefile, 'r');} else { // nice handling...}

Page 12: Errors, Exceptions & Logging (PHPNW13 Uncon)

Exceptions

Page 13: Errors, Exceptions & Logging (PHPNW13 Uncon)

● 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?

Page 14: Errors, Exceptions & Logging (PHPNW13 Uncon)

Jargon Buster

● throwTriggering an exception

● tryTry to run a piece of code which *may* throw an exception

● catchHandle an exception

● finallyAlways run some code after a try/catch block

Page 15: Errors, Exceptions & Logging (PHPNW13 Uncon)

● Built in to PHP ● More descriptive than just “Exception”, e.g.:

○ InvalidArgumentException○ LogicException○ OutOfBoundsException○ RuntimeException○ see PHP manual for more

SPL Exceptions

Page 16: Errors, Exceptions & Logging (PHPNW13 Uncon)

Example (throw)

class Division

{ public function divide($a, $b)

{

if ($b == 0) {

throw new Exception(‘div by zero’);

}

return ($a / $b);

}

}

Page 17: Errors, Exceptions & Logging (PHPNW13 Uncon)

Example (catch)

$division = new Division();

try

{

$result = $division->divide(5, 0);

}

catch (Exception $exception)

{

$logger->warning($exception->getMessage());

}

Page 18: Errors, Exceptions & Logging (PHPNW13 Uncon)

Logging

Page 19: Errors, Exceptions & Logging (PHPNW13 Uncon)
Page 20: Errors, Exceptions & Logging (PHPNW13 Uncon)

Why use logging?

● Easier to find problems● More detail● “paper trail” for code● Log where you want

Page 21: Errors, Exceptions & Logging (PHPNW13 Uncon)

What about Apache’s error_log?

source: http://up-ship.com/blog/?p=20903

Page 22: Errors, Exceptions & Logging (PHPNW13 Uncon)

Why?

● error_log is too basic (message, file, line)● difficult to read / parse● depends on “error_reporting” setting

Page 23: Errors, Exceptions & Logging (PHPNW13 Uncon)

● monolog● phpconsole● log4php● RavenPHP + Sentry● FirePHP (dev environment)● Roll your own

Logging Options

Page 24: Errors, Exceptions & Logging (PHPNW13 Uncon)

Requirements (for everyone)

● Fire & forget● Minimum or zero latency● Highly available● Should be PSR-3 compatible● Log everything:

○ Exceptions○ Errors○ Fatal Errors

Page 25: Errors, Exceptions & Logging (PHPNW13 Uncon)

How they work...

source: http://mirror-sg-wv1.gallery.hd.org/_c/natural-science/cogs-with-meshed-teeth-AJHD.jpg.html

Page 26: Errors, Exceptions & Logging (PHPNW13 Uncon)

Capture Method

Data Storage

Logger (PSR-3)

Handler / Adapter

Typical PSR-3 Compatible Design

Page 27: Errors, Exceptions & Logging (PHPNW13 Uncon)

Monolog\ErrorHandler->handleException()

MongoDB

Monolog\Logger->log()

Monolog\Handler->handle()

Monolog

Page 28: Errors, Exceptions & Logging (PHPNW13 Uncon)

Ed\Log\Handler\ErrorHandler->handleException()

RabbitMQ

Ed\Log\Logger->log()

Ed\Log\Publisher\AmqpPublisher->publish()

Logging Server

Low Latency (using AMQP)

JSON payload

Page 29: Errors, Exceptions & Logging (PHPNW13 Uncon)

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

Page 30: Errors, Exceptions & Logging (PHPNW13 Uncon)

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

Page 31: Errors, Exceptions & Logging (PHPNW13 Uncon)

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!

Page 32: Errors, Exceptions & Logging (PHPNW13 Uncon)

Questions?

Page 33: Errors, Exceptions & Logging (PHPNW13 Uncon)

Thanks!

@asgrimgithub.com/asgrim