Using Tail to Debug Drupal Sites

Drupalize.Me Tutorial

Tail is command for Unix and Unix-like systems (like OS X) that allows you to take a peek at the contents of the end of a file. From the manual page: "tail - output the last part of files." Tail can be really useful for debugging purposes, or for taking a look at the recent access logs from your Drupal setup. Tail can be particularly useful in a production environment when you may not have PHP error reporting enabled, and need to find the cause of serious errors with your Drupal site. But this isn't just for Drupal sites. You can use Tail on any text file on your system.

This tutorial is based on the free Tail - Command Line Debug video from our Command Line Basics series.

Prerequisites

  • You know how to access a Drupal site via the command line (using Terminal or similar program). It can be installed either on a local site or a remote server that you know how to access via ssh.
  • You know which web server you're running Drupal on.

Assumptions

If you are running Apache as a web server on Debian or Ubuntu, or OS X, your log files will likely be located in the /var/log/apache2/ directory, and their names will likely be: access.log and error.log, although depending on your system configuration, this may be different. For example, if you are running MAMP, your log files will be located in /Applications/MAMP/logs.

If you're not running one of the operating systems I just mentioned, or Apache, check your web server's documentation for your OS to determine your default log location.

For the purpose of this tutorial, we will be assuming that the access.log and error.log are located in /var/log/apache2. As mentioned above, this may be different on your system.

Running the Tail Command

Here's an example of how to use the tail command to view the last bit of Apache's access log:

tail /var/log/apache2/access.log

Running this command will print the last 10 lines from your access log. This can be useful if you want to see the most recent requests to your Drupal site.

Tail Command Options

Access the Manual

You can type man tail in a terminal window to open the manual (or "man" for short) for the Tail command. The Tail manual lists all of the options you can pass to the command, so it's definitely useful for when you want to learn about what Tail can do or recall the name of an option that you learned about previously.

Increase the Number of Lines

Let's say you want to look at more than the last 50 lines of the file you're tailing, you can you use the -n flag, and pass the desired number of lines.

tail -n 50 /var/log/apache2/access.log

Real Time Access Log

The flag I use most often is -f, or --follow.

This means you can watch your access.log, or error.log in real time. First, Tail still prints the first 10 lines from the file. Then, as access to the site continues, it adds more lines to the output and automatically scrolls, so that you can watch the latest access hits as they occur.

  1. Open up a web browser, and a terminal window, side by side.
  2. In the terminal window, watch your access.log with:
    tail -f /var/log/apache2/access.log
  3. In your web browser, visit your Drupal site, and watch the requests being printed to your terminal by Tail.

Tail Multiple Files

You can also tail multiple files at the same time. For example:

tail -f /var/log/apache2/access.log /var/log/apache2/error.log

This will continue to scroll output from both files.

Investigating Drupal Site Errors with Tail

When creating or updating a module, or Drupal core, and your page loads, and nothing...the dreaded White Screen of Death, also known as WSOD. This is where Tail comes to the rescue! In most instances of a WSOD, you're actually running into a PHP or server configuration problem. This will probably be located in your web server's error log.

Run the following command (modifying the location of the error log if necessary):

tail -f /var/log/apache2/error.log

You might see something like this:

This is most likely an issue with your php configuration's (php.ini) max_memory setting. You may see something totally different, try a search with your favorite search engine, it's likely someone has run in to a similar problem before.

Tail is a great tool for reading the end of files. We concentrated on web server logs today, but you could use Tail to examine the end of any text-based file.

Related Topics: 

Comments

Drush has a tail option. I use `drush ws --tail` to print the output of the watchdog log. Super handy.

That is super handy indeed, thanks Kepford!

You can also pipe tail to grep, if you want to check, say, certain pages, or other things in the log:

tail -f /var/log/apache2/access.log | grep GoogleBot

will show only pages hit by the GoogleBot. Very handy.

psteve, thanks! I'm a big grep fan, I love the '-i' flag, which means case-insensitive :)

tail -f /var/log/apache2/access.log | grep -i somestring

Would return instances of soMesTring, somestring, SOMEstring, etc :)

Add new comment