mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
05dc9846e1
The `docker logs` command performed a client-side check if the container's logging driver was supported. Now that we allow the client to connect to both "older" and "newer" daemon versions, this check is best done daemon-side. This patch remove the check on the client side, and leaves validation to the daemon, which should be the source of truth. Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
77 lines
1.9 KiB
Go
77 lines
1.9 KiB
Go
package container
|
|
|
|
import (
|
|
"io"
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
"github.com/docker/docker/api/types"
|
|
"github.com/docker/docker/cli"
|
|
"github.com/docker/docker/cli/command"
|
|
"github.com/docker/docker/pkg/stdcopy"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
type logsOptions struct {
|
|
follow bool
|
|
since string
|
|
timestamps bool
|
|
details bool
|
|
tail string
|
|
|
|
container string
|
|
}
|
|
|
|
// NewLogsCommand creates a new cobra.Command for `docker logs`
|
|
func NewLogsCommand(dockerCli *command.DockerCli) *cobra.Command {
|
|
var opts logsOptions
|
|
|
|
cmd := &cobra.Command{
|
|
Use: "logs [OPTIONS] CONTAINER",
|
|
Short: "Fetch the logs of a container",
|
|
Args: cli.ExactArgs(1),
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
opts.container = args[0]
|
|
return runLogs(dockerCli, &opts)
|
|
},
|
|
}
|
|
|
|
flags := cmd.Flags()
|
|
flags.BoolVarP(&opts.follow, "follow", "f", false, "Follow log output")
|
|
flags.StringVar(&opts.since, "since", "", "Show logs since timestamp")
|
|
flags.BoolVarP(&opts.timestamps, "timestamps", "t", false, "Show timestamps")
|
|
flags.BoolVar(&opts.details, "details", false, "Show extra details provided to logs")
|
|
flags.StringVar(&opts.tail, "tail", "all", "Number of lines to show from the end of the logs")
|
|
return cmd
|
|
}
|
|
|
|
func runLogs(dockerCli *command.DockerCli, opts *logsOptions) error {
|
|
ctx := context.Background()
|
|
|
|
options := types.ContainerLogsOptions{
|
|
ShowStdout: true,
|
|
ShowStderr: true,
|
|
Since: opts.since,
|
|
Timestamps: opts.timestamps,
|
|
Follow: opts.follow,
|
|
Tail: opts.tail,
|
|
Details: opts.details,
|
|
}
|
|
responseBody, err := dockerCli.Client().ContainerLogs(ctx, opts.container, options)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer responseBody.Close()
|
|
|
|
c, err := dockerCli.Client().ContainerInspect(ctx, opts.container)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if c.Config.Tty {
|
|
_, err = io.Copy(dockerCli.Out(), responseBody)
|
|
} else {
|
|
_, err = stdcopy.StdCopy(dockerCli.Out(), dockerCli.Err(), responseBody)
|
|
}
|
|
return err
|
|
}
|