logs.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package client
  2. import (
  3. "fmt"
  4. "net/url"
  5. "github.com/docker/docker/engine"
  6. flag "github.com/docker/docker/pkg/mflag"
  7. )
  8. // CmdLogs fetches the logs of a given container.
  9. //
  10. // docker logs [OPTIONS] CONTAINER
  11. func (cli *DockerCli) CmdLogs(args ...string) error {
  12. var (
  13. cmd = cli.Subcmd("logs", "CONTAINER", "Fetch the logs of a container", true)
  14. follow = cmd.Bool([]string{"f", "-follow"}, false, "Follow log output")
  15. times = cmd.Bool([]string{"t", "-timestamps"}, false, "Show timestamps")
  16. tail = cmd.String([]string{"-tail"}, "all", "Number of lines to show from the end of the logs")
  17. )
  18. cmd.Require(flag.Exact, 1)
  19. cmd.ParseFlags(args, true)
  20. name := cmd.Arg(0)
  21. stream, _, err := cli.call("GET", "/containers/"+name+"/json", nil, nil)
  22. if err != nil {
  23. return err
  24. }
  25. env := engine.Env{}
  26. if err := env.Decode(stream); err != nil {
  27. return err
  28. }
  29. if env.GetSubEnv("HostConfig").GetSubEnv("LogConfig").Get("Type") != "json-file" {
  30. return fmt.Errorf("\"logs\" command is supported only for \"json-file\" logging driver")
  31. }
  32. v := url.Values{}
  33. v.Set("stdout", "1")
  34. v.Set("stderr", "1")
  35. if *times {
  36. v.Set("timestamps", "1")
  37. }
  38. if *follow {
  39. v.Set("follow", "1")
  40. }
  41. v.Set("tail", *tail)
  42. return cli.streamHelper("GET", "/containers/"+name+"/logs?"+v.Encode(), env.GetSubEnv("Config").GetBool("Tty"), nil, cli.out, cli.err, nil)
  43. }