logs.go 4.0 KB

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