github.com/tompao/docker@v1.9.1/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 Cli "github.com/docker/docker/cli" 11 flag "github.com/docker/docker/pkg/mflag" 12 "github.com/docker/docker/pkg/timeutils" 13 ) 14 15 var validDrivers = map[string]bool{ 16 "json-file": true, 17 "journald": true, 18 } 19 20 // CmdLogs fetches the logs of a given container. 21 // 22 // docker logs [OPTIONS] CONTAINER 23 func (cli *DockerCli) CmdLogs(args ...string) error { 24 cmd := Cli.Subcmd("logs", []string{"CONTAINER"}, Cli.DockerCommands["logs"].Description, true) 25 follow := cmd.Bool([]string{"f", "-follow"}, false, "Follow log output") 26 since := cmd.String([]string{"-since"}, "", "Show logs since timestamp") 27 times := cmd.Bool([]string{"t", "-timestamps"}, false, "Show timestamps") 28 tail := cmd.String([]string{"-tail"}, "all", "Number of lines to show from the end of the logs") 29 cmd.Require(flag.Exact, 1) 30 31 cmd.ParseFlags(args, true) 32 33 name := cmd.Arg(0) 34 35 serverResp, err := cli.call("GET", "/containers/"+name+"/json", nil, nil) 36 if err != nil { 37 return err 38 } 39 40 var c types.ContainerJSON 41 if err := json.NewDecoder(serverResp.body).Decode(&c); err != nil { 42 return err 43 } 44 45 if !validDrivers[c.HostConfig.LogConfig.Type] { 46 return fmt.Errorf("\"logs\" command is supported only for \"json-file\" and \"journald\" logging drivers (got: %s)", c.HostConfig.LogConfig.Type) 47 } 48 49 v := url.Values{} 50 v.Set("stdout", "1") 51 v.Set("stderr", "1") 52 53 if *since != "" { 54 v.Set("since", timeutils.GetTimestamp(*since, time.Now())) 55 } 56 57 if *times { 58 v.Set("timestamps", "1") 59 } 60 61 if *follow { 62 v.Set("follow", "1") 63 } 64 v.Set("tail", *tail) 65 66 sopts := &streamOpts{ 67 rawTerminal: c.Config.Tty, 68 out: cli.out, 69 err: cli.err, 70 } 71 72 _, err = cli.stream("GET", "/containers/"+name+"/logs?"+v.Encode(), sopts) 73 return err 74 }