logs.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. Copyright 2020 Docker Compose CLI authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package compose
  14. import (
  15. "context"
  16. "github.com/spf13/cobra"
  17. "github.com/docker/compose/v2/cmd/formatter"
  18. "github.com/docker/compose/v2/pkg/api"
  19. )
  20. type logsOptions struct {
  21. *ProjectOptions
  22. composeOptions
  23. follow bool
  24. tail string
  25. since string
  26. until string
  27. noColor bool
  28. noPrefix bool
  29. timestamps bool
  30. }
  31. func logsCommand(p *ProjectOptions, streams api.Streams, backend api.Service) *cobra.Command {
  32. opts := logsOptions{
  33. ProjectOptions: p,
  34. }
  35. logsCmd := &cobra.Command{
  36. Use: "logs [OPTIONS] [SERVICE...]",
  37. Short: "View output from containers",
  38. RunE: Adapt(func(ctx context.Context, args []string) error {
  39. return runLogs(ctx, streams, backend, opts, args)
  40. }),
  41. ValidArgsFunction: completeServiceNames(p),
  42. }
  43. flags := logsCmd.Flags()
  44. flags.BoolVarP(&opts.follow, "follow", "f", false, "Follow log output.")
  45. flags.StringVar(&opts.since, "since", "", "Show logs since timestamp (e.g. 2013-01-02T13:23:37Z) or relative (e.g. 42m for 42 minutes)")
  46. 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)")
  47. flags.BoolVar(&opts.noColor, "no-color", false, "Produce monochrome output.")
  48. flags.BoolVar(&opts.noPrefix, "no-log-prefix", false, "Don't print prefix in logs.")
  49. flags.BoolVarP(&opts.timestamps, "timestamps", "t", false, "Show timestamps.")
  50. flags.StringVarP(&opts.tail, "tail", "n", "all", "Number of lines to show from the end of the logs for each container.")
  51. return logsCmd
  52. }
  53. func runLogs(ctx context.Context, streams api.Streams, backend api.Service, opts logsOptions, services []string) error {
  54. project, name, err := opts.projectOrName(services...)
  55. if err != nil {
  56. return err
  57. }
  58. consumer := formatter.NewLogConsumer(ctx, streams.Out(), streams.Err(), !opts.noColor, !opts.noPrefix, false)
  59. return backend.Logs(ctx, name, consumer, api.LogOptions{
  60. Project: project,
  61. Services: services,
  62. Follow: opts.follow,
  63. Tail: opts.tail,
  64. Since: opts.since,
  65. Until: opts.until,
  66. Timestamps: opts.timestamps,
  67. })
  68. }