Introduction
In this tutorial, you will learn everything you need to know about Apache logging to help you troubleshoot and quickly resolve any problem you may encounter on your server. Logging is a very powerful tool that will give you valuable data about all the operations of your servers. You will learn where logs are stored, how to access them, and how to customize log output to fit your needs.
Prerequisites
Apache web server.
Sudo privileges.
Step 1 — Getting To Know Apache Log Types
Apache writes logs of its events in two different log files.
Access Log - In this file, Apache stores information about incoming requests.
Error Log - This file contains information about errors that the web server encountered while processing requests.
Step 2 — Locating Apache Log Files
The location of the access log file is dependent upon the operating system on which is Apache web server running.
Location Of The Access Log
On Debian-based operating systems like Ubuntu, the access log file is located /var/log/apache2/access.log
On CentOS, the access log file is stored in /var/log/httpd/access.log
A typical access log entry might look like this:
Output:
::1 - - [13/Nov/2020:11:32:22 +0100] "GET / HTTP/1.1" 200 327 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36"
Location Of The Error Log
On Debian-based operating systems like Ubuntu, the access log file is located /var/log/apache2/error.log
On CentOS, the access log file is stored in /var/log/httpd/error.log
A typical error log entry might look like this:
Output:
[Thu May 06 12:03:28.470305 2021] [php7:error] [pid 731] [client ::1:51092] script '/var/www/html/missing.php' not found or unable to stat
Step 3 — Viewing Apache Logs
If you are working from an operating system with the UI, the easiest way to view stored logs is by opening files in the text editor. However, sometimes you need to view the content of the files directly in the terminal. In this case, there are few ways to do it.
You can tail command to view logs in real time:
tail -f /var/log/apache2/access.log
The tail command is used to print the last 10 lines from the selected file. With the -f option, the tail command will be viewing the content of the file in real-time.
To view the full content of the file, you can use the cat command:
cat /var/log/apache2/access.log
You may also want to find a specific term in the file. In that case, you can use the grep command:
grep GET /var/log/apache2/access.log
First, specify the term you want to search for, then specify the actual log file. In this case, we are looking for lines in the access log file where GET therm is present.
Step 4 — Configuring Apache Access Logs
In the access log, you can see what pages are users visiting, the status of their requests, and how long it took to process their requests.
Log Formats
As was mentioned earlier, logs are a powerful tool. To be able to use this tool you need to understand the format in which are logs stored. The format of the access logs and the log file location is defined in the CustomLog directive. This directive can be used in the server configuration file (/etc/apache2/apache2.conf) or your virtual host entry. Be aware that defining the same CustomLog directive in both files may cause problems.
Common Log Format
The common log format is the standardized text file format used by many web servers. It's popular as it is easy to read and contains just the necessary information. Its defined in the /etc/apache2/apache2.conf configuration file and its format look like this:
LogFormat "%h %l %u %t \\"%r\\" %>s %O" common
The entry in the log file will look like this:
Output:
127.0.0.1 alice Alice [06/May/2021:11:26:42 +0200] "GET / HTTP/1.1" 200 3477
This is the information that the log message contains:
%h - 127.0.0.1 - Hostname or IP address of the client that made the request
%l - alice - Remote log name (Name used to log in a user). If not set, the default value will be used -
%u - Alice - Remote username (Username of logged-in user). If not set, the default value will be used -
%t - [06/May/2021:11:26:42 +0200] - Day and time of the request
\"%r\" - "GET / HTTP/1.1" - Actual request
%>s - 200 - Response code
%O - 3477 - Size of the response in bytes
Combined Log Format
The combined log format is very similar to the common log format but contains few extra pieces of information.
Its defined in the /etc/apache2/apache2.conf configuration file and its format look like this:
LogFormat "%h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combined
The entry in the log file will look like this:
Output:
127.0.0.1 alice Alice [06/May/2021:11:18:36 +0200] "GET / HTTP/1.1" 200 3477 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36"
These are the extra pieces of information (aside from those present in the common format):
\"%{Referer}i\" - "-" - URL of the referer
\"%{User-Agent}i\" - "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36" - Detailed information about he browser of the user that made the request.
Custom Log Format
You can define your custom log format in the /etc/apache2/apache2.conf using LogFormat directive followed by the actual format of the output and nickname that will be used as format identifier.
For this example, we will create a custom log format named custom that will only print the user's browser information. The format will look like this:
LogFormat "%{User-agent}i" custom
In the virtual host file, we will use the CustomLog directive to set the format of the log messages to the custom and log file to the default access log.
CustomLog ${APACHE_LOG_DIR}/access.log custom
Now, we make a request and the Apache server will log the information about the browser that made the request into the access.log file. The log message will look like this:
Output:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36
Logging Into Multiple Files
You can also write multiple messages into multiple files. This can be done by using the CustomLog directive more than once. Note that when logging into the custom log file, the log file has to be manually created before you can log into it.
CustomLog ${APACHE_LOG_DIR}/custom.log custom
CustomLog ${APACHE_LOG_DIR}/access.log common
Step 5 — Configuring Apache Error Logs
The error log contains information about the errors the web server encountered while processing the request. A common error while processing the request is a request for a missing file.
You can choose to which file the error messages will be stored using the ErrorLog directive in your virtual host configuration file. This directive takes one argument - path to the log file. Here is an example from default virtual host configuration file /etc/apache2/sites-available/000-default.conf
ErrorLog ${APACHE_LOG_DIR}/error.log
You can choose a custom file but be aware as the file has to be manually created before you can log into it.
In the virtual host configuration file, you can also specify the level of errors that will be logged using the LogLevel directive. Setting this option to a specific value, the server will ignore errors with lover severity then set in the LogLevel directive. It is not recommended to change it to higher values than error.
These are the possible values:
trace1 - trace8 - Trace messages (LOWEST)
debug - messages used for debugging
info - informational messages
notice - notices
warn - warnings
error - errors while processing the request (doesn't require immediate action)
crit - Critical error that requires prompt action
alert - Error that requires immediate action
emerg - System is unusable
You can set the log level using the LogLevel directive like this:
LogLevel info
If the log level is not set, the server will set the log level to warn by default.
Conclusion
In this tutorial, you learned what types of log Apache web server stores, where you can find those logs, how to understand the formatting, and how to create your custom log formats. Now, you can log into multiple files and set the level of errors to which the server will react. At this point, you know everything you need to efficiently debug your web application.
You can explore more on linux logging in logtail tutorial library.
Top comments (0)