How to Redirect Apache Logs to both STDOUT and Apache Log File
Asked Answered
C

3

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!

Cosher answered 27/2, 2017 at 10:56 Comment(0)
L
6

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".)

Leanto answered 27/2, 2017 at 11:8 Comment(3)
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. – Tartuffe
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. πŸŽ‰ – Northing
@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 – Berget
N
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
Northing answered 28/9, 2021 at 13:44 Comment(0)
B
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

Berget answered 24/9, 2021 at 12:40 Comment(0)

© 2022 - 2024 β€” McMap. All rights reserved.