Read docker containers logs in JSON format - Golang

Viewed 58

I have a requirement to fetch docker container's logs, I'm using below code to fetch docker logs,

ctx := context.Background()
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
    panic(err)
}

options := types.ContainerLogsOptions{
    ShowStdout: true,
    ShowStderr: true,
    Since:      "",
    Until:      "",
    Timestamps: false,
    Follow:     true,
    Tail:       "",
    Details:    true,
}

out, err := cli.ContainerLogs(ctx, "bcd693465a62", options)
if err != nil {
    panic(err)
}

buf := new(strings.Builder)
_, err = io.Copy(buf, out)
if err != nil {
    fmt.Println(err)
}
fmt.Printf("%s", buf)

My problem is I'm getting docker logs in two different format, for some containers it's just plain text

exec /entrypoint.sh: no such file or directory

but in containerID-json.log file it's showing in below format

{"log":"exec /entrypoint.sh: no such file or directory\n","stream":"stderr","time":"2022-09-06T12:23:57.741316145Z"}

and for some containers it's showing in dynamic format/schema like

time="2022-09-05T02:13:44Z" level=debug msg="cleanup aborting ingest" ref="buildkit/1/layer-sha256:2774afd0c4d3ded992c58f3b5e5939d091bd26f40e507c6dc21dcbd8b7ff486f"

time="2022-09-05T02:13:44Z" level=debug msg="content garbage collected" d=2.971574ms

How I can collect all docker container's logs in same schema/format so it can be stored in below JSON format?

{
    "containerID": "bcd693465a62",
    "logs": [
        {"log":"exec /entrypoint.sh: no such file or directory\n","stream":"stderr","time":"2022-09-06T12:23:57.741316145Z"},
        {"log":"cleanup aborting ingest","stream":"stdout","time":"2022-09-05T02:13:44Z"}
    ]
}

Any help is appreciated

1 Answers

I wrote below code with the help of code reference by @BrianWagner to fulfill my requirements (It works as expected), please suggest if it can be done better way, or if any improvements.

package main

import (
    "context"
    "encoding/binary"
    "fmt"
    "io"
    "log"
    "strings"

    "github.com/docker/docker/api/types"
    "github.com/docker/docker/client"
)

type DockerContainerLog struct {
    ContainerID string      `json:"containerID"`
    DockerLogs  []DockerLog `json:"dockerLogs"`
}

type DockerLog struct {
    Time   string `json:"time"`
    Stream string `json:"stream"`
    Log    string `json:"log"`
}

func main() {
    logs := getContainerLogs("cd3d8362ba45")
    fmt.Print(logs)
}

func getContainerLogs(cid string) DockerContainerLog {
    cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
    if err != nil {
        panic(err)
    }

    options := types.ContainerLogsOptions{
        ShowStdout: true,
        ShowStderr: true,
        Since:      "",
        Until:      "",
        Timestamps: true,
        Follow:     true,
        Tail:       "",
        Details:    false,
    }

    reader, err := cli.ContainerLogs(context.Background(), cid, options)
    if err != nil {
        log.Fatal(err)
    }
    defer reader.Close()

    dockerLogs := DockerContainerLog{ContainerID: cid}

    hdr := make([]byte, 8)
    for {
        var docLog DockerLog
        _, err := reader.Read(hdr)
        if err != nil {
            if err == io.EOF {
                return dockerLogs
            }

            log.Fatal(err)
        }

        count := binary.BigEndian.Uint32(hdr[4:])
        dat := make([]byte, count)
        _, err = reader.Read(dat)
        if err != nil && err != io.EOF {
            log.Fatal(err)
        }

        time, log, found := strings.Cut(string(dat), " ")
        if found {
            docLog.Time = time
            docLog.Log = log
            switch hdr[0] {
            case 1:
                docLog.Stream = "Stdout"
            default:
                docLog.Stream = "Stderr"
            }

            dockerLogs.DockerLogs = append(dockerLogs.DockerLogs, docLog)
        }
    }
}
Related