2018-02-05 16:05:59 -05:00
|
|
|
package client // import "github.com/docker/docker/client"
|
2016-10-26 04:17:31 -04:00
|
|
|
|
|
|
|
import (
|
2018-04-19 18:30:59 -04:00
|
|
|
"context"
|
2016-10-26 04:17:31 -04:00
|
|
|
"io"
|
|
|
|
"net/url"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
timetypes "github.com/docker/docker/api/types/time"
|
2017-11-03 12:39:53 -04:00
|
|
|
"github.com/pkg/errors"
|
2016-10-26 04:17:31 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// ServiceLogs returns the logs generated by a service in an io.ReadCloser.
|
|
|
|
// It's up to the caller to close the stream.
|
|
|
|
func (cli *Client) ServiceLogs(ctx context.Context, serviceID 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 {
|
2017-11-03 12:39:53 -04:00
|
|
|
return nil, errors.Wrap(err, `invalid value for "since"`)
|
2016-10-26 04:17:31 -04:00
|
|
|
}
|
|
|
|
query.Set("since", 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, "/services/"+serviceID+"/logs", query, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return resp.body, nil
|
|
|
|
}
|