github.com/ncdc/docker@v0.10.1-0.20160129113957-6c6729ef5b74/api/client/logs.go (about) 1 package client 2 3 import ( 4 "fmt" 5 "io" 6 7 Cli "github.com/docker/docker/cli" 8 flag "github.com/docker/docker/pkg/mflag" 9 "github.com/docker/docker/pkg/stdcopy" 10 "github.com/docker/engine-api/types" 11 ) 12 13 var validDrivers = map[string]bool{ 14 "json-file": true, 15 "journald": true, 16 } 17 18 // CmdLogs fetches the logs of a given container. 19 // 20 // docker logs [OPTIONS] CONTAINER 21 func (cli *DockerCli) CmdLogs(args ...string) error { 22 cmd := Cli.Subcmd("logs", []string{"CONTAINER"}, Cli.DockerCommands["logs"].Description, true) 23 follow := cmd.Bool([]string{"f", "-follow"}, false, "Follow log output") 24 since := cmd.String([]string{"-since"}, "", "Show logs since timestamp") 25 times := cmd.Bool([]string{"t", "-timestamps"}, false, "Show timestamps") 26 tail := cmd.String([]string{"-tail"}, "all", "Number of lines to show from the end of the logs") 27 cmd.Require(flag.Exact, 1) 28 29 cmd.ParseFlags(args, true) 30 31 name := cmd.Arg(0) 32 33 c, err := cli.client.ContainerInspect(name) 34 if err != nil { 35 return err 36 } 37 38 if !validDrivers[c.HostConfig.LogConfig.Type] { 39 return fmt.Errorf("\"logs\" command is supported only for \"json-file\" and \"journald\" logging drivers (got: %s)", c.HostConfig.LogConfig.Type) 40 } 41 42 options := types.ContainerLogsOptions{ 43 ContainerID: name, 44 ShowStdout: true, 45 ShowStderr: true, 46 Since: *since, 47 Timestamps: *times, 48 Follow: *follow, 49 Tail: *tail, 50 } 51 responseBody, err := cli.client.ContainerLogs(options) 52 if err != nil { 53 return err 54 } 55 defer responseBody.Close() 56 57 if c.Config.Tty { 58 _, err = io.Copy(cli.out, responseBody) 59 } else { 60 _, err = stdcopy.StdCopy(cli.out, cli.err, responseBody) 61 } 62 return err 63 }