What are the logs?
Application logs are records generated by applications based on a series of events recorded at a particular time; they are used to record data such as:
- Application errors.
- Users who access the application.
- Time in a certain module of the application.
Among other parameters; the logs are important tools to detect possible problems, errors, bugs in the application.
The logs in Apache
Apache logs can be easily configured using the CustomLog directive within the VirtualHost using the following syntax:
CustomLog <path>/<file>.<extension> format
Where:
- <path>: It is the location of the <file>; that is, where the <file> is referenced.
- <file>: It is the name that you put to the log.
- <extension>: (Optional) Is the file extension.
- <format>: It is the configuration of the log which can be:
- combined: %h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-agent}i\"
- common: %h %l %u %t \"%r\" %>s %b
Where each of the "format string" means:
Format String | Description |
---|---|
%h | It is the host that accesses the server. |
%l | It is the RFC 1413 User Identification Protocol. |
%u | It is the name of the user (usually the output will be a hyphen unless it is an authenticated user in the system). |
%t | It is the date including time and UTC. |
%r | It is the request made by the user. |
%>s | It is the HTTP protocol status response code. |
%{Referer}i | It is the reference URI to the resource. |
%{User-agent}i | It is the user-agent of the user.Es el user-agent del usuario. |
The parameter table based on the official Apache documentation; mentioning some of the most used "Format String":
Format String | Description |
---|---|
%% | It's the percent sign. |
%a | It is the remote IP address. |
%A | It is the local IP address. |
%B | It is the Size of the "response" (response) in bytes including the HTTP header. |
%b | It is the Size of the "response" (response) in bytes including the HTTP header, in CLF format. |
%{VARNAME}C | It is the content of the cookie in the request sent to the server. |
%D | It is the time taken by the server to give a response. |
%{VARNAME}e | Gives us the content of the variable. |
%f | It is the name of the file. |
%h | It is the Remote Host. |
%H | It's the protocol. |
%k | It is the number of "keepalive" handled in the connection. |
%m | It is the request method. |
%p | It is the canonical port of the request service server. |
%q | It is the query string. |
%u | It is the username. |
%U | It is the request URL, it does not include any query string.Es la URL de petición, no incluye ningún query string. |
We can also create a custom log format using the "LogFormat" directive; as we will see in the next section.
Multiple access logs in Apache
Apache allows you to manage multiple access logs; to create a log from Apache, it is enough to specify as many CustomLog directives as logs are desired.
Creating a log with the CustomLog directive in Apache
For example, the following directives create three access logs.
- The first contains basic CLF information.
- The second contains information about the accessed resources and the user.
- the last "CustomLog" Shows the agent used.
LogFormat "%h %l %u %t \"%r\" %>s %b" common
CustomLog logs/access_log common
CustomLog logs/referer_log "%{Referer}i -> %U"
CustomLog logs/agent_log "%{User-agent}i"
Condition logs in Apache
It may be convenient to exclude information from the logs; We can easily do this using environment variables. first we must set the environment variable to indicate that the request meets certain conditions; This is usually accomplished with <SetEnvIf. So the env= clause of the CustomLog; is used to include or exclude requests that the environment variable is set.
Example Condition logs in Apache
SetEnvIf Remote_Addr "127\.0\.0\.1" dontlog
SetEnvIf Request_URI "^/robots\.txt$" dontlog
CustomLog logs/access_log common env=!dontlog
Another example consists of sending to a different log the requests of those who "speak English" to a different file.
SetEnvIf Accept-Language "en" english CustomLog logs/english_log common env=english
CustomLog logs/non_english_log common env=!english
Develop with Laravel, Django, Flask, CodeIgniter, HTML5, CSS3, MySQL, JavaScript, Vue, Android, iOS, Flutter