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
|
|
|
"fmt"
|
|
|
|
"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-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 {
|
|
|
|
var (
|
2015-05-13 22:45:01 -04:00
|
|
|
cmd = cli.Subcmd("logs", []string{"CONTAINER"}, "Fetch the logs of a container", true)
|
2015-03-24 23:57:23 -04:00
|
|
|
follow = cmd.Bool([]string{"f", "-follow"}, false, "Follow log output")
|
2015-04-14 00:36:12 -04:00
|
|
|
since = cmd.String([]string{"-since"}, "", "Show logs since timestamp")
|
2015-03-24 23:57:23 -04:00
|
|
|
times = cmd.Bool([]string{"t", "-timestamps"}, false, "Show timestamps")
|
|
|
|
tail = cmd.String([]string{"-tail"}, "all", "Number of lines to show from the end of the logs")
|
|
|
|
)
|
|
|
|
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-05-13 21:36:36 -04:00
|
|
|
stream, _, _, 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
|
|
|
|
if err := json.NewDecoder(stream).Decode(&c); err != nil {
|
2015-03-24 23:57:23 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-04-09 00:23:30 -04:00
|
|
|
if logType := c.HostConfig.LogConfig.Type; logType != "json-file" {
|
|
|
|
return fmt.Errorf("\"logs\" command is supported only for \"json-file\" logging driver (got: %s)", logType)
|
2015-03-24 23:57:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
|
|
|
|
return cli.stream("GET", "/containers/"+name+"/logs?"+v.Encode(), sopts)
|
2015-03-24 23:57:23 -04:00
|
|
|
}
|