Skip to content

Commit

Permalink
fix: read and send container logs line-by-line
Browse files Browse the repository at this point in the history
This ensures that each line is sent unbroken which is important for when
the logs are in a specific format that will eventually be parsed
  • Loading branch information
neurosnap committed Nov 23, 2024
1 parent a477920 commit 13c1d7c
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"bufio"
"context"
"flag"
"io"
Expand Down Expand Up @@ -66,11 +67,17 @@ func containerStart(ctx context.Context, logger *slog.Logger, client *client.Cli
readCloser, err := client.ContainerLogs(ctx, containerID, opts)
if err != nil {
logger.Error("unable to fetch container logs", "err", err)
return err
}

go func() {
_, err = io.Copy(reconn, readCloser)
if err != nil {
logger.Error("cannot write to pipe topic", "err", err)
scanner := bufio.NewScanner(readCloser)
for scanner.Scan() {
line := scanner.Text()
_, err = reconn.Write([]byte(line + "\n"))
if err != nil {
logger.Error("cannot write to pipe topic", "err", err)
}
}
}()

Expand Down

0 comments on commit 13c1d7c

Please sign in to comment.