github.com/goern/docker@v1.9.0-rc1/api/client/logs.go (about)

     1  package client
     2  
     3  import (
     4  	"encoding/json"
     5  	"net/url"
     6  	"time"
     7  
     8  	"github.com/docker/docker/api/types"
     9  	Cli "github.com/docker/docker/cli"
    10  	flag "github.com/docker/docker/pkg/mflag"
    11  	"github.com/docker/docker/pkg/timeutils"
    12  )
    13  
    14  // CmdLogs fetches the logs of a given container.
    15  //
    16  // docker logs [OPTIONS] CONTAINER
    17  func (cli *DockerCli) CmdLogs(args ...string) error {
    18  	cmd := Cli.Subcmd("logs", []string{"CONTAINER"}, Cli.DockerCommands["logs"].Description, true)
    19  	follow := cmd.Bool([]string{"f", "-follow"}, false, "Follow log output")
    20  	since := cmd.String([]string{"-since"}, "", "Show logs since timestamp")
    21  	times := cmd.Bool([]string{"t", "-timestamps"}, false, "Show timestamps")
    22  	tail := cmd.String([]string{"-tail"}, "all", "Number of lines to show from the end of the logs")
    23  	cmd.Require(flag.Exact, 1)
    24  
    25  	cmd.ParseFlags(args, true)
    26  
    27  	name := cmd.Arg(0)
    28  
    29  	serverResp, err := cli.call("GET", "/containers/"+name+"/json", nil, nil)
    30  	if err != nil {
    31  		return err
    32  	}
    33  
    34  	var c types.ContainerJSON
    35  	if err := json.NewDecoder(serverResp.body).Decode(&c); err != nil {
    36  		return err
    37  	}
    38  
    39  	v := url.Values{}
    40  	v.Set("stdout", "1")
    41  	v.Set("stderr", "1")
    42  
    43  	if *since != "" {
    44  		v.Set("since", timeutils.GetTimestamp(*since, time.Now()))
    45  	}
    46  
    47  	if *times {
    48  		v.Set("timestamps", "1")
    49  	}
    50  
    51  	if *follow {
    52  		v.Set("follow", "1")
    53  	}
    54  	v.Set("tail", *tail)
    55  
    56  	sopts := &streamOpts{
    57  		rawTerminal: c.Config.Tty,
    58  		out:         cli.out,
    59  		err:         cli.err,
    60  	}
    61  
    62  	_, err = cli.stream("GET", "/containers/"+name+"/logs?"+v.Encode(), sopts)
    63  	return err
    64  }