Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

PSR-3 logs using Monolog and Graylog

646 views

Published on

Advantages of using PSR-3 logger. How to use Monolog in Symfony projects sending logs to Graylog.
Presented by Mateusz Wadoń

Published in: Technology
  • Be the first to comment

  • Be the first to like this

PSR-3 logs using Monolog and Graylog

  1. 1. PSR-3: logs using Monolog and Graylog
  2. 2. 2 About me UI/UX enthusiast Mateusz Wadoń mateusz.wadon@msales.com Software Developer at msales @mwadon
  3. 3. 3 ?
  4. 4. 4 Logs Record important events Debug problems with our code Application flow Track error messages History of users activity
  5. 5. 5 Old way
  6. 6. 6 PHP standards before 2009…
  7. 7. 7 PHP standards before 2009… Dependency Inversion Principle violation Hard to change logging library Apps dependent on the logging library
  8. 8. 8 PSR-3: Logger Interface http://www.php-fig.org/psr/psr-3/ interface LoggerInterface { public function emergency($message, array $context = array()); public function alert($message, array $context = array()); public function critical($message, array $context = array()); public function error($message, array $context = array()); public function warning($message, array $context = array()); public function notice($message, array $context = array()); public function info($message, array $context = array()); public function debug($message, array $context = array()); public function log($level, $message, array $context = array()); }
  9. 9. 9 PSR-3: Log levels Alert – immediate action is required Critical – critical conditions Emergency – the system is unusable Error – errors that do not require immediate attention but should be monitored Warning – unusual or undesirable occurrences that are not errors Notice – normal but significant events Info – interesting events Debug – detailed information for debugging purposes
  10. 10. 10 Monolog Logging library for PHP PSR-3-compatible https://github.com/Seldaek/monolog
  11. 11. 11 Monolog architecture https://www.webfactory.de/blog/logging-with-monolog-in-symfony2
  12. 12. 12 Monolog: Handlers Log to database (MongoDBHandler, …) Log to files and syslog (StreamHandler, SyslogHandler, …) Send alerts and emails (NativeMailerHandler, SlackbotHandler, …) Log specific servers and networked logging (GelfHandler, AmqpHandler, …) Logging in development (ChromePHPHandler, …)
  13. 13. 13 IFTTTHandler
  14. 14. 14 Monolog: Wrappers (special Handlers) FilterHandler FingersCrossedHandler DeduplicationHandler GroupHandler BufferHandler NullHandler
  15. 15. 15 Monolog: Formatters LineFormatter JsonFormatter ChromePHPFormatter GelfMessageFormatter ElasticaFormatter MongoDBFormatter
  16. 16. 16 Monolog: Processors IntrospectionProcessor WebProcessor MemoryUsageProcessor GitProcessor
  17. 17. 17 SessionRequestProcessor /** * @param array $record * @return array */ public function processRecord(array $record) { if (null === $this->sessionId) {…} if (null === $this->requestId) {…} $record['extra']['sess_id'] = $this->sessionId; $record['extra']['req_id'] = $this->requestId; return $record; }
  18. 18. 18 Do you use Frameworks/CMS? Symfony https://github.com/symfony/monolog-bundle Slim https://github.com/flynsarmy/Slim-Monolog Zend2 https://packagist.org/packages/enlitepro/enlite-monolog Drupal https://www.drupal.org/project/monolog WordPress https://packagist.org/packages/fancyguy/wordpress-monolog
  19. 19. 19 msales Logger msales.logger: abstract: true class: MsalesMonitoringBundleLogsLoggerLogger arguments: - "@logger" tags: - { name: monolog.logger, channel: logger } /** * @param $module * @param $element * @param $action */ public function setup($module, $element = null, $action = null) { $this->context = array_merge( $this->context, [ 'module' => $module, 'element' => $element, 'action' => $action, ] ); }
  20. 20. 20 msales Logger app.logger.invoice_generate: parent: app.logger calls: - [setup, [Microservice, Invoive, Generate]] app.logger.visit_product: parent: app.logger calls: - [setup, [Microservice, Product, Visit]] app.logger.buy_product: parent: app.logger calls: - [setup, [Microservice, Product, Buy]] msales.logger: abstract: true class: MsalesMonitoringBundleLogsLoggerLogger arguments: - "@logger" tags: - { name: monolog.logger, channel: logger }
  21. 21. 21 Graylog Open-source log menagement Collect and Process Analyze and Search Drill-Down and Visualise Alert and Trigger
  22. 22. 22
  23. 23. 23 ?
  24. 24. 24 Graylog http://graylog.ocoderfest.msales:8888/
  25. 25. 25
  26. 26. 26 Thanks info.pl@msales.com www.msales.com +48 32 630 40 76 follow us @msalestech follow me @mwadon
  27. 27. 27 A picture is worth a thousand words By the way, we’re hiring! Who doesn’t, right? :)

×