我们有一个在docker上运行的应用程序;当前,所有日志都已写入日志文件。我们想将日志移到stdout
。我们已经使用/dev/fd/1
将日志写入stdout
,并且正在运行。
App A具有以下日志:
nginx log
supervisor log
app-supervisor log
nginx config,这是更改:
access_log /dev/fd/1 main;
error_log /dev/fd/1 error;
在supervisor配置中,这是更改:
stdout_logfile=/dev/fd/1
示例主管:
[program:app]
command=/bin/bash -c "exec npm run start-generic"
directory=/app01
stdout_logfile=/dev/fd/1
stdout_logfile_maxbytes=0
autostart=true
autorestart=true
startretries=0
stopasgroup=true
[program:nginx]
command=/usr/sbin/nginx -g "daemon off;"
stdout_logfile=/dev/fd/1
stdout_logfile_maxbytes=0
现在的问题是所有日志都已写入stdout
。我想区分这些日志(即在它们来自的每行前放置一些标识符)。
类似以下内容:
[app] .....app log line1...
[app] .....app log line2...
[nginx] ....nginx log line1...
[app] .....app log line3...
[nginx] ....nginx log line2...
这不能完全回答问题,但我建议使用docker-compose。在docker-compose下将应用程序的所有组件创建为服务。现在,当您使用docker-compose up
启动它时,您不仅会获得根据服务标记的不同日志,而且还可以方便地以更简单的方式分别管理它们。
如果其他人正在寻找答案-
您可以使用use超级用户stdout事件监听器-
[supervisord]
nodaemon = true << make it true
[program:web]
command = ...
stdout_events_enabled = true. <<< add these lines in your each program config
stderr_events_enabled = true <<< add these lines in your each program config
[eventlistener:stdout]
command = supervisor_stdout
buffer_size = 100
events = PROCESS_LOG
result_handler = supervisor_stdout:event_handler
这会将程序名称附加在每一行的前面。但是使用此工具时我看到了日志重复。
或如果您不想使用事件侦听器并且不想更改代码,则可以使用tail程序跟踪日志并使用sed附加一些文本。
假设下面是您的配置-
[program:app]
command=/bin/bash -c "exec npm run start-generic"
directory=/app01
stdout_logfile=/logs/app.log
stdout_logfile_maxbytes=50
autostart=true
autorestart=true
startretries=0
stopasgroup=true
[program:nginx]
command=/usr/sbin/nginx -g "daemon off;"
logfile = /logs/nginx.log
stdout_logfile_maxbytes=50
然后在配置中添加尾部作业以在stdout中写入并在其之前附加字符串-
[program:nginx-log-access]
command=/bin/bash -c "tail -f /logs/nginx.log | sed 's/^/[nginx-access] /'"
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
[program:nginx-log-error]
command=/bin/bash -c "tail -f /logs/nginx-error.log | sed 's/^/[nginx-error] /'"
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
[program:app-log]
command=/bin/bash -c "tail -f /logs/app.log | sed 's/^/[app-access] /'"
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0