Download - Logging with Monolog
![Page 1: Logging with Monolog](https://reader036.vdocuments.us/reader036/viewer/2022062502/58a053761a28ab5c1c8b4941/html5/thumbnails/1.jpg)
Logging with Monolog
Tudor Barbu@motanelu || @CodeVoyagers
#phpbcnLighting talk
![Page 2: Logging with Monolog](https://reader036.vdocuments.us/reader036/viewer/2022062502/58a053761a28ab5c1c8b4941/html5/thumbnails/2.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062502/58a053761a28ab5c1c8b4941/html5/thumbnails/3.jpg)
Monolog Channel
Filehandler
SlackHandler
Formatter(s) Formatter(s)
/var/log/my-app/error.log #my-app-production-monitoring
![Page 4: Logging with Monolog](https://reader036.vdocuments.us/reader036/viewer/2022062502/58a053761a28ab5c1c8b4941/html5/thumbnails/4.jpg)
AmqpBrowser ConsoleBufferChrome PHPCouchDBCubeDeduplicationDoctrine CouchDBDynamoDbElasticSearchErrorLogFirePHPFleepHookFlowdockGelfHip Chat-IFTTTLogEntriesLogglyMail
MandrillMongoDBNative MailerNew RelicNullPHPConsolePsrPushoverRavenRedisRollbarRotating FileSamplingSlackSocket StreamSwift MailerSyslogSyslog UdpTest Zend Monitor
Available handlers
![Page 5: Logging with Monolog](https://reader036.vdocuments.us/reader036/viewer/2022062502/58a053761a28ab5c1c8b4941/html5/thumbnails/5.jpg)
Common problems with logs
![Page 6: Logging with Monolog](https://reader036.vdocuments.us/reader036/viewer/2022062502/58a053761a28ab5c1c8b4941/html5/thumbnails/6.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062502/58a053761a28ab5c1c8b4941/html5/thumbnails/7.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062502/58a053761a28ab5c1c8b4941/html5/thumbnails/8.jpg)
$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](https://reader036.vdocuments.us/reader036/viewer/2022062502/58a053761a28ab5c1c8b4941/html5/thumbnails/9.jpg)
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