答案1
@flocki 感谢您的精彩回答!下面是一个稍微更完整的示例:
[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
前缀日志:
#!/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 "$@"
答案2
我最近遇到了同样的问题,但无法弄清楚supervisor-stdout
。以下是我所做的:
在 bash 中你可以使用I/O 重定向和流程替代通过另一个过程进行管道传输stdout
。stderr
#!/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
现在您可以设置 Supervisor 使用 stdout 和 stderr 进行日志记录:
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0
并作为命令使用设置 I/O 重定向的 bash 脚本。