I'm using supervisor in docker images when I have to run multiple services in one image e.g. postfix and other mail services. When I redirect the stdout/stderr from all services to supervisor and supervisor does also log to stdout/stderr I would prefer to have a prefix/label infront of the actual log output on the console to know which log is coming from which service. I can't find any config setting for this but maybe you know a way.
Asked
Active
Viewed 2,598 times
11
-
2Try using supervisor-stdout in your supervisor.conf: https://github.com/coderanger/supervisor-stdout – Gustavo Kawamoto Jan 26 '17 at 01:06
-
@GustavoKawamoto http://meta.stackexchange.com/questions/8231/are-answers-that-just-contain-links-elsewhere-really-good-answers – chicks Feb 02 '17 at 14:05
2 Answers
7
@flocki Thanks for your great answer! Below a slightly more complete example:
[program:nginx]
command=/usr/local/bin/prefix-log /usr/sbin/nginx -g "daemon off; error_log /dev/stderr info;"
autostart=true
autorestart=true
priority=10
stdout_events_enabled=true
stderr_events_enabled=true
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0
stopsignal=QUIT
prefix-log:
#!/usr/bin/env bash
# setup fd-3 to point to the original stdout
exec 3>&1
# setup fd-4 to point to the original stderr
exec 4>&2
# get the prefix from SUPERVISOR_PROCESS_NAME environement variable
printf -v PREFIX "%-10.10s" ${SUPERVISOR_PROCESS_NAME}
# reassign stdout and stderr to a preprocessed and redirected to the original stdout/stderr (3 and 4) we have create eralier
exec 1> >( perl -ne '$| = 1; print "'"${PREFIX}"' | $_"' >&3)
exec 2> >( perl -ne '$| = 1; print "'"${PREFIX}"' | $_"' >&4)
# from here on everthing that outputs to stdout/stderr will be go through the perl script
exec "$@"

Pieter Vogelaar
- 171
- 1
- 4
-
Why setup additional FDs? wouldn't you get exactly the same results with just `exec 1> >( perl -ne '$| = 1; print "'"${PREFIX}"' | $_"' >&1)` ? – istepaniuk Jan 17 '23 at 19:51
4
I was having the same problem recently, and could not figure out supervisor-stdout
. Here is what I did:
In bash you can use I/O redirection and Process Substitution to pipe stdout
, stderr
through another process.
#!/usr/bin/env bash
# setup fd-3 to point to the original stdout
exec 3>&1
# setup fd-4 to point to the original stderr
exec 4>&2
# get the prefix from SUPERVISOR_PROCESS_NAME environement variable
printf -v PREFIX "%-10.10s" ${SUPERVISOR_PROCESS_NAME}
# reassign stdout and stderr to a preprocessed and redirected to the original stdout/stderr (3 and 4) we have create eralier
exec 1> >( perl -ne '$| = 1; print "'"${PREFIX}"' | $_"' >&3)
exec 2> >( perl -ne '$| = 1; print "'"${PREFIX}"' | $_"' >&4)
# from here on everthing that outputs to stdout/stderr will be go through the perl script
echo "I will be prefixed"
# don't forget to use exec
Now you can setup supervisor to use stdout and stderr for logging:
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0
and as command use a bash script that setups the I/O redirection.

derflocki
- 141
- 3