5

I am running a docker container and want to write logs of my apache server to both STDOUT and file.

Any idea as to what kind of configuration is needed in my Apache httpd.conf file?

Any help would be highly appreciated!

Ritesh
  • 301
  • 1
  • 4
  • 11

3 Answers3

5

you can try this:

CustomLog "| /usr/bin/tee /var/log/access_log" common

from apache.org docs: Apache httpd is capable of writing error and access log files through a pipe to another process, rather than directly to a file. This capability dramatically increases the flexibility of logging, without adding code to the main server. In order to write logs to a pipe, simply replace the filename with the pipe character "|", followed by the name of the executable which should accept log entries on its standard input. Apache will start the piped-log process when the server starts, and will restart it if it crashes while the server is running. (This last feature is why we can refer to this technique as "reliable piped logging".)

Maoz Zadok
  • 4,871
  • 3
  • 33
  • 43
  • 5
    Additional note: You can define and use CustomLog more than once as you see fit in the same context, that is, you can use CustomLog to point to a file directly and another CustomLog directive with pipe and something else, etc. – Daniel Ferradal Feb 27 '17 at 12:19
  • 2
    Thank you @maoz-zadok! This did the trick for me after battling with this problem for an hour or two. I'm running a container with `2.4.43-alpine`, and I wanted to write the logs out both to stdout and to a file on a volume. I could write them to stdout with `CustomLog /proc/self/fd/1 common`, and to a file with `CustomLog /usr/local/apache2/logs/access.log common`, but `CustomLog "|/proc/self/fd/1 /usr/local/apache2/logs/access.log" common` caused the error `Permission denied: AH00104: unable to start piped log program`. Substituting `/usr/bin/tee` for `/proc/self/df/1` made it work. – And Finally May 02 '20 at 14:22
  • @AndFinally please add your comment as an answer so it helps others. I struggled for days and your comment was the only thing that worked for me – mnagdev Sep 24 '21 at 08:24
1

Slight tweak to @maoz-zadok's answer, which did the trick for me after battling with this problem for an hour or two. Thanks Maoz!

I'm running a container with 2.4.43-alpine, and I wanted to write the logs out both to stdout and to a file on a volume. I could write them to stdout with

CustomLog /proc/self/fd/1 common

and to a file with

CustomLog /usr/local/apache2/logs/access.log common

but

CustomLog "|/proc/self/fd/1 /usr/local/apache2/logs/access.log" common

caused the error Permission denied: AH00104: unable to start piped log program.

Substituting /usr/bin/tee for /proc/self/df/1 made it work, so for me the solution was

CustomLog "|/usr/bin/tee /usr/local/apache2/logs/access.log" common
And Finally
  • 5,602
  • 14
  • 70
  • 110
0

In case you're building a Docker image, put this in your Dockerfile after the apache package is installed

#This command is used to replace the default values for ErrorLog and CustomLog in the httpd.conf file with a pipe command to redirect logs to both file and console  
RUN sed -ri \
        -e 's!^(\s*CustomLog)\s+\S+!\1 "| /usr/bin/tee /var/log/access_log"!g' \
        -e 's!^(\s*ErrorLog)\s+\S+!\1  "| /usr/bin/tee /var/log/error_log"!g' \
        "/etc/httpd/conf/httpd.conf" 

Reference : https://github.com/docker-library/httpd/blob/master/2.4/Dockerfile

mnagdev
  • 384
  • 3
  • 11