github.com/circular-dark/docker@v1.7.0/api/client/logs.go (about)

     1  package client
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"net/url"
     7  
     8  	"github.com/docker/docker/api/types"
     9  	flag "github.com/docker/docker/pkg/mflag"
    10  	"github.com/docker/docker/pkg/timeutils"
    11  )
    12  
    13  // CmdLogs fetches the logs of a given container.
    14  //
    15  // docker logs [OPTIONS] CONTAINER
    16  func (cli *DockerCli) CmdLogs(args ...string) error {
    17  	var (
    18  		cmd    = cli.Subcmd("logs", "CONTAINER", "Fetch the logs of a container", 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  	)
    24  	cmd.Require(flag.Exact, 1)
    25  
    26  	cmd.ParseFlags(args, true)
    27  
    28  	name := cmd.Arg(0)
    29  
    30  	stream, _, err := cli.call("GET", "/containers/"+name+"/json", nil, nil)
    31  	if err != nil {
    32  		return err
    33  	}
    34  
    35  	var c types.ContainerJSON
    36  	if err := json.NewDecoder(stream).Decode(&c); err != nil {
    37  		return err
    38  	}
    39  
    40  	if logType := c.HostConfig.LogConfig.Type; logType != "json-file" {
    41  		return fmt.Errorf("\"logs\" command is supported only for \"json-file\" logging driver (got: %s)", logType)
    42  	}
    43  
    44  	v := url.Values{}
    45  	v.Set("stdout", "1")
    46  	v.Set("stderr", "1")
    47  
    48  	if *since != "" {
    49  		v.Set("since", timeutils.GetTimestamp(*since))
    50  	}
    51  
    52  	if *times {
    53  		v.Set("timestamps", "1")
    54  	}
    55  
    56  	if *follow {
    57  		v.Set("follow", "1")
    58  	}
    59  	v.Set("tail", *tail)
    60  
    61  	sopts := &streamOpts{
    62  		rawTerminal: c.Config.Tty,
    63  		out:         cli.out,
    64  		err:         cli.err,
    65  	}
    66  
    67  	return cli.stream("GET", "/containers/"+name+"/logs?"+v.Encode(), sopts)
    68  }