2015-03-25 03:57:23 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2015-11-06 19:40:48 +00:00
|
|
|
"fmt"
|
2015-12-04 00:34:16 +00:00
|
|
|
"io"
|
2015-03-25 03:57:23 +00:00
|
|
|
|
2016-02-03 23:41:26 +00:00
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
2015-05-05 04:18:28 +00:00
|
|
|
Cli "github.com/docker/docker/cli"
|
2015-03-25 03:57:23 +00:00
|
|
|
flag "github.com/docker/docker/pkg/mflag"
|
2015-12-04 00:34:16 +00:00
|
|
|
"github.com/docker/docker/pkg/stdcopy"
|
2016-01-05 00:05:26 +00:00
|
|
|
"github.com/docker/engine-api/types"
|
2015-03-25 03:57:23 +00:00
|
|
|
)
|
|
|
|
|
2015-11-06 19:40:48 +00:00
|
|
|
var validDrivers = map[string]bool{
|
|
|
|
"json-file": true,
|
|
|
|
"journald": true,
|
|
|
|
}
|
|
|
|
|
2015-03-25 17:34:41 +00:00
|
|
|
// CmdLogs fetches the logs of a given container.
|
|
|
|
//
|
|
|
|
// docker logs [OPTIONS] CONTAINER
|
2015-03-25 03:57:23 +00:00
|
|
|
func (cli *DockerCli) CmdLogs(args ...string) error {
|
2015-10-08 12:46:21 +00:00
|
|
|
cmd := Cli.Subcmd("logs", []string{"CONTAINER"}, Cli.DockerCommands["logs"].Description, true)
|
2015-07-03 09:26:09 +00:00
|
|
|
follow := cmd.Bool([]string{"f", "-follow"}, false, "Follow log output")
|
|
|
|
since := cmd.String([]string{"-since"}, "", "Show logs since timestamp")
|
|
|
|
times := cmd.Bool([]string{"t", "-timestamps"}, false, "Show timestamps")
|
2015-07-03 13:50:06 +00:00
|
|
|
tail := cmd.String([]string{"-tail"}, "all", "Number of lines to show from the end of the logs")
|
2015-03-25 03:57:23 +00:00
|
|
|
cmd.Require(flag.Exact, 1)
|
|
|
|
|
2015-03-29 01:22:46 +00:00
|
|
|
cmd.ParseFlags(args, true)
|
2015-03-25 03:57:23 +00:00
|
|
|
|
|
|
|
name := cmd.Arg(0)
|
|
|
|
|
2016-03-16 19:19:22 +00:00
|
|
|
c, err := cli.client.ContainerInspect(context.Background(), name)
|
2015-03-25 03:57:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-11-06 19:40:48 +00:00
|
|
|
if !validDrivers[c.HostConfig.LogConfig.Type] {
|
|
|
|
return fmt.Errorf("\"logs\" command is supported only for \"json-file\" and \"journald\" logging drivers (got: %s)", c.HostConfig.LogConfig.Type)
|
|
|
|
}
|
|
|
|
|
2015-12-04 22:02:06 +00:00
|
|
|
options := types.ContainerLogsOptions{
|
2015-12-04 00:34:16 +00:00
|
|
|
ContainerID: name,
|
|
|
|
ShowStdout: true,
|
|
|
|
ShowStderr: true,
|
|
|
|
Since: *since,
|
|
|
|
Timestamps: *times,
|
|
|
|
Follow: *follow,
|
|
|
|
Tail: *tail,
|
2015-03-25 03:57:23 +00:00
|
|
|
}
|
2016-02-03 23:41:26 +00:00
|
|
|
responseBody, err := cli.client.ContainerLogs(context.Background(), options)
|
2015-12-04 00:34:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-03-25 03:57:23 +00:00
|
|
|
}
|
2015-12-04 00:34:16 +00:00
|
|
|
defer responseBody.Close()
|
2015-03-25 03:57:23 +00:00
|
|
|
|
2015-12-04 00:34:16 +00:00
|
|
|
if c.Config.Tty {
|
|
|
|
_, err = io.Copy(cli.out, responseBody)
|
|
|
|
} else {
|
|
|
|
_, err = stdcopy.StdCopy(cli.out, cli.err, responseBody)
|
2015-05-01 18:23:44 +00:00
|
|
|
}
|
2015-06-04 13:30:14 +00:00
|
|
|
return err
|
2015-03-25 03:57:23 +00:00
|
|
|
}
|