logs.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. "errors"
  17. "github.com/docker/cli/cli/command"
  18. "github.com/spf13/cobra"
  19. "github.com/docker/compose/v2/cmd/formatter"
  20. "github.com/docker/compose/v2/pkg/api"
  21. )
  22. type logsOptions struct {
  23. *ProjectOptions
  24. composeOptions
  25. follow bool
  26. index int
  27. tail string
  28. since string
  29. until string
  30. noColor bool
  31. noPrefix bool
  32. timestamps bool
  33. }
  34. func logsCommand(p *ProjectOptions, dockerCli command.Cli, backend api.Service) *cobra.Command {
  35. opts := logsOptions{
  36. ProjectOptions: p,
  37. }
  38. logsCmd := &cobra.Command{
  39. Use: "logs [OPTIONS] [SERVICE...]",
  40. Short: "View output from containers",
  41. RunE: Adapt(func(ctx context.Context, args []string) error {
  42. return runLogs(ctx, dockerCli, backend, opts, args)
  43. }),
  44. PreRunE: func(cmd *cobra.Command, args []string) error {
  45. if opts.index > 0 && len(args) != 1 {
  46. return errors.New("--index requires one service to be selected")
  47. }
  48. return nil
  49. },
  50. ValidArgsFunction: completeServiceNames(dockerCli, p),
  51. }
  52. flags := logsCmd.Flags()
  53. flags.BoolVarP(&opts.follow, "follow", "f", false, "Follow log output")
  54. flags.IntVar(&opts.index, "index", 0, "index of the container if service has multiple replicas")
  55. flags.StringVar(&opts.since, "since", "", "Show logs since timestamp (e.g. 2013-01-02T13:23:37Z) or relative (e.g. 42m for 42 minutes)")
  56. 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)")
  57. flags.BoolVar(&opts.noColor, "no-color", false, "Produce monochrome output")
  58. flags.BoolVar(&opts.noPrefix, "no-log-prefix", false, "Don't print prefix in logs")
  59. flags.BoolVarP(&opts.timestamps, "timestamps", "t", false, "Show timestamps")
  60. flags.StringVarP(&opts.tail, "tail", "n", "all", "Number of lines to show from the end of the logs for each container")
  61. return logsCmd
  62. }
  63. func runLogs(ctx context.Context, dockerCli command.Cli, backend api.Service, opts logsOptions, services []string) error {
  64. project, name, err := opts.projectOrName(ctx, dockerCli, services...)
  65. if err != nil {
  66. return err
  67. }
  68. // exclude services configured to ignore output (attach: false), until explicitly selected
  69. if project != nil && len(services) == 0 {
  70. for n, service := range project.Services {
  71. if service.Attach == nil || *service.Attach {
  72. services = append(services, n)
  73. }
  74. }
  75. }
  76. consumer := formatter.NewLogConsumer(ctx, dockerCli.Out(), dockerCli.Err(), !opts.noColor, !opts.noPrefix, false)
  77. return backend.Logs(ctx, name, consumer, api.LogOptions{
  78. Project: project,
  79. Services: services,
  80. Follow: opts.follow,
  81. Index: opts.index,
  82. Tail: opts.tail,
  83. Since: opts.since,
  84. Until: opts.until,
  85. Timestamps: opts.timestamps,
  86. })
  87. }