How to create custom logs in Apache

- Andrés Cruz

En español

How to create custom logs in Apache

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 StringDescription
%hIt is the host that accesses the server.
%lIt is the RFC 1413 User Identification Protocol.
%uIt is the name of the user (usually the output will be a hyphen unless it is an authenticated user in the system).
%tIt is the date including time and UTC.
%rIt is the request made by the user.
%>sIt is the HTTP protocol status response code.
%{Referer}iIt is the reference URI to the resource.
%{User-agent}iIt 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 StringDescription
%%It's the percent sign.
%aIt is the remote IP address.
%AIt is the local IP address.
%BIt is the Size of the "response" (response) in bytes including the HTTP header.
%bIt is the Size of the "response" (response) in bytes including the HTTP header, in CLF format.
%{VARNAME}CIt is the content of the cookie in the request sent to the server.
%DIt is the time taken by the server to give a response.
%{VARNAME}eGives us the content of the variable.
%fIt is the name of the file.
%hIt is the Remote Host.
%HIt's the protocol.
%kIt is the number of "keepalive" handled in the connection.
%mIt is the request method.
%pIt is the canonical port of the request service server.
%qIt is the query string.
%uIt is the username.
%UIt 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
Andrés Cruz

Develop with Laravel, Django, Flask, CodeIgniter, HTML5, CSS3, MySQL, JavaScript, Vue, Android, iOS, Flutter

Andrés Cruz In Udemy

I agree to receive announcements of interest about this Blog.

!Courses from!

10$

On Udemy

There are 4d 09:47!


Udemy

!Courses from!

4$

In Academy

View courses

!Books from!

1$

See the books
¡Become an affiliate on Gumroad!