SlideShare a Scribd company logo
Low Latency Logging
at BrightonPHP
James Titcumb

@asgrim
github.com/asgrim
● Started on Amiga 500
● PHP for 11 years
● ZCE PHP 5.3
●
●
●
●
●

Run PHP Hampshire user group
Lead dev on browscap.ini
First open source GoDeploy
Development Manager at Protected.co.uk
Mostly a ZF2 developer

@asgrim
github.com/asgrim
Hello! :)

@asgrim
github.com/asgrim
Errors
What are errors?
● Something broke… :)
● e.g.
○ Can’t connect to MySQL (mysqli_connect)
○ No such file or directory (fopen)

● Usually PHP core
● Sometimes “fatal”
Problems?

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

error_reporting setting
Errors look ugly
“@”
Limited options
Not very “OO”
Ways Around
// Will raise E_NOTICE
fopen($somefile, 'r');
Ways Around
// No error! :)
if (file_exists($somefile)) {
fopen($somefile, 'r');
} else {
// nice handling...
// maybe throw exception...
}
Exceptions
Jargon Buster
● throw
Triggering
● try
Try to run
● catch
Handle it
● finally
Run code after try/catch
What are exceptions?
●
●
●
●
●

Something still broke
OO
Catchable
Fatal errors
They are classes
SPL Exceptions
● Built in to PHP
● More descriptive than just “Exception”, e.g.:
○
○
○
○
○

InvalidArgumentException
LogicException
OutOfBoundsException
RuntimeException
see PHP manual for more
Example (exception class)
class DivisionByZeroException
extends LogicException
{
}
Example (throw)
class Mathematics
{
public function divide($a, $b)
{
if ($b == 0) {
$msg = sprintf(‘Tried to divide %s by zero”, $a);
throw new DivisionByZeroException($msg);
}
return ($a / $b);
}
}
Example (catch)
$maths = new Mathematics();
try
{
$result = $maths->divide(5, 0);
}
catch (DivisionByZeroException $exception)
{
$logger->warning($exception>getMessage());
}
Logging
Low latency Logging (BrightonPHP - 18th Nov 2013)
What is “logging”?
Keeping a record of all events...
exceptions, errors, warnings, info, debug
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
● Reading / parsing
● error_reporting (again)
Doin’ it right wrong…
/************************************************************
*

Magic file that makes your entire project work perfectly

*

************************************************************/
@ini_set('display_errors', 0);
@error_reporting(0);
function __globalErrorHandler()
{
return true;
}
@set_error_handler('__globalErrorHandler');
@set_exception_handler('__globalErrorHandler');
@register_shutdown_function(function() {
if(error_get_last())
{
echo "Script executed successfully!";
}
});

https://github.com/webarto/boostrap.php
Requirements (for everyone)
●
●
●
●

Fire & forget
Minimum or zero latency
Highly available
Log everything:
○
○
○
○

Exceptions
Errors
Fatal Errors
Debug & info

● PSR-3 compatible
PSR-3
● Common logging interface
→ LoggerInterface
● RFC-5424 Levels
(debug, info, notice, warning, error, critical, alert, emergency)

● Interoperability
● Reusability
Logging Options
●
●
●
●
●
●
●

monolog (PSR-3)
Drupal - PSR-3 Watchdog
phpconsole
log4php
RavenPHP + Sentry
FirePHP (dev environment)
Roll your own
How they work...

source: http://mirror-sg-wv1.gallery.hd.org/_c/natural-science/cogs-with-meshed-teeth-AJHD.jpg.html
Capturing Logging
Use “capture methods”, send to $logger
● set_exception_handler()
○ Handles all uncaught exceptions

● set_error_handler()
○ Handles most errors

● register_shutdown_function()
○ Handles fatal errors
Sending Log Messages
● Handler/Adapter translates
● However you want…
● Monolog has loads:
○
○
○
○

syslog-compatible / error_log
Email, HipChat
AMQP, Sentry, Zend Monitor, Graylog2
Redis, MongoDB, CouchDB
Typical PSR-3 Compatible Design
Capture Method

Logger (PSR-3)

Handler / Adapter

Data Storage
Monolog
MonologErrorHandler
->handleException()

MonologLogger
->log()

MonologHandler
->handle()
Low Latency
High Performance
What do I mean?
● Easy to add
● Fire & Forget
● Minimum impact to request
Slow Logging
Browser

Application

Log Server

HTTP request

Error!

Send log message to database

