container_logs.go 1.0 KB

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