logs.go 4.1 KB

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