Acknowledge message
HTTP response to client
Zero Latency Logging (ideal)
Browser

Application

Log Server

HTTP request

Error!

HTTP response to client

Send log message to database
What about UDP?
● Yes… Zero latency, but….
● Do you even care about your logs?
(UDP means log messages may get lost...)
● TCP means guaranteed network delivery
● Any non-blocking fails
Low Latency Logging (balance)
Browser

Application

Log Server

HTTP request

Error!

HTTP response to client

Send log message to database
So how?
Say hello to RabbitMQ
What is RabbitMQ?
●
●
●
●

Robust messaging for applications
Easy to use
Runs on all major operating systems
Supports a huge number of developer
platforms
● Open source and commercially supported
www.rabbitmq.com
RabbitMQ - Basic

queue
“Consumer”

“Publisher”

source: http://www.rabbitmq.com/tutorials/tutorial-one-php.html
RabbitMQ - Exchanges
“Publisher”

“Publisher”

queue 1
“Consumer 1”

“Publisher”

“Exchange”

queue 2
“Consumer 2”

“Publisher”
“Publisher”

source: http://www.rabbitmq.com/tutorials/tutorial-three-php.html
Using Queues === Fast!
Add RabbitMQ to logging architecture...
Low Latency (using AMQP)
EdLogHandlerErrorHandler
->handleException()

EdLogLogger
->log()

EdLogPublisherAmqpPublisher
->publish()

RabbitMQ

Logging Server

JSON payload
Low Latency Logging (with AMQP)
Browser

Application

RabbitMQ

Log Server

HTTP request

Error!

JSON via AMQP
HTTP response

Fetch message
Why bother?
● Scalability
Log Worker
Application A

Log Worker
Application B

RabbitMQ
Log Worker

Application C
Log Worker
Single Point of Failure...
● RabbitMQ can do HA
RabbitMQ
Node 6
RabbitMQ
Node 1

RabbitMQ
Node 2
RabbitMQ
Node 5

RabbitMQ
Node 3
RabbitMQ
Node 4
Live demo
(pre-recorded… meh)
Questions?
Feedback please...
https://joind.in/9928
Thanks!
https://joind.in/9928

@asgrim
github.com/asgrim

More Related Content

PDF
Apache Dispatch
PDF
A Tour of Go - Workshop
PDF
The Go features I can't live without, 2nd round
PDF
MySafe
PDF
The Go features I can't live without
PDF
Learning Python from Data
PDF
Programming with Python - Adv.
PDF
Node.js for Rubists
Apache Dispatch
A Tour of Go - Workshop
The Go features I can't live without, 2nd round
MySafe
The Go features I can't live without
Learning Python from Data
Programming with Python - Adv.
Node.js for Rubists

What's hot (20)

PDF
Php under the_hood
PDF
Practicing Python 3
PDF
Dynamic PHP web-application analysis
PDF
OSCON2014 : Quick Introduction to System Tools Programming with Go
PPT
Go lang introduction
PDF
PHP, Under The Hood - DPC
PDF
Unix is my IDE
PDF
Go for SysAdmins - LISA 2015
PPTX
PHP Profiling/performance
PDF
Python debugging techniques
PDF
Profile all the things! - Capital Go 2017
ODP
Piloting processes through std IO at the Ruby Drink-up of Sophia, January 2012
PDF
Painless Data Storage with MongoDB & Go
PDF
Static Analysis of PHP Code – IPC Berlin 2016
PDF
Gcrc talk
ODP
Getting groovy (ODP)
PDF
Methods of debugging - Atomate.net
PDF
Clojure and Modularity
PDF
Variable precedence: Where should I put a variable?
PDF
PHP 7 OPCache extension review
Php under the_hood
Practicing Python 3
Dynamic PHP web-application analysis
OSCON2014 : Quick Introduction to System Tools Programming with Go
Go lang introduction
PHP, Under The Hood - DPC
Unix is my IDE
Go for SysAdmins - LISA 2015
PHP Profiling/performance
Python debugging techniques
Profile all the things! - Capital Go 2017
Piloting processes through std IO at the Ruby Drink-up of Sophia, January 2012
Painless Data Storage with MongoDB & Go
Static Analysis of PHP Code – IPC Berlin 2016
Gcrc talk
Getting groovy (ODP)
Methods of debugging - Atomate.net
Clojure and Modularity
Variable precedence: Where should I put a variable?
PHP 7 OPCache extension review
Ad

