• Keeping a Live Eye on Logs

    Oct 22 2008

    In web development, you can get really used to getting good error messages. If you're coding in PHP, you come to rely on the web server telling you if a variable is undefined or a file is missing.

    Likewise, when you're debugging, sometimes the easiest sanity check is to output some text. You'll find me using echo "wtf?"; die; quite often when I'm trying to figure out why a block of code isn't being executed.

    But sometimes, even these rudimentary methods of developing aren't available to you. Maybe error messages are turned off on the web server you're stuck debugging. Or maybe you have a lot of Ajax requests flying around and are tired of peeking at the result of each request.

    Log files to the rescue. Nearly every framework and server has logging functionality. Apache usually logs every request, so you can see when images, static files, and other requests are made. CodeIgniter has several levels of logging, including DEBUG, ERROR and INFO. Whatever framework you use, or if you're using one you built yourself, you'll probably find some way of writing log messages to a file.

    And if you have a log file that's being updated, you have a way of watching things happen in real time. I'm not talking about opening up the log file every now and then and looking back through it, I'm talking about watching it being updated as it happens.

    Let's say you have a web server running Linux and are trying to debug something using CodeIgniter. You SSH into the web server, find the log file, and tail -f it. tail is a command which will dump the end of a file out for you. If you add the -f parameter, it will "follow" the file, printing out any new lines as they are added to the log file.

    $ cd thefutureoftheweb.com/system/logs
    $ tail -f log-2008-10-23.php
    

    If you're working locally on Windows, make sure you have Cygwin installed. (Cygwin gives you a Linux-style console in Windows so you can do awesome stuff like this.) If so, you should have or be able to use tail in the exact same way. Mac and Linux users should have tail installed already.

    Back at my old job, we used to keep console windows open using tail to watch the live web servers, especially after an update. This way we could see errors appearing to users live and could investigate and solve problems that would otherwise a) get buried in the logs, or b) go completely unnoticed.

    It can really help to get in the groove of having one or two terminal windows open tailing the logs of the servers you're currently working on. And then if you get the urge to write echo "wtf?"; die; you can instead write log_message('info', 'wtf?'); (or equivalent in your framework of choice).

    For further information, check out:

    Happy tailing!

  • Comments

    1. Oliver at 9:31pm on October 23, 2008

    Spam alert!
    I'm a fan of 'wtf' exit(0);

    2. Bob at 7:38pm on October 27, 2008

    wtf($this);

    function wtf($var)
    {
        die('WTF?<pre>'. print_r($this, 1). '</pre>');
    }

    3. Gabriel at 2:05am on August 18, 2010

    If you have many log messages whizzing past you it's easy to miss the one your are looking for. If you want to only display the message you are looking for, use the following command :

    tail -f log-2008-10-23.php | grep 'wtf'

    Replace 'wtf' with the string you are looking for. If you know regular expressions, you can use them with grep -E

    Commenting is now closed. Come find me on Twitter.