Upgrade to Pro — share decks privately, control downloads, hide ads and more …

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

craigwillis85

November 25, 2016
Tweet

More Decks by craigwillis85

Other Decks in Programming

Transcript

  1. Logging • What is logging? • Why do we log?

    • What do we log? • Logging in a WordPress world
  2. 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
  3. What to log? When Where Who What - Date /

    Time - Identifier (line number, file, name, protocol) - IP address / user - Type, log level, description
  4. 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
  5. 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”); }
  6. 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"); } }
  7. 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"); } } }
  8. 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");
  9. 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 [] []
  10. 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
  11. Monitoring • Why do we monitor? • What do we

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

    SLA / USP Application Process Management (APM)
  13. 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(‘[email protected]’, ‘Site Down’, ‘Website is down!’); }
  14. 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
  15. 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/