Rob Watson df9724afa7
Some checks are pending
ci-build / lint (push) Waiting to run
ci-build / build (push) Blocked by required conditions
ci-build / release (push) Blocked by required conditions
feat: container logs
2025-04-14 11:17:10 +01:00

54 lines
1.1 KiB
Go

package container
import (
"bufio"
"context"
"log/slog"
typescontainer "github.com/docker/docker/api/types/container"
)
func getLogs(
ctx context.Context,
containerID string,
apiClient DockerClient,
cfg LogConfig,
ch chan<- []byte,
logger *slog.Logger,
) {
logsC, err := apiClient.ContainerLogs(
ctx,
containerID,
typescontainer.LogsOptions{
ShowStdout: cfg.Stdout,
ShowStderr: cfg.Stderr,
Follow: true,
},
)
if err != nil {
logger.Error("Error getting container logs", "err", err, "id", shortID(containerID))
return
}
defer logsC.Close()
scanner := bufio.NewScanner(logsC)
for scanner.Scan() {
select {
case <-ctx.Done():
return
default:
// Docker logs are prefixed with an 8 byte prefix.
// See client.ContainerLogs for more details.
// We could use
// [StdCopy](https://pkg.go.dev/github.com/docker/docker/pkg/stdcopy#StdCopy)
// but for our purposes it's enough to just slice it off.
const prefixLen = 8
line := scanner.Bytes()
if len(line) <= prefixLen {
continue
}
ch <- line[prefixLen:]
}
}
}