service_logs.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package client // import "github.com/docker/docker/client"
  2. import (
  3. "context"
  4. "io"
  5. "net/url"
  6. "time"
  7. "github.com/docker/docker/api/types/container"
  8. timetypes "github.com/docker/docker/api/types/time"
  9. "github.com/pkg/errors"
  10. )
  11. // ServiceLogs returns the logs generated by a service in an io.ReadCloser.
  12. // It's up to the caller to close the stream.
  13. func (cli *Client) ServiceLogs(ctx context.Context, serviceID string, options container.LogsOptions) (io.ReadCloser, error) {
  14. query := url.Values{}
  15. if options.ShowStdout {
  16. query.Set("stdout", "1")
  17. }
  18. if options.ShowStderr {
  19. query.Set("stderr", "1")
  20. }
  21. if options.Since != "" {
  22. ts, err := timetypes.GetTimestamp(options.Since, time.Now())
  23. if err != nil {
  24. return nil, errors.Wrap(err, `invalid value for "since"`)
  25. }
  26. query.Set("since", ts)
  27. }
  28. if options.Timestamps {
  29. query.Set("timestamps", "1")
  30. }
  31. if options.Details {
  32. query.Set("details", "1")
  33. }
  34. if options.Follow {
  35. query.Set("follow", "1")
  36. }
  37. query.Set("tail", options.Tail)
  38. resp, err := cli.get(ctx, "/services/"+serviceID+"/logs", query, nil)
  39. if err != nil {
  40. return nil, err
  41. }
  42. return resp.body, nil
  43. }