2016-09-08 13:11:39 -04:00
|
|
|
package container
|
|
|
|
|
|
|
|
import (
|
2018-05-03 21:02:44 -04:00
|
|
|
"context"
|
2016-09-08 13:11:39 -04:00
|
|
|
"io"
|
|
|
|
|
2017-04-17 18:07:56 -04:00
|
|
|
"github.com/docker/cli/cli"
|
|
|
|
"github.com/docker/cli/cli/command"
|
2022-03-30 09:27:25 -04:00
|
|
|
"github.com/docker/cli/cli/command/completion"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/pkg/stdcopy"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
type logsOptions struct {
|
|
|
|
follow bool
|
|
|
|
since string
|
2017-05-10 08:00:00 -04:00
|
|
|
until string
|
2016-09-08 13:11:39 -04:00
|
|
|
timestamps bool
|
|
|
|
details bool
|
|
|
|
tail string
|
|
|
|
|
|
|
|
container string
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewLogsCommand creates a new cobra.Command for `docker logs`
|
2017-10-11 12:18:27 -04:00
|
|
|
func NewLogsCommand(dockerCli command.Cli) *cobra.Command {
|
2016-09-08 13:11:39 -04:00
|
|
|
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)
|
|
|
|
},
|
2022-03-30 09:27:25 -04:00
|
|
|
ValidArgsFunction: completion.ContainerNames(dockerCli, true),
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
flags := cmd.Flags()
|
|
|
|
flags.BoolVarP(&opts.follow, "follow", "f", false, "Follow log output")
|
2020-05-28 07:56:07 -04:00
|
|
|
flags.StringVar(&opts.since, "since", "", "Show logs since timestamp (e.g. 2013-01-02T13:23:37Z) or relative (e.g. 42m for 42 minutes)")
|
|
|
|
flags.StringVar(&opts.until, "until", "", "Show logs before a timestamp (e.g. 2013-01-02T13:23:37Z) or relative (e.g. 42m for 42 minutes)")
|
2017-05-10 08:00:00 -04:00
|
|
|
flags.SetAnnotation("until", "version", []string{"1.35"})
|
2016-09-08 13:11:39 -04:00
|
|
|
flags.BoolVarP(&opts.timestamps, "timestamps", "t", false, "Show timestamps")
|
|
|
|
flags.BoolVar(&opts.details, "details", false, "Show extra details provided to logs")
|
2020-07-23 07:54:12 -04:00
|
|
|
flags.StringVarP(&opts.tail, "tail", "n", "all", "Number of lines to show from the end of the logs")
|
2016-09-08 13:11:39 -04:00
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2017-10-11 12:18:27 -04:00
|
|
|
func runLogs(dockerCli command.Cli, opts *logsOptions) error {
|
2016-09-08 13:11:39 -04:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2018-10-22 06:53:22 -04:00
|
|
|
c, err := dockerCli.Client().ContainerInspect(ctx, opts.container)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-09-08 13:11:39 -04:00
|
|
|
options := types.ContainerLogsOptions{
|
|
|
|
ShowStdout: true,
|
|
|
|
ShowStderr: true,
|
|
|
|
Since: opts.since,
|
2017-05-10 08:00:00 -04:00
|
|
|
Until: opts.until,
|
2016-09-08 13:11:39 -04:00
|
|
|
Timestamps: opts.timestamps,
|
|
|
|
Follow: opts.follow,
|
|
|
|
Tail: opts.tail,
|
|
|
|
Details: opts.details,
|
|
|
|
}
|
2018-10-22 06:53:22 -04:00
|
|
|
responseBody, err := dockerCli.Client().ContainerLogs(ctx, c.ID, options)
|
2016-09-08 13:11:39 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer responseBody.Close()
|
|
|
|
|
|
|
|
if c.Config.Tty {
|
|
|
|
_, err = io.Copy(dockerCli.Out(), responseBody)
|
|
|
|
} else {
|
|
|
|
_, err = stdcopy.StdCopy(dockerCli.Out(), dockerCli.Err(), responseBody)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|