2020-08-05 16:32:51 +02:00
|
|
|
/*
|
2020-09-22 12:13:00 +02:00
|
|
|
Copyright 2020 Docker Compose CLI authors
|
2020-08-05 16:32:51 +02:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package compose
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-02-06 21:21:29 +11:00
|
|
|
"errors"
|
2020-08-05 16:32:51 +02:00
|
|
|
|
2023-08-30 10:15:35 +02:00
|
|
|
"github.com/docker/cli/cli/command"
|
2020-12-29 16:22:57 -03:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
2022-12-13 19:01:08 +01:00
|
|
|
"github.com/docker/compose/v2/cmd/formatter"
|
2021-08-31 18:53:24 +02:00
|
|
|
"github.com/docker/compose/v2/pkg/api"
|
2020-08-05 16:32:51 +02:00
|
|
|
)
|
|
|
|
|
2021-01-20 11:02:46 +01:00
|
|
|
type logsOptions struct {
|
2022-12-19 16:38:36 +00:00
|
|
|
*ProjectOptions
|
2021-01-20 11:02:46 +01:00
|
|
|
composeOptions
|
2021-02-18 16:50:39 +01:00
|
|
|
follow bool
|
2023-01-24 08:48:17 +11:00
|
|
|
index int
|
2021-02-18 16:50:39 +01:00
|
|
|
tail string
|
2021-06-15 00:14:30 +08:00
|
|
|
since string
|
2021-06-16 08:55:53 +08:00
|
|
|
until string
|
2021-02-18 16:50:39 +01:00
|
|
|
noColor bool
|
|
|
|
noPrefix bool
|
|
|
|
timestamps bool
|
2021-01-20 11:02:46 +01:00
|
|
|
}
|
|
|
|
|
2023-08-30 10:15:35 +02:00
|
|
|
func logsCommand(p *ProjectOptions, dockerCli command.Cli, backend api.Service) *cobra.Command {
|
2021-01-20 11:02:46 +01:00
|
|
|
opts := logsOptions{
|
2022-12-19 16:38:36 +00:00
|
|
|
ProjectOptions: p,
|
2021-01-20 11:02:46 +01:00
|
|
|
}
|
2020-08-05 16:32:51 +02:00
|
|
|
logsCmd := &cobra.Command{
|
2022-08-02 13:11:57 +02:00
|
|
|
Use: "logs [OPTIONS] [SERVICE...]",
|
2020-12-10 15:43:20 +01:00
|
|
|
Short: "View output from containers",
|
2021-04-15 12:43:18 +02:00
|
|
|
RunE: Adapt(func(ctx context.Context, args []string) error {
|
2023-08-30 10:15:35 +02:00
|
|
|
return runLogs(ctx, dockerCli, backend, opts, args)
|
2021-04-15 12:43:18 +02:00
|
|
|
}),
|
2023-02-06 21:21:29 +11:00
|
|
|
PreRunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
if opts.index > 0 && len(args) != 1 {
|
|
|
|
return errors.New("--index requires one service to be selected")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
2023-08-30 10:15:35 +02:00
|
|
|
ValidArgsFunction: completeServiceNames(dockerCli, p),
|
2020-08-05 16:32:51 +02:00
|
|
|
}
|
2021-02-12 10:02:05 +01:00
|
|
|
flags := logsCmd.Flags()
|
2024-02-14 20:02:37 +01:00
|
|
|
flags.BoolVarP(&opts.follow, "follow", "f", false, "Follow log output")
|
2023-02-06 21:21:29 +11:00
|
|
|
flags.IntVar(&opts.index, "index", 0, "index of the container if service has multiple replicas")
|
2021-06-16 08:55:53 +08: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)")
|
2024-02-14 20:02:37 +01:00
|
|
|
flags.BoolVar(&opts.noColor, "no-color", false, "Produce monochrome output")
|
|
|
|
flags.BoolVar(&opts.noPrefix, "no-log-prefix", false, "Don't print prefix in logs")
|
|
|
|
flags.BoolVarP(&opts.timestamps, "timestamps", "t", false, "Show timestamps")
|
|
|
|
flags.StringVarP(&opts.tail, "tail", "n", "all", "Number of lines to show from the end of the logs for each container")
|
2020-08-05 16:32:51 +02:00
|
|
|
return logsCmd
|
|
|
|
}
|
|
|
|
|
2023-08-30 10:15:35 +02:00
|
|
|
func runLogs(ctx context.Context, dockerCli command.Cli, backend api.Service, opts logsOptions, services []string) error {
|
2024-02-05 17:38:15 -05:00
|
|
|
project, name, err := opts.projectOrName(ctx, dockerCli, services...)
|
2020-08-18 09:40:57 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-02-22 18:27:05 +01:00
|
|
|
|
|
|
|
// exclude services configured to ignore output (attach: false), until explicitly selected
|
|
|
|
if project != nil && len(services) == 0 {
|
|
|
|
for n, service := range project.Services {
|
|
|
|
if service.Attach == nil || *service.Attach {
|
|
|
|
services = append(services, n)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-30 10:15:35 +02:00
|
|
|
consumer := formatter.NewLogConsumer(ctx, dockerCli.Out(), dockerCli.Err(), !opts.noColor, !opts.noPrefix, false)
|
2022-09-08 16:26:00 -04:00
|
|
|
return backend.Logs(ctx, name, consumer, api.LogOptions{
|
|
|
|
Project: project,
|
2021-02-22 14:57:49 +01:00
|
|
|
Services: services,
|
|
|
|
Follow: opts.follow,
|
2023-02-06 21:21:29 +11:00
|
|
|
Index: opts.index,
|
2021-02-22 14:57:49 +01:00
|
|
|
Tail: opts.tail,
|
2021-06-15 00:14:30 +08:00
|
|
|
Since: opts.since,
|
2021-06-16 08:55:53 +08:00
|
|
|
Until: opts.until,
|
2021-02-18 16:50:39 +01:00
|
|
|
Timestamps: opts.timestamps,
|
2020-12-16 16:58:54 +01:00
|
|
|
})
|
2020-08-05 16:32:51 +02:00
|
|
|
}
|