10
Logging with Monolog Tudor Barbu @motanelu || @CodeVoyagers #phpbc n Lighting talk

Logging with Monolog

Embed Size (px)

Citation preview

Page 1: Logging with Monolog

Logging with Monolog

Tudor Barbu@motanelu || @CodeVoyagers

#phpbcnLighting talk

Page 2: Logging with Monolog

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

Page 3: Logging with Monolog

Monolog Channel

Filehandler

SlackHandler

Formatter(s) Formatter(s)

/var/log/my-app/error.log #my-app-production-monitoring

Page 4: Logging with Monolog

AmqpBrowser ConsoleBufferChrome PHPCouchDBCubeDeduplicationDoctrine CouchDBDynamoDbElasticSearchErrorLogFirePHPFleepHookFlowdockGelfHip Chat-IFTTTLogEntriesLogglyMail

MandrillMongoDBNative MailerNew RelicNullPHPConsolePsrPushoverRavenRedisRollbarRotating FileSamplingSlackSocket StreamSwift MailerSyslogSyslog UdpTest Zend Monitor

Available handlers

Page 5: Logging with Monolog

Common problems with logs

Page 6: Logging with Monolog

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.

Page 7: Logging with Monolog

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

Page 8: Logging with Monolog

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

Page 9: Logging with Monolog

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

Page 10: Logging with Monolog

Thank you

@[email protected]@tudorbarbu.ninja

@CodeVoyagerswww.codevoyagers.com