logs.go 931 B

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