2015-03-24 23:57:23 -04:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2015-11-06 14:40:48 -05:00
|
|
|
"fmt"
|
2015-12-03 19:34:16 -05:00
|
|
|
"io"
|
2015-03-24 23:57:23 -04:00
|
|
|
|
2016-02-03 18:41:26 -05:00
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
2015-05-05 00:18:28 -04:00
|
|
|
Cli "github.com/docker/docker/cli"
|
2015-03-24 23:57:23 -04:00
|
|
|
flag "github.com/docker/docker/pkg/mflag"
|
2015-12-03 19:34:16 -05:00
|
|
|
"github.com/docker/docker/pkg/stdcopy"
|
2016-01-04 19:05:26 -05:00
|
|
|
"github.com/docker/engine-api/types"
|
2015-03-24 23:57:23 -04:00
|
|
|
)
|
|
|
|
|
2015-11-06 14:40:48 -05:00
|
|
|
var validDrivers = map[string]bool{
|
|
|
|
"json-file": true,
|
|
|
|
"journald": true,
|
|
|
|
}
|
|
|
|
|
2015-03-25 13:34:41 -04:00
|
|
|
// CmdLogs fetches the logs of a given container.
|
|
|
|
//
|
|
|
|
// docker logs [OPTIONS] CONTAINER
|
2015-03-24 23:57:23 -04:00
|
|
|
func (cli *DockerCli) CmdLogs(args ...string) error {
|
2015-10-08 08:46:21 -04:00
|
|
|
cmd := Cli.Subcmd("logs", []string{"CONTAINER"}, Cli.DockerCommands["logs"].Description, true)
|
2015-07-03 05:26:09 -04: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")
|
2016-04-08 12:15:08 -04:00
|
|
|
details := cmd.Bool([]string{"-details"}, false, "Show extra details provided to logs")
|
2015-07-03 09:50:06 -04:00
|
|
|
tail := cmd.String([]string{"-tail"}, "all", "Number of lines to show from the end of the logs")
|
2015-03-24 23:57:23 -04:00
|
|
|
cmd.Require(flag.Exact, 1)
|
|
|
|
|
2015-03-28 21:22:46 -04:00
|
|
|
cmd.ParseFlags(args, true)
|
2015-03-24 23:57:23 -04:00
|
|
|
|
|
|
|
name := cmd.Arg(0)
|
|
|
|
|
2016-05-21 09:57:57 -04:00
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
c, err := cli.client.ContainerInspect(ctx, name)
|
2015-03-24 23:57:23 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-11-06 14:40:48 -05: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 17:02:06 -05:00
|
|
|
options := types.ContainerLogsOptions{
|
2016-04-13 04:33:46 -04:00
|
|
|
ShowStdout: true,
|
|
|
|
ShowStderr: true,
|
|
|
|
Since: *since,
|
|
|
|
Timestamps: *times,
|
|
|
|
Follow: *follow,
|
|
|
|
Tail: *tail,
|
2016-04-08 12:15:08 -04:00
|
|
|
Details: *details,
|
2015-03-24 23:57:23 -04:00
|
|
|
}
|
2016-05-21 09:57:57 -04:00
|
|
|
responseBody, err := cli.client.ContainerLogs(ctx, name, options)
|
2015-12-03 19:34:16 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-03-24 23:57:23 -04:00
|
|
|
}
|
2015-12-03 19:34:16 -05:00
|
|
|
defer responseBody.Close()
|
2015-03-24 23:57:23 -04:00
|
|
|
|
2015-12-03 19:34:16 -05:00
|
|
|
if c.Config.Tty {
|
|
|
|
_, err = io.Copy(cli.out, responseBody)
|
|
|
|
} else {
|
|
|
|
_, err = stdcopy.StdCopy(cli.out, cli.err, responseBody)
|
2015-05-01 14:23:44 -04:00
|
|
|
}
|
2015-06-04 09:30:14 -04:00
|
|
|
return err
|
2015-03-24 23:57:23 -04:00
|
|
|
}
|