Viewers also liked (20)

PDF
[TapjoyX5Rocks App Discovery Seminar] Session 3 - 선데이토즈 임정민 이사
PPT
Команда единомышленников- создаем сайт доу
PDF
ACE FORKLIFT
PPTX
Las vegas convention photographer
PDF
Pedido de-impeachment-na-c3adntegra
PDF
Presentazione Gruppo Informatica Solidale Firenze - Febbraio 2014
PDF
Club Palm: What does history suggest?
PPTX
How to lose weight fast
PPTX
unity in diversity
PDF
Salon emploi 2016
PDF
Dialnet la teoriahistoricoculturaldevygotsky-2382969
PPTX
tiempo correcion
PDF
Ronaldo Villaver - Candidate for FILCCA President
PPTX
Presentacion power point
PDF
Bender keynote
PPTX
PPTX
Daniel
PPTX
Physical Science Unit 4
PDF
... now write an interpreter (PHPem 2016)
[TapjoyX5Rocks App Discovery Seminar] Session 3 - 선데이토즈 임정민 이사
Команда единомышленников- создаем сайт доу
ACE FORKLIFT
Las vegas convention photographer
Pedido de-impeachment-na-c3adntegra
Presentazione Gruppo Informatica Solidale Firenze - Febbraio 2014
Club Palm: What does history suggest?
How to lose weight fast
unity in diversity
Salon emploi 2016
Dialnet la teoriahistoricoculturaldevygotsky-2382969
tiempo correcion
Ronaldo Villaver - Candidate for FILCCA President
Presentacion power point
Bender keynote
Daniel
Physical Science Unit 4
... now write an interpreter (PHPem 2016)
Ad

Similar to Low latency Logging (BrightonPHP - 18th Nov 2013) (20)

