Stream logs to local volume

You’re welcome. For B, I meant to start fly logs in the background from your startup command, for example by setting the command to (untested) fly logs -i "$FLY_ALLOC_ID" >>/path/to/log 2>&1 & ORIG_CMD. So you don’t have to manually ssh in, and when the VM restarts the logs process will be started automatically.

For A, here is a quick demo of using ts to add timestamps:

$ apt-get install -y moreutils
$ ./app
Waiting for request...
Processing request...
Request complete
$ ./app 2>&1 | ts
Apr 15 08:17:00 Waiting for request...
Apr 15 08:17:01 Processing request...
Apr 15 08:17:02 Request complete
$ ./app 2>&1 | TZ=UTC0 ts "%FT%.T app[${FLY_ALLOC_ID%%-*}] $FLY_REGION [info]"
2023-04-15T08:17:05.188238 app[b996131a] ams [info] Waiting for request...
2023-04-15T08:17:06.179019 app[b996131a] ams [info] Processing request...
2023-04-15T08:17:07.182022 app[b996131a] ams [info] Request complete

where ./app is the following dummy script:

#!/bin/sh
echo "Waiting for request..."
sleep 1
echo "Processing request..."
sleep 1
echo "Request complete"

Getting the log messages to appear both in your custom log file and in the output of fly logs without duplicate timestamps is slightly tricky, here is one way:

ORIG_CMD 2>&1 | tee -a /dev/stderr | TZ=UTC0 ts "%FT%.T app[${FLY_ALLOC_ID%%-*}] $FLY_REGION [info]" >>/path/to/log
2 Likes