# HAProxy Not Logging With Rsyslog

If HAProxy is not logging with `rsyslog`, it typically means that HAProxy logs are either not being generated or are not being forwarded correctly to `rsyslog`. Here’s a comprehensive guide to troubleshoot and resolve the issue:

### 1. **Verify HAProxy Logging Configuration**

Ensure that HAProxy is configured to send logs to the syslog.

### **1.1 Edit HAProxy Configuration**

1. **Open the HAProxy Configuration File**
    
    The main configuration file for HAProxy is usually located at `/etc/haproxy/haproxy.cfg`.
    
    ```bash
    sudo nano /etc/haproxy/haproxy.cfg
    ```
    
2. **Configure Logging Settings**
    
    Add or ensure the logging settings are present in the `global` section:
    
    ```
    global
        log /dev/log local0
        log /dev/log local1 notice
        chroot /var/lib/haproxy
        stats socket /var/run/haproxy.sock mode 660
        user haproxy
        group haproxy
        daemon
    
    ```
    
    Ensure that `log /dev/log local0` is configured to send logs to the local syslog (`/dev/log`).
    
    - `local0` and `local1` are facilities used to distinguish HAProxy logs from other logs.
3. **Save and Exit**
    
    Save the changes and exit the editor.
    
4. **Restart HAProxy**
    
    Restart HAProxy to apply the changes:
    
    ```bash
    sudo systemctl restart haproxy
    ```
    

### 2. **Verify `rsyslog` Configuration**

Ensure that `rsyslog` is configured to receive and handle logs from HAProxy.

### *2.1 Check `/etc/rsyslog.conf` or `/etc/rsyslog.d/`

1. **Open the Configuration File**
    
    Open the main `rsyslog` configuration file or any relevant file in `/etc/rsyslog.d/`.
    
    ```bash
    sudo nano /etc/rsyslog.conf
    ```
    
2. **Check for Log Reception**
    
    Ensure that `rsyslog` is set up to listen for log messages from HAProxy. You might have an entry like this to direct `local0` and `local1` logs to specific files:
    
    ```
    # Log HAProxy messages to /var/log/haproxy.log
    local0.*    /var/log/haproxy.log
    local1.*    /var/log/haproxy.log
    ```
    
    - This configuration directs logs from the `local0` and `local1` facilities to `/var/log/haproxy.log`.
3. **Save and Exit**
    
    Save the changes and exit the editor.
    
4. **Restart `rsyslog`**
    
    Restart `rsyslog` to apply the changes:
    
    ```bash
    sudo systemctl restart rsyslog
    ```
    

### 3. **Check for Log File Creation**

Ensure that the log files are being created and that `rsyslog` is writing logs to them.

### **3.1 Verify Log Files**

Check if `/var/log/haproxy.log` or any other configured log file is being created and populated:

```bash
ls -l /var/log/haproxy.log
```

If the file does not exist or is empty, it could indicate an issue with HAProxy or `rsyslog` configuration.

### 4. **Check Permissions**

Ensure that both HAProxy and `rsyslog` have the necessary permissions to write to the log files.

### **4.1 File Permissions**

Check the permissions of the log file:

```bash
ls -l /var/log/haproxy.log
```

Ensure that `rsyslog` has write permissions to this file. If not, adjust permissions accordingly:

```bash
sudo chown syslog:adm /var/log/haproxy.log
sudo chmod 640 /var/log/haproxy.log
```

### **4.2 Directory Permissions**

Ensure that the `/var/log` directory and any relevant subdirectories have appropriate permissions.

### 5. **Test Logging**

Generate some traffic to HAProxy and check if logs are being recorded.

### **5.1 Generate Traffic**

Use tools like `curl` or a web browser to generate traffic through HAProxy.

### **5.2 Check Logs**

Monitor the log file for new entries:

```bash
tail -f /var/log/haproxy.log
```

### 6. **Use `logger` Command for Testing**

You can use the `logger` command to test if `rsyslog` is correctly receiving and logging messages.

### **6.1 Test with `logger`**

Send a test message to `local0`:

```bash
logger -p local0.info "Test message for HAProxy logging"
```

Check if this message appears in `/var/log/haproxy.log`.

### 7. **Consult Logs for Errors**

Check `rsyslog` and HAProxy logs for any errors or warnings that might indicate problems with logging.

```bash
sudo tail -f /var/log/syslog
sudo tail -f /var/log/haproxy.log
```

### Summary

To resolve issues with HAProxy logging to `rsyslog`:

1. Verify HAProxy is configured to log to syslog.
2. Ensure `rsyslog` is set up to receive and write HAProxy logs.
3. Check log file creation and permissions.
4. Test logging to ensure proper configuration.