Logging with Monolog
Tudor Barbu
@motanelu || @CodeVoyagers
Not this kind of logging, but I couldn’t find a relevant picture 
Logging with Monolog
 https://github.com/Seldaek/monolog
 PHP 5.3+ && PHP ^7.0
 PSR-3 compatible
 widely used – Symfony, Laravel, etc.
$ composer require monolog/monolog
Monolog Channel
File
handler
Slack
Handler
Formatter(s) Formatter(s)
/var/log/my-app/error.log #my-app-production-monitoring
Amqp
Browser Console
Buffer
Chrome PHP
CouchDB
Cube
Deduplication
Doctrine CouchDB
DynamoDb
ElasticSearch
ErrorLog
FirePHP
FleepHook
Flowdock
Gelf
Hip Chat-
IFTTT
LogEntries
Loggly
Mail
Mandrill
MongoDB
Native Mailer
New Relic
Null
PHPConsole
PsrPushover
Raven
Redis
Rollbar
Rotating File
Sampling
Slack
Socket Stream
Swift Mailer
Syslog
Syslog Udp
Test Zend Monitor
Available handlers
Common problems with logs
Today’s quest:
Find the sweet spot of logging, where all the
information needed to fix the problem is there, yet
there’s not too much information so that the
relevant one is easily available.
Finger crossed strategy
$logger = new Logger('my-app');
$streamHandler = new StreamHandler('/var/log/my-app/error.log’);
$formatter = new LineFormatter(
"[%datetime%-%channel%[%level_name%] %message% %context% %extra%n”
);
$formatter->ignoreEmptyContextAndExtra();
$streamHandler->setFormatter($formatter);
$b64p = 'aHR0cHM6Ly93d3cueW91dHViZS5jb20vd2F0Y2g/dj1kUXc0dzlXZ1hjUQ==';
$slackHandler = new SlackHandler($b64p, '#general', 'BugsyBot');
$fcg = new FingersCrossedHandler(
[$streamHandler, $slackHandler,],
Logger::ERROR
);
$logger->setHandler($fcg);
// ...
$logger->info('...');
$logger->info('...');
$logger->warning('...');
$logger->error('...'); // all handlers are triggered
$logger = new Logger('my-app');
$streamHandler = new StreamHandler('/var/log/my-app/error.log’);
$formatter = new LineFormatter(
"[%datetime%-%channel%[%level_name%] %message% %context% %extra%n”
);
$formatter->ignoreEmptyContextAndExtra();
$streamHandler->setFormatter($formatter);
$b64p = 'aHR0cHM6Ly93d3cueW91dHViZS5jb20vd2F0Y2g/dj1kUXc0dzlXZ1hjUQ==';
$slackHandler = new SlackHandler($b64p, '#general', 'BugsyBot');
$fcg = new WhatFailureGroupHandler(
[$streamHandler, $slackHandler,]
);
$logger->setHandler($fcg);
Protect against handler failure
Missed me?
http://www.belkin.com/us/p/P-F7C050/ https://ifttt.com/recipes/227062-turn-
on-your-coffee-maker
Making sure the engineer has *all* the needed tools
Monolog IFTTT handler
WeMo powered coffee machine
Really bad bug in prod
Thank you
@motanelu
tudor.barbu@skyscanner.net
hello@tudorbarbu.ninja 
@CodeVoyagers
www.codevoyagers.com

Logging with Monolog

  • 1.
    Logging with Monolog TudorBarbu @motanelu || @CodeVoyagers
  • 2.
    Not this kindof logging, but I couldn’t find a relevant picture  Logging with Monolog  https://github.com/Seldaek/monolog  PHP 5.3+ && PHP ^7.0  PSR-3 compatible  widely used – Symfony, Laravel, etc. $ composer require monolog/monolog
  • 3.
  • 4.
    Amqp Browser Console Buffer Chrome PHP CouchDB Cube Deduplication DoctrineCouchDB DynamoDb ElasticSearch ErrorLog FirePHP FleepHook Flowdock Gelf Hip Chat- IFTTT LogEntries Loggly Mail Mandrill MongoDB Native Mailer New Relic Null PHPConsole PsrPushover Raven Redis Rollbar Rotating File Sampling Slack Socket Stream Swift Mailer Syslog Syslog Udp Test Zend Monitor Available handlers
  • 5.
  • 6.
    Today’s quest: Find thesweet spot of logging, where all the information needed to fix the problem is there, yet there’s not too much information so that the relevant one is easily available.
  • 7.
    Finger crossed strategy $logger= new Logger('my-app'); $streamHandler = new StreamHandler('/var/log/my-app/error.log’); $formatter = new LineFormatter( "[%datetime%-%channel%[%level_name%] %message% %context% %extra%n” ); $formatter->ignoreEmptyContextAndExtra(); $streamHandler->setFormatter($formatter); $b64p = 'aHR0cHM6Ly93d3cueW91dHViZS5jb20vd2F0Y2g/dj1kUXc0dzlXZ1hjUQ=='; $slackHandler = new SlackHandler($b64p, '#general', 'BugsyBot'); $fcg = new FingersCrossedHandler( [$streamHandler, $slackHandler,], Logger::ERROR ); $logger->setHandler($fcg); // ... $logger->info('...'); $logger->info('...'); $logger->warning('...'); $logger->error('...'); // all handlers are triggered
  • 8.
    $logger = newLogger('my-app'); $streamHandler = new StreamHandler('/var/log/my-app/error.log’); $formatter = new LineFormatter( "[%datetime%-%channel%[%level_name%] %message% %context% %extra%n” ); $formatter->ignoreEmptyContextAndExtra(); $streamHandler->setFormatter($formatter); $b64p = 'aHR0cHM6Ly93d3cueW91dHViZS5jb20vd2F0Y2g/dj1kUXc0dzlXZ1hjUQ=='; $slackHandler = new SlackHandler($b64p, '#general', 'BugsyBot'); $fcg = new WhatFailureGroupHandler( [$streamHandler, $slackHandler,] ); $logger->setHandler($fcg); Protect against handler failure Missed me?
  • 9.
    http://www.belkin.com/us/p/P-F7C050/ https://ifttt.com/recipes/227062-turn- on-your-coffee-maker Making surethe engineer has *all* the needed tools Monolog IFTTT handler WeMo powered coffee machine Really bad bug in prod
  • 10.