container_logs.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. //
  13. // The stream format on the response will be in one of two formats:
  14. //
  15. // If the container is using a TTY, there is only a single stream (stdout), and
  16. // data is copied directly from the container output stream, no extra
  17. // multiplexing or headers.
  18. //
  19. // If the container is *not* using a TTY, streams for stdout and stderr are
  20. // multiplexed.
  21. // The format of the multiplexed stream is as follows:
  22. //
  23. // [8]byte{STREAM_TYPE, 0, 0, 0, SIZE1, SIZE2, SIZE3, SIZE4}[]byte{OUTPUT}
  24. //
  25. // STREAM_TYPE can be 1 for stdout and 2 for stderr
  26. //
  27. // SIZE1, SIZE2, SIZE3, and SIZE4 are four bytes of uint32 encoded as big endian.
  28. // This is the size of OUTPUT.
  29. //
  30. // You can use github.com/docker/docker/pkg/stdcopy.StdCopy to demultiplex this
  31. // stream.
  32. func (cli *Client) ContainerLogs(ctx context.Context, container string, options types.ContainerLogsOptions) (io.ReadCloser, error) {
  33. query := url.Values{}
  34. if options.ShowStdout {
  35. query.Set("stdout", "1")
  36. }
  37. if options.ShowStderr {
  38. query.Set("stderr", "1")
  39. }
  40. if options.Since != "" {
  41. ts, err := timetypes.GetTimestamp(options.Since, time.Now())
  42. if err != nil {
  43. return nil, err
  44. }
  45. query.Set("since", ts)
  46. }
  47. if options.Timestamps {
  48. query.Set("timestamps", "1")
  49. }
  50. if options.Details {
  51. query.Set("details", "1")
  52. }
  53. if options.Follow {
  54. query.Set("follow", "1")
  55. }
  56. query.Set("tail", options.Tail)
  57. resp, err := cli.get(ctx, "/containers/"+container+"/logs", query, nil)
  58. if err != nil {
  59. return nil, err
  60. }
  61. return resp.body, nil
  62. }