Logging and Monitoring - WordPress London November 2016

Logging and Monitoring - WordPress London November 2016

A talk I gave at WordPress London User Group on Application Logging and Monitoring

335072828e0fd0fdc46185ba365bee8e?s=128

craigwillis85

November 25, 2016
Tweet

Transcript

  1. log($this) $monitor->that() 24/11/16 WordPress London

  2. Who am I? Craig Willis Web Developer @ Ayima Co-organiser

    PHP London @craigwillis85
  3. Logging • What is logging? • Why do we log?

    • What do we log? • Logging in a WordPress world
  4. What is logging?

  5. Types of logs Application • Error / Exception • Access

    logs Infrastructure • Server logs • Database logs • Mail logs Issue No. URLs The <img> tag does not have an ALT attribute defined 107,669 The page contains unnecessary redirects 55,195 The page contains broken hyperlinks 9,283 The title is too long 6,444 The page contains invalid markup 5,779 The page was excluded by a noindex attribute 4,531 The page contains multiple canonical formats 4,508 The page contains a large amount of script code 785 The description is too long 700 The page contains a large number of Cascading Style Sheet definitions 104 The page contains multiple <h1> tags 85 The <h1> tag is missing 38 Logging and Monitoring Example of logging
  6. Why do we log?

  7. Why we log? Debugging User / System level errors Analytics

    / Reporting Accountability
  8. What do we log?

  9. EVERYTHING

  10. What to log? Information critical to you Authentication / Authorization

    APIs requests/responses Shopping Carts
  11. What to log? When Where Who What - Date /

    Time - Identifier (line number, file, name, protocol) - IP address / user - Type, log level, description
  12. Log levels (RFC 5424) DEBUG INFO NOTICE WARNING - (0):

    System is unusable - (5): Normal but significant events ERROR CRITICAL ALERT - (6): Events such as logging in, SQL logs - (4): Exceptional occurrences not errors - (3): Runtime errors no immediate action - (2): Critical. Unexpected exceptions EMERGENCY - (1): Action must be taken immediately - (7): Detailed debug information
  13. Examples

  14. Basic Example <?php try { something() } catch($e Exception) {

    error_log(“We have an error”); } <?php try { doSomething() } catch($e Exception) { error_log(“We have an error”); }
  15. tail -f error.log

  16. Monolog

  17. composer require monolog/monolog

  18. composer require “monolog/monolog:^1.0”

  19. Monolog Logging made easy Handlers Formatters Processors

  20. API Example <?php namespace WP; class FooBarAPI { public function

    __contruct($username, $password) { $this->username = $username; $this->password = $password; } public function login($username, $password) { // We would login to a real API here // Return an exception for demonstration throw new \Exception("We have an error"); } }
  21. API Example - Logger <?php namespace WP; use Monolog\Logger; class

    API extends FooBarAPI { private $logger; public function __construct(Logger $logger) { $this->logger = $logger; } public function test($something) { try { parent::login('admin', 'password'); } catch(\Exception $e) { $this->logger->alert("We have an error, oops!"); $this->logger->warning("This is a warning message"); $this->logger->debug("This is a debug message"); } } }
  22. API Example - Test File <?php require __DIR__ . '/vendor/autoload.php';

    use Monolog\Logger; use Monolog\Handler\StreamHandler; // Instantiate logger $log = new Logger('API'); $log->pushHandler(new StreamHandler('app_error.log', Logger::DEBUG)); // create the api and do something $api = new WP\API($log); $api->test("something");
  23. API Example - Log File [2016-11-14T17:57:48.271290+00:00] API.ALERT: We have an

    error, oops! [] [] [2016-11-14T17:57:48.272004+00:00] API.WARNING: This is a warning message [] []
  24. Logging in a WordPress world

  25. Logging in a Wordpress world define( ‘WP_DEBUG’, true ); define(

    `WP_DEBUG_LOG`, true); define( 'WP_DEBUG', true ); define( 'WP_DEBUG_DISPLAY', false ); define( 'WP_DEBUG_LOG', true ); // wp-content/debug.log Production? • Query Monitor • Debug Bar • Log Deprecated Notices • Error Log Monitor Plugins
  26. Real-world logging Graph Graph Visibility • Easy to access •

    Easy to read • Easy to find problems
  27. Monitoring

  28. Monitoring • Why do we monitor? • What do we

    monitor? • Real-world examples
  29. Why do monitoring? Ensuring systems run as expected Automated Measurable

    SLA / USP Application Process Management (APM)
  30. What do we monitor? Web / Database servers Web application

    endpoints External APIs Your users!
  31. Real-world examples

  32. */10 * * * * curl http://example.com/uptime.php > /dev/null 2>&1

  33. cronjob example <?php // uptime.php // check that our site

    returns 200 response $siteCheck = http_status_code(); if ( $siteCheck == ‘200’) { // All good! } else { // Send warning email mail(‘craig@example.com’, ‘Site Down’, ‘Website is down!’); }
  34. Web server status example ExtendedStatus on <Location /server-status> SetHandler server-status

    AuthType basic AuthName "Apache status" AuthUserFile /etc/httpd/conf/htpasswd Require valid-user </Location> http://example.com/server-status location /nginx_status { stub_status on; access_log off; auth_basic “Restricted”; auth_basic_user_file /etc/httpd/conf/htpasswd } http://example.com/nginx_status
  35. New Relic Server / Application / Infrastructure Monitoring

  36. New Relic Server Monitoring

  37. New Relic Application Monitoring

  38. New Relic Alerts Slack

  39. Monit

  40. Resources • Query Monitor - https://wordpress.org/plugins/query-monitor/ • Debug Bar -

    https://wordpress.org/plugins/debug-bar/ • Log Deprecated Notices - https://wordpress.org/plugins/log-deprecated-notices/ • Monolog - https://github.com/Seldaek/monolo • Mod_status - http://httpd.apache.org/docs/2.0/mod/mod_status.html • New Relic - https://newrelic.com/ • Monit - https://mmonit.com/monit/ • Logs - https://www.pexels.com/photo/brown-fire-wood-170560/ • CCTV - https://www.pexels.com/photo/police-bule-sky-security-surveillance-96612/
  41. Thank you!

  42. Questions?

  43. We’re hiring https://www.ayima.com/join-us/job-D4DF954E92