xavier-hernandez / goaccess-for-nginxproxymanager

GoAccess Docker Image for Nginx Proxy Manager and more...
https://hub.docker.com/r/xavierh/goaccess-for-nginxproxymanager
MIT License
532 stars 39 forks source link

Allow custom log format to be set through environmental variables #165

Closed yyrliu closed 1 year ago

yyrliu commented 1 year ago

I'm working on using this project to monitor traefik with multiple subdomains (virtual host field in goaccess), which is only recorded when logging access log in json. Therefore it will be great if I can customize the log-format, including time-format and date-format.

I simply made some small changes to resources/scripts/logs/traefik.sh to allow user to override the formats with -e TIME_FORMAT, -e DATE_FORMAT and -e LOG_FORMAT. However someone may come up with some more elegant solution, or just simply create a new -e LOG_TYPE=TRAEFIK

@@ -32,9 +32,9 @@ function traefik_goaccess_config(){
     echo "# ${goan_version}" >> ${goan_config}
     echo "# GOAN_PROXY_CONFIG" >> ${goan_config}
     echo "######################################" >> ${goan_config}
-    echo "time-format %T" >> ${goan_config}
-    echo "date-format %d/%b/%Y" >> ${goan_config}
-    echo "log-format %h %^[%d:%t %^] \"%r\" %s %b \"%R\" \"%u\" %Lm" >> ${goan_config}
+    echo "time-format ${TIME_FORMAT:-'%T'}" >> ${goan_config}
+    echo "date-format ${DATE_FORMAT:-'%d/%b/%Y'}" >> ${goan_config}
+    echo "log-format ${LOG_FORMAT:-'%h %^[%d:%t %^] \"%r\" %s %b \"%R\" \"%u\" %Lm'}" >> ${goan_config}
     echo "port 7890" >> ${goan_config}    
     echo "real-time-html true" >> ${goan_config}
     echo "output ${nginx_html}" >> ${goan_config}

Just in case if anyone is interested, my configs are the followings

# goaccess-for-nginxproxymanager envs

TIME_FORMAT=%H:%M:%S
DATE_FORMAT=%Y-%m-%d
LOG_FORMAT='{"ClientHost": "%h", "ClientUsername": "%e", "DownstreamContentSize": "%b", "Duration": "%n", "DownstreamStatus": "%s", "RequestAddr": "%v", "RequestMethod": "%m", "RequestPath": "%U", "RequestProtocol": "%H", "StartUTC": "%dT%t.%^", "request_User-Agent": "%u"}'
# traefik access log config

--accesslog=true
--accesslog.filepath=/log/access.log
--accesslog.bufferingsize=100
--accesslog.format=json
--accesslog.fields.defaultmode=keep
--accesslog.fields.headers.defaultmode=drop
--accesslog.fields.headers.names.User-Agent=keep
--accesslog.fields.headers.names.Content-Type=keep
xavier-hernandez commented 1 year ago

@stultusphobia this was incorporated into the new version.