2015-03-24 23:57:23 -04:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2015-04-13 10:17:14 -04:00
|
|
|
"encoding/json"
|
2015-03-24 23:57:23 -04:00
|
|
|
"net/url"
|
2015-05-12 15:59:34 -04:00
|
|
|
"time"
|
2015-03-24 23:57:23 -04:00
|
|
|
|
2015-04-13 10:17:14 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
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-04-14 00:36:12 -04:00
|
|
|
"github.com/docker/docker/pkg/timeutils"
|
2015-03-24 23:57:23 -04:00
|
|
|
)
|
|
|
|
|
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-05-05 00:18:28 -04:00
|
|
|
cmd := Cli.Subcmd("logs", []string{"CONTAINER"}, "Fetch the logs of a container", 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")
|
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)
|
|
|
|
|
2015-07-09 22:05:50 -04:00
|
|
|
serverResp, err := cli.call("GET", "/containers/"+name+"/json", nil, nil)
|
2015-03-24 23:57:23 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-04-13 10:17:14 -04:00
|
|
|
var c types.ContainerJSON
|
2015-07-09 22:05:50 -04:00
|
|
|
if err := json.NewDecoder(serverResp.body).Decode(&c); err != nil {
|
2015-03-24 23:57:23 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
v := url.Values{}
|
|
|
|
v.Set("stdout", "1")
|
|
|
|
v.Set("stderr", "1")
|
|
|
|
|
2015-04-14 00:36:12 -04:00
|
|
|
if *since != "" {
|
2015-05-12 15:59:34 -04:00
|
|
|
v.Set("since", timeutils.GetTimestamp(*since, time.Now()))
|
2015-04-14 00:36:12 -04:00
|
|
|
}
|
|
|
|
|
2015-03-24 23:57:23 -04:00
|
|
|
if *times {
|
|
|
|
v.Set("timestamps", "1")
|
|
|
|
}
|
|
|
|
|
|
|
|
if *follow {
|
|
|
|
v.Set("follow", "1")
|
|
|
|
}
|
|
|
|
v.Set("tail", *tail)
|
|
|
|
|
2015-05-01 14:23:44 -04:00
|
|
|
sopts := &streamOpts{
|
|
|
|
rawTerminal: c.Config.Tty,
|
|
|
|
out: cli.out,
|
|
|
|
err: cli.err,
|
|
|
|
}
|
|
|
|
|
2015-06-04 09:30:14 -04:00
|
|
|
_, err = cli.stream("GET", "/containers/"+name+"/logs?"+v.Encode(), sopts)
|
|
|
|
return err
|
2015-03-24 23:57:23 -04:00
|
|
|
}
|