github.com/portworx/docker@v1.12.1/api/client/container/logs.go (about)

     1  package container
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  
     7  	"golang.org/x/net/context"
     8  
     9  	"github.com/docker/docker/api/client"
    10  	"github.com/docker/docker/cli"
    11  	"github.com/docker/docker/pkg/stdcopy"
    12  	"github.com/docker/engine-api/types"
    13  	"github.com/spf13/cobra"
    14  )
    15  
    16  var validDrivers = map[string]bool{
    17  	"json-file": true,
    18  	"journald":  true,
    19  }
    20  
    21  type logsOptions struct {
    22  	follow     bool
    23  	since      string
    24  	timestamps bool
    25  	details    bool
    26  	tail       string
    27  
    28  	container string
    29  }
    30  
    31  // NewLogsCommand creats a new cobra.Command for `docker logs`
    32  func NewLogsCommand(dockerCli *client.DockerCli) *cobra.Command {
    33  	var opts logsOptions
    34  
    35  	cmd := &cobra.Command{
    36  		Use:   "logs [OPTIONS] CONTAINER",
    37  		Short: "Fetch the logs of a container",
    38  		Args:  cli.ExactArgs(1),
    39  		RunE: func(cmd *cobra.Command, args []string) error {
    40  			opts.container = args[0]
    41  			return runLogs(dockerCli, &opts)
    42  		},
    43  	}
    44  	cmd.SetFlagErrorFunc(flagErrorFunc)
    45  
    46  	flags := cmd.Flags()
    47  	flags.BoolVarP(&opts.follow, "follow", "f", false, "Follow log output")
    48  	flags.StringVar(&opts.since, "since", "", "Show logs since timestamp")
    49  	flags.BoolVarP(&opts.timestamps, "timestamps", "t", false, "Show timestamps")
    50  	flags.BoolVar(&opts.details, "details", false, "Show extra details provided to logs")
    51  	flags.StringVar(&opts.tail, "tail", "all", "Number of lines to show from the end of the logs")
    52  	return cmd
    53  }
    54  
    55  func runLogs(dockerCli *client.DockerCli, opts *logsOptions) error {
    56  	ctx := context.Background()
    57  
    58  	c, err := dockerCli.Client().ContainerInspect(ctx, opts.container)
    59  	if err != nil {
    60  		return err
    61  	}
    62  
    63  	if !validDrivers[c.HostConfig.LogConfig.Type] {
    64  		return fmt.Errorf("\"logs\" command is supported only for \"json-file\" and \"journald\" logging drivers (got: %s)", c.HostConfig.LogConfig.Type)
    65  	}
    66  
    67  	options := types.ContainerLogsOptions{
    68  		ShowStdout: true,
    69  		ShowStderr: true,
    70  		Since:      opts.since,
    71  		Timestamps: opts.timestamps,
    72  		Follow:     opts.follow,
    73  		Tail:       opts.tail,
    74  		Details:    opts.details,
    75  	}
    76  	responseBody, err := dockerCli.Client().ContainerLogs(ctx, opts.container, options)
    77  	if err != nil {
    78  		return err
    79  	}
    80  	defer responseBody.Close()
    81  
    82  	if c.Config.Tty {
    83  		_, err = io.Copy(dockerCli.Out(), responseBody)
    84  	} else {
    85  		_, err = stdcopy.StdCopy(dockerCli.Out(), dockerCli.Err(), responseBody)
    86  	}
    87  	return err
    88  }