在ruby程序中登录到标准输出(不在Docker中工作)
我正在对接我的一个ruby应用程序,但我有一个非常奇怪的日志行为。它似乎只在程序结束时加载,而不是在程序运行时加载。当我用docker compose运行程序(守护进程)时,我看到的是:在ruby程序中登录到标准输出(不在Docker中工作),ruby,logging,docker,docker-compose,Ruby,Logging,Docker,Docker Compose,我正在对接我的一个ruby应用程序,但我有一个非常奇怪的日志行为。它似乎只在程序结束时加载,而不是在程序运行时加载。当我用docker compose运行程序(守护进程)时,我看到的是: Starting custom_daemon_1 Attaching to custom_daemon_1 但是,如果我将退出部分放入程序,我会看到所有输入和记录器输出 Starting custom_daemon_1 Attaching to custom_daemon_1 custom_daemon_1
Starting custom_daemon_1
Attaching to custom_daemon_1
但是,如果我将退出
部分放入程序,我会看到所有输入
和记录器
输出
Starting custom_daemon_1
Attaching to custom_daemon_1
custom_daemon_1 | requires
custom_daemon_1 | starting logger
custom_daemon_1 | Starting loads
custom_daemon_1 | Hello base
custom_daemon_1 | Loaded track
custom_daemon_1 | Loaded geo
custom_daemon_1 | Loaded geo_all
custom_daemon_1 | Loaded unique
custom_daemon_1 | D, [2016-11-14T13:31:19.295785 #1] DEBUG -- : Starting custom_daemon...
custom_daemon_1 | D, [2016-11-14T13:31:19.295889 #1] DEBUG -- : Loading xx from disk...
custom_daemon_1 exited with code 0
没有时间的前两个是调试,看看它是否会显示-下两个是由以下内容创建的:
Logger.new(STDOUT)
LOG = Logger.new(STDOUT)
LOG.level = Logger::DEBUG
然后我会调用LOG.debug“xxx”
或LOG.error“xxx”
知道为什么会发生这种奇怪的行为吗?当Ictrl+c
退出第一个时,日志仍然不显示
这最初是由.sh
脚本运行的,现在我调用它作为Dockerfile的CMD
直接运行
我发现有一个python问题在问类似的问题。有人推测,这可能与PID 1
进程的STDOUT日志记录受支持有关
测试
下面是我运行的一个测试:
puts "starting logger"
Logger.new(STDOUT)
LOG = Logger.new(STDOUT)
LOG.level = Logger::DEBUG
puts "this is 'puts'"
p "this is 'p'"
LOG.debug "this is 'log.debug'"
puts "Starting loads"
产出:
custom_daemon_1 | starting logger
custom_daemon_1 | this is 'puts'
custom_daemon_1 | "this is 'p'"
请注意,前两个将
打印出来,但只要我尝试使用LOG.debug
它就不起作用了
测试2
我还决定尝试使用一个文件来记录日志,正如预期的那样,它通过docker很好地记录到该文件中
我所做的只是将Logger.new(STDOUT)更改为
Logger.new('mylog.log')
,我可以tail-f mylog.log
,所有log.debug
提示都会显示出来。我已经临时修复了这个问题,添加了一个基于的符号链接。在Dockerfile中:
运行ln-sf/proc/1/fd/1/var/log/mylog.log
并将我的记录器设置为,log=logger.new('/var/log/mylog.log')
,但这有两个不希望的后果。首先,日志文件将增长并占用空间,可能需要管理—我不想处理这个问题。其次,必须添加一个符号链接才能使日志正常工作,这似乎不雅观。。。希望有另一种解决方案。如本文所述
尝试禁用对标准输出的输出缓冲:$STDOUT.sync=true