13

I've just installed haproxy on my test server.

Is there a way of making it write its logs to a local file, rather than syslog?

This is only for testing so I don't want to start opening ports / cluttering up syslog with all my test data.

Unfortunately, the only information I can find all revolves around logging to a syslog server.

I tried using:

log /home/user/ha.log local0

in my config. But that told me:

[ALERT] 039/095022 (9528) : sendto logger #1 failed: No such file or directory (errno=2)

When I restarted. So I created the file with touch /home/user/ha.log and restarted at which point I got:

[ALERT] 039/095055 (9593) : sendto logger #1 failed: Connection refused (errno=111)

Is this possible, or am I going to have to configure syslog etc. to see my test data?

  • 2
    I don't think HAProxy can log to a file, and I suspect the reason for this is that writes to disk are a blocking operation. Why do you really not want to use syslog? Config is not all that tricky. You can assign a local facility to HAProxy and configure your syslog daemon to write those entries to a different file, and not to other syslog files (or network streams), if you're not wanting the HAProxy logs not to be mixed in with everything else. – Michael - sqlbot Feb 9 '16 at 12:23
13

Haproxy simply doesn't support logging to files. As stated in the documentation (https://cbonte.github.io/haproxy-dconv/1.8/configuration.html#3.1-log), the "log" statement takes as first parameter an address. If that's a file, it's a unix socket and HAProxy will speak in the syslog format to this socket. Haproxy is designed like this because its responsability is to proxy requests, not write files, it delegates writing of log files to syslog. If you don't want to mess with your machine, you can for example install logstash and run: logstash -e 'input { unix { path => "/tmp/haprxoy_log.sock" } } output { stdout { } }' and add: log /tmp/haprxoy_log.sock In your haproxy.cfg to test it.

6

You can change haproxy log config file under /etc/rsyslog.d/ to point the file to the path you like. Not sure about other distribution. For me, I'm using Debian. I changed /etc/rsyslog.d/49-haproxy.conf to point to /var/log/haproxy/haproxy.log,

# Create an additional socket in haproxy's chroot in order to allow logging via
# /dev/log to chroot'ed HAProxy processes
$AddUnixListenSocket /var/lib/haproxy/dev/log

# Send HAProxy messages to a dedicated logfile
if $programname startswith 'haproxy' then /var/log/haproxy/haproxy.log
&~

You should also update the log path in /etc/logrotate.d/haproxy to the new path. So that it will rotate and compress logfile in newly configured path using gzip.

Then restart rsyslog.service.

sudo systemctl restart rsyslog.service

Now haproxy.log.* file will be in /var/log/haproxy/ directory.

0

You have tried to configured in the user home directory, haproxy doesn't have permission to access user's home directory, so that cause issue.

Instead try in other location, create directory in /var/<directory>

log /var/<directory>/ha.log
  • 2
    I tried sudo touch /var/log/ha.log and added log /var/log/ha.log local0 to my config, but got the same error as above connection refused. – IGGt Feb 9 '16 at 10:56
  • In log why you giving local0 in the end? – KKD Feb 9 '16 at 11:24
  • 1
    I tried leaving it out, but when I restarted I got the error 'log' expects <address> and <facility> as arguments. so required something there. According to the instructions <facility> must be one of the 24 standard syslog facilities (which I think may be part of the problem) – IGGt Feb 9 '16 at 11:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.