mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
7d62e40f7e
Since Go 1.7, context is a standard package. Since Go 1.9, everything that is provided by "x/net/context" is a couple of type aliases to types in "context". Many vendored packages still use x/net/context, so vendor entry remains for now. Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
79 lines
2 KiB
Go
79 lines
2 KiB
Go
package client // import "github.com/docker/docker/client"
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"net/url"
|
|
"time"
|
|
|
|
"github.com/docker/docker/api/types"
|
|
timetypes "github.com/docker/docker/api/types/time"
|
|
)
|
|
|
|
// ContainerLogs returns the logs generated by a container in an io.ReadCloser.
|
|
// It's up to the caller to close the stream.
|
|
//
|
|
// The stream format on the response will be in one of two formats:
|
|
//
|
|
// If the container is using a TTY, there is only a single stream (stdout), and
|
|
// data is copied directly from the container output stream, no extra
|
|
// multiplexing or headers.
|
|
//
|
|
// If the container is *not* using a TTY, streams for stdout and stderr are
|
|
// multiplexed.
|
|
// The format of the multiplexed stream is as follows:
|
|
//
|
|
// [8]byte{STREAM_TYPE, 0, 0, 0, SIZE1, SIZE2, SIZE3, SIZE4}[]byte{OUTPUT}
|
|
//
|
|
// STREAM_TYPE can be 1 for stdout and 2 for stderr
|
|
//
|
|
// SIZE1, SIZE2, SIZE3, and SIZE4 are four bytes of uint32 encoded as big endian.
|
|
// This is the size of OUTPUT.
|
|
//
|
|
// You can use github.com/docker/docker/pkg/stdcopy.StdCopy to demultiplex this
|
|
// stream.
|
|
func (cli *Client) ContainerLogs(ctx context.Context, container string, options types.ContainerLogsOptions) (io.ReadCloser, error) {
|
|
query := url.Values{}
|
|
if options.ShowStdout {
|
|
query.Set("stdout", "1")
|
|
}
|
|
|
|
if options.ShowStderr {
|
|
query.Set("stderr", "1")
|
|
}
|
|
|
|
if options.Since != "" {
|
|
ts, err := timetypes.GetTimestamp(options.Since, time.Now())
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
query.Set("since", ts)
|
|
}
|
|
|
|
if options.Until != "" {
|
|
ts, err := timetypes.GetTimestamp(options.Until, time.Now())
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
query.Set("until", ts)
|
|
}
|
|
|
|
if options.Timestamps {
|
|
query.Set("timestamps", "1")
|
|
}
|
|
|
|
if options.Details {
|
|
query.Set("details", "1")
|
|
}
|
|
|
|
if options.Follow {
|
|
query.Set("follow", "1")
|
|
}
|
|
query.Set("tail", options.Tail)
|
|
|
|
resp, err := cli.get(ctx, "/containers/"+container+"/logs", query, nil)
|
|
if err != nil {
|
|
return nil, wrapResponseError(err, resp, "container", container)
|
|
}
|
|
return resp.body, nil
|
|
}
|