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-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-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 (
|
|
|
|
cmd = cli.Subcmd("logs", "CONTAINER", "Fetch the logs of a container", true)
|
|
|
|
follow = cmd.Bool([]string{"f", "-follow"}, false, "Follow log output")
|
|
|
|
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-01-12 14:56:01 -05: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-13 10:17:14 -04:00
|
|
|
if c.HostConfig.LogConfig.Type != "json-file" {
|
2015-03-24 23:57:23 -04:00
|
|
|
return fmt.Errorf("\"logs\" command is supported only for \"json-file\" logging driver")
|
|
|
|
}
|
|
|
|
|
|
|
|
v := url.Values{}
|
|
|
|
v.Set("stdout", "1")
|
|
|
|
v.Set("stderr", "1")
|
|
|
|
|
|
|
|
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
|
|
|
}
|