Many younger developers don’t have an opportunity to use log files, especially in situations where they have installed their web server on their local computer and don’t need to worry about debugging their site on the fly.

Once a developer steps up to more critical projects, like those in an Enterprise environment, debugging production issues means dealing with log files.

In this article, I will give some Solid tips for working with web server logs in a Bash (Unix) shell command line, covering the basics to make your development and debugging easier.

Where are the log files?

The majority of people who will benefit from this article will find themselves using Apache server on Linux. It’s used everywhere in the business world and even with dirt-cheap hosting from GoDaddy.

For Apache/Linux, you will almost certainly find your logs under /var/log. If you happen to be running Tomcat server, you will find your logs under /usr/share/tomcat/logs, usr/share/logs, or opt/tomcat/logs. For Windows IIS server, you can find logs under c:\winnt\system32\LogFiles, but then again this article won’t be of any use to you.

You can go directly to your logs in a shell by using the change directory command:

cd /var/logs

Solid Tip: These are the default log locations. The locations or file names might be changed, so watch out. Additionally, custom logs might be created and intercept the data you expect to be elsewhere. If your server is part of shared hosting, your logs will likely be directed into some sort of home folder.

How can I view log files?

Thankfully, you have plenty of options for viewing logs. Sometimes log files can get very large, making simply downloading/opening them futile. You can use the head or tail commands to view the beginning or tail end of the file respectively. You can watch the log entries on-the-fly as they come in by using the tail command with a follow flag as shown below:

tail -f your_log_file.log

The previous command will show a rolling tail-end of the log file. It shows 10 lines by default, but you can also specify the number of lines, like tail -f -25 php_error_log.

My logs are huge! How do I truncate a log?

As said above, logs can become very large, becoming Gigabytes if not pruned. Additionally, you might encounter issues trying to clear the file out because it is in use by the server. You can easily truncate giant log files with a greater-than arrow:

> your_log_file.log

This command also works while the log is running. The arrow is a “redirect” command that puts the stuff output on its left into the file on its right.

Solid Tip: Other methods include a truncate command or cat /dev/null > your_log_file.log. The arrow syntax is simply the shortest way of handling it (plus the cat command with one file is frowned upon as “cat abuse”).

How do I search a log for text?

You can search an entire log file for a string of text, but it could take a long time. You can combine a search in a tail (described up above) and pluck a desired entry out right as it enters the log:

tail -f your_log_file.log | grep -i "search string"

If you want to search the entire log, simply use the find command:

find "search string" your_log_file.log

Count how often something appears in a log

If you need to know how often an error or other log entry is occuring, the following will return the number of lines that a string appears:

grep -c "search string" your_log_file.log

Hopefully these simple tips will aid you in debugging and understanding your web applications. Of course, if you want to know how to do something else or have a better way of doing something, let me know!