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"
|
2020-08-17 17:48:52 +02:00
|
|
|
"os"
|
2020-08-05 16:32:51 +02:00
|
|
|
|
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 {
|
|
|
|
*projectOptions
|
|
|
|
composeOptions
|
2021-02-18 16:50:39 +01:00
|
|
|
follow bool
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-08-31 15:41:20 +02:00
|
|
|
func logsCommand(p *projectOptions, backend api.Service) *cobra.Command {
|
2021-01-20 11:02:46 +01:00
|
|
|
opts := logsOptions{
|
|
|
|
projectOptions: p,
|
|
|
|
}
|
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 {
|
|
|
|
return runLogs(ctx, backend, opts, args)
|
|
|
|
}),
|
2022-09-26 19:21:45 +02:00
|
|
|
ValidArgsFunction: completeServiceNames(p),
|
2020-08-05 16:32:51 +02:00
|
|
|
}
|
2021-02-12 10:02:05 +01:00
|
|
|
flags := logsCmd.Flags()
|
2021-02-22 15:49:13 +01:00
|
|
|
flags.BoolVarP(&opts.follow, "follow", "f", false, "Follow log output.")
|
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)")
|
2021-02-12 10:02:05 +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.")
|
2021-02-18 16:50:39 +01:00
|
|
|
flags.BoolVarP(&opts.timestamps, "timestamps", "t", false, "Show timestamps.")
|
2021-08-31 15:41:20 +02:00
|
|
|
flags.StringVar(&opts.tail, "tail", "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
|
|
|
|
}
|
|
|
|
|
2021-06-14 16:26:14 +02:00
|
|
|
func runLogs(ctx context.Context, backend api.Service, opts logsOptions, services []string) error {
|
2022-11-28 22:07:36 +01:00
|
|
|
project, name, err := opts.projectOrName(services...)
|
2020-08-18 09:40:57 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-12-13 19:01:08 +01:00
|
|
|
consumer := formatter.NewLogConsumer(ctx, os.Stdout, os.Stderr, !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,
|
|
|
|
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
|
|
|
}
|