github.com/guilhermebr/docker@v1.4.2-0.20150428121140-67da055cebca/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  )
    11  
    12  // CmdLogs fetches the logs of a given container.
    13  //
    14  // docker logs [OPTIONS] CONTAINER
    15  func (cli *DockerCli) CmdLogs(args ...string) error {
    16  	var (
    17  		cmd    = cli.Subcmd("logs", "CONTAINER", "Fetch the logs of a container", true)
    18  		follow = cmd.Bool([]string{"f", "-follow"}, false, "Follow log output")
    19  		times  = cmd.Bool([]string{"t", "-timestamps"}, false, "Show timestamps")
    20  		tail   = cmd.String([]string{"-tail"}, "all", "Number of lines to show from the end of the logs")
    21  	)
    22  	cmd.Require(flag.Exact, 1)
    23  
    24  	cmd.ParseFlags(args, true)
    25  
    26  	name := cmd.Arg(0)
    27  
    28  	stream, _, err := cli.call("GET", "/containers/"+name+"/json", nil, nil)
    29  	if err != nil {
    30  		return err
    31  	}
    32  
    33  	var c types.ContainerJSON
    34  	if err := json.NewDecoder(stream).Decode(&c); err != nil {
    35  		return err
    36  	}
    37  
    38  	if c.HostConfig.LogConfig.Type != "json-file" {
    39  		return fmt.Errorf("\"logs\" command is supported only for \"json-file\" logging driver")
    40  	}
    41  
    42  	v := url.Values{}
    43  	v.Set("stdout", "1")
    44  	v.Set("stderr", "1")
    45  
    46  	if *times {
    47  		v.Set("timestamps", "1")
    48  	}
    49  
    50  	if *follow {
    51  		v.Set("follow", "1")
    52  	}
    53  	v.Set("tail", *tail)
    54  
    55  	return cli.streamHelper("GET", "/containers/"+name+"/logs?"+v.Encode(), c.Config.Tty, nil, cli.out, cli.err, nil)
    56  }