Writing Apache2 Logs to stdout/stderr?

Solution 1:

  ErrorLog /dev/stderr
  TransferLog /dev/stdout

works on ubuntu and centos fo me

Solution 2:

How about placing this in your Dockerfile after the apache2 package is installed?

RUN ln -sf /proc/self/fd/1 /var/log/apache2/access.log && \
    ln -sf /proc/self/fd/1 /var/log/apache2/error.log

Assuming that this is the path of the logs. It is for Ubuntu 14.04 and also works for Ubuntu 16.04.

Note: if you're certain that the symbolic links /dev/stdout or /proc/stderr are there, then you may also use those. I prefer the path to the real file as this is guaranteed present.

Solution 3:

Not specifically an answer asked for but maybe a better way, depending on your scenario, would be to to not log to stdout/stderr at all. Just pipe the logs to cat in a JSON format. This would remove the need to differentiate the streams as the json could have the data needed in it to distinguish them. eg something along the lines of the following. This can then be ingested much more easily into something like graylog

GlobalLog "| cat - " gelf
ErrorLog "| cat - " 

LogFormat "{ \"apache_log\": \"ACCESS\", \"app_name\": \"apache\",  \"Connection\": \"%{X-Forwarded-Proto}i:%{X-Forwarded-Port}i \", \"X-Forwarded-For\": \"%{X-Forwarded-For}i\",  \"version\": \"1.1\", \"vhost\": \"%V\", \"short_message\": \"%r\", \"timestamp\": %{%s}t, \"level\": 6, \"user_agent\": \"%{User-Agent}i\", \"source_ip\": \"%a\", \"duration_usec\": %D, \"duration_sec\": %T, \"request_size_byte\": %O, \"http_status\": %s, \"http_request_path\": \"%U\", \"http_request\": \"%U%q\", \"http_method\": \"%m\", \"http_referer\": \"%{Referer}i\", \"X-Powered-By\": \"%{X-Powered-By}i\" }" gelf

ErrorLogFormat "{ \"app_name\": \"apache\",  \"apache_log\": \"ERROR\", \"time\":\"%{%Y-%m-%d}tT%{%T}t.%{msec_frac}tZ\", \"function\" : \"[%-m:%l]\" , \"process\" : \" [pid %P:tid %T] \" , \"message\" : \"%M\" ,\ \"referer\"\ : \" %{Referer}i \" }"

There is also a gelf logging module, so you can stream direct from apache to a graylog type server as well if you wish