Date post: | 12-Feb-2017 |
Category: |
Software |
Upload: | tudor-barbu |
View: | 465 times |
Download: | 0 times |
Logging with Monolog
Tudor Barbu@motanelu || @CodeVoyagers
#phpbcnLighting talk
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
Filehandler
SlackHandler
Formatter(s) Formatter(s)
/var/log/my-app/error.log #my-app-production-monitoring
AmqpBrowser ConsoleBufferChrome PHPCouchDBCubeDeduplicationDoctrine CouchDBDynamoDbElasticSearchErrorLogFirePHPFleepHookFlowdockGelfHip Chat-IFTTTLogEntriesLogglyMail
MandrillMongoDBNative MailerNew RelicNullPHPConsolePsrPushoverRavenRedisRollbarRotating FileSamplingSlackSocket StreamSwift MailerSyslogSyslog UdpTest 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