PDF
Low Latency Logging with RabbitMQ (PHP London - 4th Sep 2014)
ODP
Turbo charge your logs
PDF
Low Latency Logging with RabbitMQ (Brno PHP, CZ - 20th Sep 2014)
PDF
Go debugging and troubleshooting tips - from real life lessons at SignalFx
ODP
Turbo charge your logs
PDF
Fuzzing softwares for bugs - OWASP Seasides
PDF
PSR-3 logs using Monolog and Graylog
ODP
Debugging Rails 3 Applications
PDF
PyConUK 2014 - PostMortem Debugging and Web Development Updated
ODP
Ceph Day Melbourne - Troubleshooting Ceph
PDF
Getting modern with logging via log4perl
PDF
How a Failed Experiment Helped Me Understand the Go Runtime in More Depth
PDF
Screaming Fast Wpmu
PDF
Ruxmon.2013-08.-.CodeBro!
PDF
Customize and Secure the Runtime and Dependencies of Your Procedural Language...
PDF
We shall play a game....
PDF
Errors, Exceptions & Logging (PHP Hants Oct '13)
PPTX
A simple tool for debug (tap>)
PDF
Errors, Exceptions & Logging (PHPNW13 Uncon)
PDF
44CON London 2015 - 15-Minute Linux Incident Response Live Analysis
Low Latency Logging with RabbitMQ (PHP London - 4th Sep 2014)
Turbo charge your logs
Low Latency Logging with RabbitMQ (Brno PHP, CZ - 20th Sep 2014)
Go debugging and troubleshooting tips - from real life lessons at SignalFx
Turbo charge your logs
Fuzzing softwares for bugs - OWASP Seasides
PSR-3 logs using Monolog and Graylog
Debugging Rails 3 Applications
PyConUK 2014 - PostMortem Debugging and Web Development Updated
Ceph Day Melbourne - Troubleshooting Ceph
Getting modern with logging via log4perl
How a Failed Experiment Helped Me Understand the Go Runtime in More Depth
Screaming Fast Wpmu
Ruxmon.2013-08.-.CodeBro!
Customize and Secure the Runtime and Dependencies of Your Procedural Language...
We shall play a game....
Errors, Exceptions & Logging (PHP Hants Oct '13)
A simple tool for debug (tap>)
Errors, Exceptions & Logging (PHPNW13 Uncon)
44CON London 2015 - 15-Minute Linux Incident Response Live Analysis

More from James Titcumb (20)

PDF
Living the Best Life on a Legacy Project (phpday 2022).pdf
PDF
Tips for Tackling a Legacy Codebase (ScotlandPHP 2021)
PDF
Climbing the Abstract Syntax Tree (Midwest PHP 2020)
PDF
Best practices for crafting high quality PHP apps (Bulgaria 2019)
PDF
Climbing the Abstract Syntax Tree (php[world] 2019)
PDF
Best practices for crafting high quality PHP apps (php[world] 2019)
PDF
Crafting Quality PHP Applications (PHP Joburg Oct 2019)
PDF
Climbing the Abstract Syntax Tree (PHP Russia 2019)
PDF
Best practices for crafting high quality PHP apps - PHP UK 2019
PDF
Climbing the Abstract Syntax Tree (ScotlandPHP 2018)
PDF
Best practices for crafting high quality PHP apps (ScotlandPHP 2018)
PDF
Kicking off with Zend Expressive and Doctrine ORM (PHP South Africa 2018)
PDF
Best practices for crafting high quality PHP apps (PHP South Africa 2018)
PDF
Climbing the Abstract Syntax Tree (PHP Developer Days Dresden 2018)
PDF
Climbing the Abstract Syntax Tree (Southeast PHP 2018)
PDF
Crafting Quality PHP Applications (PHPkonf 2018)
PDF
Best practices for crafting high quality PHP apps (PHP Yorkshire 2018)
PDF
Crafting Quality PHP Applications: an overview (PHPSW March 2018)
PDF
Kicking off with Zend Expressive and Doctrine ORM (PHP MiNDS March 2018)
PDF
Climbing the Abstract Syntax Tree (PHP UK 2018)
Living the Best Life on a Legacy Project (phpday 2022).pdf
Tips for Tackling a Legacy Codebase (ScotlandPHP 2021)
Climbing the Abstract Syntax Tree (Midwest PHP 2020)
Best practices for crafting high quality PHP apps (Bulgaria 2019)
Climbing the Abstract Syntax Tree (php[world] 2019)
Best practices for crafting high quality PHP apps (php[world] 2019)
Crafting Quality PHP Applications (PHP Joburg Oct 2019)
Climbing the Abstract Syntax Tree (PHP Russia 2019)
Best practices for crafting high quality PHP apps - PHP UK 2019
Climbing the Abstract Syntax Tree (ScotlandPHP 2018)
Best practices for crafting high quality PHP apps (ScotlandPHP 2018)
Kicking off with Zend Expressive and Doctrine ORM (PHP South Africa 2018)
Best practices for crafting high quality PHP apps (PHP South Africa 2018)
Climbing the Abstract Syntax Tree (PHP Developer Days Dresden 2018)
Climbing the Abstract Syntax Tree (Southeast PHP 2018)
Crafting Quality PHP Applications (PHPkonf 2018)
Best practices for crafting high quality PHP apps (PHP Yorkshire 2018)
Crafting Quality PHP Applications: an overview (PHPSW March 2018)
Kicking off with Zend Expressive and Doctrine ORM (PHP MiNDS March 2018)
Climbing the Abstract Syntax Tree (PHP UK 2018)

Recently uploaded (20)

PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Approach and Philosophy of On baking technology
PPTX
Cloud computing and distributed systems.
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPTX
sap open course for s4hana steps from ECC to s4
PDF
Empathic Computing: Creating Shared Understanding
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PPTX
Programs and apps: productivity, graphics, security and other tools
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Encapsulation_ Review paper, used for researhc scholars
PPTX
MYSQL Presentation for SQL database connectivity
PPTX
Spectroscopy.pptx food analysis technology
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
Diabetes mellitus diagnosis method based random forest with bat algorithm
Approach and Philosophy of On baking technology
Cloud computing and distributed systems.
Unlocking AI with Model Context Protocol (MCP)
Advanced methodologies resolving dimensionality complications for autism neur...
sap open course for s4hana steps from ECC to s4
Empathic Computing: Creating Shared Understanding
Building Integrated photovoltaic BIPV_UPV.pdf
Programs and apps: productivity, graphics, security and other tools
Understanding_Digital_Forensics_Presentation.pptx
Per capita expenditure prediction using model stacking based on satellite ima...
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
“AI and Expert System Decision Support & Business Intelligence Systems”
Encapsulation_ Review paper, used for researhc scholars
MYSQL Presentation for SQL database connectivity
Spectroscopy.pptx food analysis technology
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
The Rise and Fall of 3GPP – Time for a Sabbatical?
Digital-Transformation-Roadmap-for-Companies.pptx

Low latency Logging (BrightonPHP - 18th Nov 2013)