container_logs.go 1000 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package client
  2. import (
  3. "io"
  4. "net/url"
  5. "time"
  6. "golang.org/x/net/context"
  7. "github.com/docker/engine-api/types"
  8. timetypes "github.com/docker/engine-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, 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.Follow {
  31. query.Set("follow", "1")
  32. }
  33. query.Set("tail", options.Tail)
  34. resp, err := cli.get(ctx, "/containers/"+options.ContainerID+"/logs", query, nil)
  35. if err != nil {
  36. return nil, err
  37. }
  38. return resp.body, nil
  39. }