Configurations of logs in Apache2.4
Tags: cloud, productivity, tutorial, devops
Published: 2022-03-14 20:06:35

## Why monitor the logs? What types of log output does web-server give?



Apache has the in-built configuration for logging, logs are files that record and track everything the webserver processes, each request and response are logged, log files are customization, you can define your own log format and which application or services need to log. 
The records of all Apache events are placed in two different text files:

**Access Log:** This file stores and keep track of logs of information about incoming requests and outgoing responses.Some of the basic information about queries like latency, protocol,client-OS, time-date, HTTP Status, IP address of the client, and more we will cover in this lesson in detail.


**Error Log:** This file contains diagnostic information about any errors that were encountered while processing requests and responses of the active running web application.


## Apache2 logs basics and formats

On Debian-based operating systems like Ubuntu, the access log file is located in `/var/log/apache2/access.log`

On CentOS, RHEL, or Fedora, the access log file is stored in `/var/log/httpd/access_log`

You can view apache error.log and access.log with this command.
By default, apache2 logs are stored in 
`/var/apache2/` directory.

```
sudo tail -f /var/log/apache2/access.log
```

The Common Log Format is the standardized access log format used by many web servers because it is easy to read and understand. It is defined in the /etc/apache2/apache2.conf configuration file through the LogFormat directive.

```
sudo grep common /etc/apache2/apache2.conf
```

which has a pattern like this
```
LogFormat "%h %l %u %t \"%r\" %>s %O" common
```

## Setting-up and configuring Logging System

The logging of the webserver and the location to store the logs can be set globally or on a virtual host basis.

The `CustomLog` or `ErrorLog` directives are set in the main server context, the server writes all log messages to the same access and error log files. Otherwise, if the directives are placed inside a <VirtualHost> block, only the log messages for that virtual host are written to the specified file.'
Below are the messages that were written on apache2.conf file for the configuration of error.log file
You can see these configuration file by running this command:
```
sudo nano /etc/apache2/apache2.conf
```

> A snippet of apache2.conf
```
# ErrorLog: The location of the error log file.
# If you do not specify an ErrorLog directive within a <VirtualHost>
# container, error messages relating to that virtual host will be
# logged here.  If you *do* define an error logfile for a <VirtualHost>
# container, that host's errors will be logged there and not here.

ErrorLog ${APACHE_LOG_DIR}/error.log


# LogLevel: Control the severity of messages logged to the error_log.
# Available values: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the log level for particular modules, e.g.
# "LogLevel info ssl:warn"

LogLevel warn

```


The log directive set in the <VirtualHost> block overrides the one set in the server context.

Virtual hosts without CustomLog or ErrorLog directives will have their log messages written to the global server logs.


```
<VirtualHost *:80>
     ServerName example.com
     ServerAlias www.example.com
     ServerAdmin website@example.com
     DocumentRoot /var/www/example.com/public
     LogLevel warn
     ErrorLog /var/www/example.com/logs/error.log
     CustomLog /var/www/example.com/logs/access.log combined
</VirtualHost>
```