module(
  load="imuxsock"
  SysSock.Name="{{ configuration['log-socket'] }}")

# Just simply output the raw line without any additional information, as
# haproxy emits enough information by itself
# Also cut out first empty space in msg, which is related to rsyslogd
# internal and end up cutting on 8k, as it's default of $MaxMessageSize
template(name="rawoutput" type="string" string="%msg:2:8192%\n")
$ActionFileDefaultTemplate rawoutput

$FileCreateMode 0600
$DirCreateMode 0700
$Umask 0022

$WorkDirectory {{ configuration['spool-directory'] }}

# Setup logging per slave, by extracting the slave name from the log stream
{%- set regex = "^\\\\s*\\\\{(\\\\S.*)-https{0,1}\\\\} (.*)!!!(.*)" %}
# Extract file name part from 1st match
template(name="extract_slave_name" type="string" string="%msg:R,ERE,1,FIELD:{{ regex }}--end%")
set $!slave_name = exec_template("extract_slave_name");
# Frontend output - the HAProxy's like httplog
template(name="frontend_output" type="string" string="{{ configuration['slave-log-directory'] }}/%$!slave_name%_frontend_log")
template(name="haproxy_frontend_line" type="string" string="%msg:R,ERE,2,FIELD:{{ regex }}--end%\n")
# Access output - the Apache's CLF
template(name="access_output" type="string" string="{{ configuration['slave-log-directory'] }}/%$!slave_name%_access_log")
template(name="haproxy_access_line" type="string" string="%msg:R,ERE,3,FIELD:{{ regex }}--end%\n")
# React on match
if (re_match($msg, '{{ regex }}')) then {
 action(type="omfile" dynaFile="frontend_output" template="haproxy_frontend_line")
 action(type="omfile" dynaFile="access_output" template="haproxy_access_line")
 stop
}

{#- emit all not catched messages to full log file #}
*.* {{ configuration['log-file'] }}
