How to View and Examine Log Files

The Apache webserver stores its logs in the ~/logs folder which you can find in the root of your site when you log in with SFTP or SSH. For commands used in this article, SSH is the recommended choice. The AccessLog contains all requests that are passed back from the front-end webserver (nginx), typically the traffic of your site.

CLI commands used to view and examine log files

Below you can find some examples of how you can view and examine the AccessLog and ErrorLog.

cat

cat [filename]  For manual, use: cat --help

Print the contents of a file to the console in the directory you are in, for example: cat ~/logs/AccessLog, or cat ~/logs/ErrorLog

zcat

zcat [filename]  For manual, use: zcat --help

Print contents of the gzip file to the console in the directory you are in, for example: zcat ~/logs/AccessLog-20211201.gz, or zcat ~/logs/ErrorLog-20211201.gz

tail

tail -f [filename] For manual, use: tail --help

The following command will display log entries as they are added in real-time for all files ending in “Log”:

tail -f ~/logs/*Log

You can also specify AccessLog or ErrorLog to be followed in real-time, for example: tail -f ~/logs/AccessLog, or tail -f ~/logs/ErrorLog

grep

grep ‘word’ [filename] For manual, use: grep --help 

Grep is used to search for strings and print the lines that match it. Example where we search for status codes of 200 in the AccessLog: cat ~/logs/AccessLog | grep " 200 ". We can also use grep in real-time with the same example by using tail: tail -f ~/logs/AccessLog | grep " 200 "

In these examples, we use pipe ( | ) in combination with grep. Pipe will use two or more commands to output from the left of it, and input it to the right.

wc -l

wc -l [filename] For manual, use: wc --help

You can also use wc -l to print the number of lines present in a file, or that match the search using grep. For example where we search for status codes of 200 in the AccessLog and print the number of lines that match the search: cat ~/logs/AccessLog | grep " 200 " | wc -l