Viewing and examining 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. The ErrorLog by default will contain the PHP errors your site produce, but also more general errors caused by your application.

CLI commands used to view and examine log files

Down below you can find some examples on how you can view and examine the AccessLog and ErrorLog.

cat

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

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

zcat

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

Print contents of gzip file to console in the directory you are in, 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, 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. 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

Give us your feedback on this article