swift“打印”不会出现在 STDOut 中,但在 ECS 上的 docker 中运行时,第 3 方 C 库日志会出现

Avn*_*arr 3 amazon-ecs docker swift swift-package-manager

因此,当在开发中通过xcode或使用SPM控制台日志进行编译时,本地日志会按预期显示。

SPM本地一切都很好

swift build --configuration release 
.build/release/Myapp # prints to console
Run Code Online (Sandbox Code Playgroud)

但是当我通过在 ECS(我想是 Linux)上运行的 docker 容器运行可执行文件时,我看不到 Swift 代码生成的日志,但我确实看到 stderr 被第 3 方库打印(即 libssl 正在打印错误)以及启动应用程序时的 shell 日志

例如:

Dockerfile

FROM swift
WORKDIR /app

COPY Package.swift ./
COPY Sources ./Sources
COPY Tests ./Tests
RUN swift package clean
RUN swift build --configuration release
RUN chmod +x start.sh
CMD ["start.sh"] # just a wrapper to see if "echo" works
Run Code Online (Sandbox Code Playgroud)

在start.sh中

# prints as expected
echo "hi this will print"
# nothing in the executable will print though
.build/release/MyApp

Run Code Online (Sandbox Code Playgroud)

Sno*_*803 5

遇到同样的问题,我提交了雷达,苹果回答:

当通过管道传输到另一个进程时,打印会被缓冲,因此在缓冲区填满之前不会出现任何字符。(当通过管道传输到终端时,我们只会缓冲直到遇到换行符。)

您可以通过在启动时调用一次来获得您想要的行为setbuf(stdout, nil)

 import Darwin
 setbuf(stdout, nil)
Run Code Online (Sandbox Code Playgroud)