2018-02-05 21:05:59 +00:00
|
|
|
package client // import "github.com/docker/docker/client"
|
2016-09-06 18:46:37 +00:00
|
|
|
|
|
|
|
import (
|
2018-04-19 22:30:59 +00:00
|
|
|
"context"
|
2016-09-06 18:46:37 +00:00
|
|
|
"io"
|
|
|
|
"net/url"
|
|
|
|
"time"
|
|
|
|
|
2023-08-25 22:19:21 +00:00
|
|
|
"github.com/docker/docker/api/types/container"
|
2016-09-06 18:46:37 +00:00
|
|
|
timetypes "github.com/docker/docker/api/types/time"
|
2017-11-03 16:39:53 +00:00
|
|
|
"github.com/pkg/errors"
|
2016-09-06 18:46:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// ContainerLogs returns the logs generated by a container in an io.ReadCloser.
|
|
|
|
// It's up to the caller to close the stream.
|
2017-06-02 16:32:25 +00:00
|
|
|
//
|
|
|
|
// 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:
|
|
|
|
//
|
2022-07-08 16:27:07 +00:00
|
|
|
// [8]byte{STREAM_TYPE, 0, 0, 0, SIZE1, SIZE2, SIZE3, SIZE4}[]byte{OUTPUT}
|
2017-06-02 16:32:25 +00:00
|
|
|
//
|
|
|
|
// 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.
|
2023-08-25 22:19:21 +00:00
|
|
|
func (cli *Client) ContainerLogs(ctx context.Context, container string, options container.LogsOptions) (io.ReadCloser, error) {
|
2016-09-06 18:46:37 +00:00
|
|
|
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 {
|
2017-11-03 16:39:53 +00:00
|
|
|
return nil, errors.Wrap(err, `invalid value for "since"`)
|
2016-09-06 18:46:37 +00:00
|
|
|
}
|
|
|
|
query.Set("since", ts)
|
|
|
|
}
|
|
|
|
|
2017-04-28 11:53:00 +00:00
|
|
|
if options.Until != "" {
|
|
|
|
ts, err := timetypes.GetTimestamp(options.Until, time.Now())
|
|
|
|
if err != nil {
|
2017-11-03 16:39:53 +00:00
|
|
|
return nil, errors.Wrap(err, `invalid value for "until"`)
|
2017-04-28 11:53:00 +00:00
|
|
|
}
|
|
|
|
query.Set("until", ts)
|
|
|
|
}
|
|
|
|
|
2016-09-06 18:46:37 +00:00
|
|
|
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 {
|
2022-03-20 15:55:42 +00:00
|
|
|
return nil, err
|
2016-09-06 18:46:37 +00:00
|
|
|
}
|
|
|
|
return resp.body, nil
|
|
|
|
}
|