github.com/chenchun/docker@v1.3.2-0.20150629222414-20467faf132b/api/client/logs.go (about) 1 package client 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "net/url" 7 "time" 8 9 "github.com/docker/docker/api/types" 10 flag "github.com/docker/docker/pkg/mflag" 11 "github.com/docker/docker/pkg/timeutils" 12 ) 13 14 // CmdLogs fetches the logs of a given container. 15 // 16 // docker logs [OPTIONS] CONTAINER 17 func (cli *DockerCli) CmdLogs(args ...string) error { 18 var ( 19 cmd = cli.Subcmd("logs", []string{"CONTAINER"}, "Fetch the logs of a container", true) 20 follow = cmd.Bool([]string{"f", "-follow"}, false, "Follow log output") 21 since = cmd.String([]string{"-since"}, "", "Show logs since timestamp") 22 times = cmd.Bool([]string{"t", "-timestamps"}, false, "Show timestamps") 23 tail = cmd.String([]string{"-tail"}, "all", "Number of lines to show from the end of the logs") 24 ) 25 cmd.Require(flag.Exact, 1) 26 27 cmd.ParseFlags(args, true) 28 29 name := cmd.Arg(0) 30 31 stream, _, _, err := cli.call("GET", "/containers/"+name+"/json", nil, nil) 32 if err != nil { 33 return err 34 } 35 36 var c types.ContainerJSON 37 if err := json.NewDecoder(stream).Decode(&c); err != nil { 38 return err 39 } 40 41 if logType := c.HostConfig.LogConfig.Type; logType != "json-file" { 42 return fmt.Errorf("\"logs\" command is supported only for \"json-file\" logging driver (got: %s)", logType) 43 } 44 45 v := url.Values{} 46 v.Set("stdout", "1") 47 v.Set("stderr", "1") 48 49 if *since != "" { 50 v.Set("since", timeutils.GetTimestamp(*since, time.Now())) 51 } 52 53 if *times { 54 v.Set("timestamps", "1") 55 } 56 57 if *follow { 58 v.Set("follow", "1") 59 } 60 v.Set("tail", *tail) 61 62 sopts := &streamOpts{ 63 rawTerminal: c.Config.Tty, 64 out: cli.out, 65 err: cli.err, 66 } 67 68 _, err = cli.stream("GET", "/containers/"+name+"/logs?"+v.Encode(), sopts) 69 return err 70 }