github.com/hustcat/docker@v1.3.3-0.20160314103604-901c67a8eeab/api/client/logs.go (about) 1 package client 2 3 import ( 4 "fmt" 5 "io" 6 7 "golang.org/x/net/context" 8 9 Cli "github.com/docker/docker/cli" 10 flag "github.com/docker/docker/pkg/mflag" 11 "github.com/docker/docker/pkg/stdcopy" 12 "github.com/docker/engine-api/types" 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 c, err := cli.client.ContainerInspect(name) 36 if err != nil { 37 return err 38 } 39 40 if !validDrivers[c.HostConfig.LogConfig.Type] { 41 return fmt.Errorf("\"logs\" command is supported only for \"json-file\" and \"journald\" logging drivers (got: %s)", c.HostConfig.LogConfig.Type) 42 } 43 44 options := types.ContainerLogsOptions{ 45 ContainerID: name, 46 ShowStdout: true, 47 ShowStderr: true, 48 Since: *since, 49 Timestamps: *times, 50 Follow: *follow, 51 Tail: *tail, 52 } 53 responseBody, err := cli.client.ContainerLogs(context.Background(), options) 54 if err != nil { 55 return err 56 } 57 defer responseBody.Close() 58 59 if c.Config.Tty { 60 _, err = io.Copy(cli.out, responseBody) 61 } else { 62 _, err = stdcopy.StdCopy(cli.out, cli.err, responseBody) 63 } 64 return err 65 }