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. log($this)
    $monitor->that()
    24/11/16
    WordPress London

    View Slide

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

    View Slide

  3. Logging
    ● What is logging?
    ● Why do we log?
    ● What do we log?
    ● Logging in a WordPress world

    View Slide

  4. What is logging?

    View Slide

  5. Types of logs
    Application ● Error / Exception
    ● Access logs
    Infrastructure
    ● Server logs
    ● Database logs
    ● Mail logs
    Issue No. URLs
    The 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 tags 85
    The tag is missing 38
    Logging and Monitoring
    Example of logging

    View Slide

  6. Why do we log?

    View Slide

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

    View Slide

  8. What do we log?

    View Slide

  9. EVERYTHING

    View Slide

  10. What to log?
    Information critical to you
    Authentication / Authorization
    APIs requests/responses
    Shopping Carts

    View Slide

  11. What to log?
    When
    Where
    Who
    What
    - Date / Time
    - Identifier (line number, file, name, protocol)
    - IP address / user
    - Type, log level, description

    View Slide

  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

    View Slide

  13. Examples

    View Slide

  14. Basic Example
    try {
    something()
    } catch($e Exception) {
    error_log(“We have an error”);
    }
    try {
    doSomething()
    } catch($e Exception) {
    error_log(“We have an error”);
    }

    View Slide

  15. tail -f error.log

    View Slide

  16. Monolog

    View Slide

  17. composer require monolog/monolog

    View Slide

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

    View Slide

  19. Monolog
    Logging made easy
    Handlers
    Formatters
    Processors

    View Slide

  20. API Example
    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");
    }
    }

    View Slide

  21. API Example - Logger
    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");
    }
    }
    }

    View Slide

  22. API Example - Test File
    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");

    View Slide

  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 [] []

    View Slide

  24. Logging in a WordPress world

    View Slide

  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

    View Slide

  26. Real-world logging
    Graph
    Graph
    Visibility
    ● Easy to access
    ● Easy to read
    ● Easy to find problems

    View Slide

  27. Monitoring

    View Slide

  28. Monitoring
    ● Why do we monitor?
    ● What do we monitor?
    ● Real-world examples

    View Slide

  29. Why do
    monitoring?
    Ensuring systems run as expected
    Automated
    Measurable
    SLA / USP
    Application Process Management (APM)

    View Slide

  30. What do we
    monitor?
    Web / Database servers
    Web application endpoints
    External APIs
    Your users!

    View Slide

  31. Real-world examples

    View Slide

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

    View Slide

  33. cronjob example
    // 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!’);
    }

    View Slide

  34. Web server status example
    ExtendedStatus on

    SetHandler server-status
    AuthType basic
    AuthName "Apache status"
    AuthUserFile /etc/httpd/conf/htpasswd
    Require valid-user

    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

    View Slide

  35. New Relic
    Server / Application / Infrastructure Monitoring

    View Slide

  36. New Relic
    Server Monitoring

    View Slide

  37. New Relic
    Application Monitoring

    View Slide

  38. New Relic
    Alerts
    Slack

    View Slide

  39. Monit

    View Slide

  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/

    View Slide

  41. Thank you!

    View Slide

  42. Questions?

    View Slide

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

    View Slide