logs.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. "io"
  17. "github.com/containerd/errdefs"
  18. "github.com/docker/docker/api/types/container"
  19. "github.com/docker/docker/pkg/stdcopy"
  20. "github.com/sirupsen/logrus"
  21. "golang.org/x/sync/errgroup"
  22. "github.com/docker/compose/v2/pkg/api"
  23. "github.com/docker/compose/v2/pkg/utils"
  24. )
  25. func (s *composeService) Logs(
  26. ctx context.Context,
  27. projectName string,
  28. consumer api.LogConsumer,
  29. options api.LogOptions,
  30. ) error {
  31. var containers Containers
  32. var err error
  33. if options.Index > 0 {
  34. ctr, err := s.getSpecifiedContainer(ctx, projectName, oneOffExclude, true, options.Services[0], options.Index)
  35. if err != nil {
  36. return err
  37. }
  38. containers = append(containers, ctr)
  39. } else {
  40. containers, err = s.getContainers(ctx, projectName, oneOffExclude, true, options.Services...)
  41. if err != nil {
  42. return err
  43. }
  44. }
  45. if options.Project != nil && len(options.Services) == 0 {
  46. // we run with an explicit compose.yaml, so only consider services defined in this file
  47. options.Services = options.Project.ServiceNames()
  48. containers = containers.filter(isService(options.Services...))
  49. }
  50. eg, ctx := errgroup.WithContext(ctx)
  51. for _, ctr := range containers {
  52. eg.Go(func() error {
  53. err := s.logContainer(ctx, consumer, ctr, options)
  54. if errdefs.IsNotImplemented(err) {
  55. logrus.Warnf("Can't retrieve logs for %q: %s", getCanonicalContainerName(ctr), err.Error())
  56. return nil
  57. }
  58. return err
  59. })
  60. }
  61. if options.Follow {
  62. printer := newLogPrinter(consumer)
  63. monitor := newMonitor(s.apiClient(), projectName)
  64. if len(options.Services) > 0 {
  65. monitor.withServices(options.Services)
  66. } else if options.Project != nil {
  67. monitor.withServices(options.Project.ServiceNames())
  68. }
  69. monitor.withListener(printer.HandleEvent)
  70. monitor.withListener(func(event api.ContainerEvent) {
  71. if event.Type == api.ContainerEventStarted {
  72. eg.Go(func() error {
  73. ctr, err := s.apiClient().ContainerInspect(ctx, event.ID)
  74. if err != nil {
  75. return err
  76. }
  77. err = s.doLogContainer(ctx, consumer, event.Source, ctr, api.LogOptions{
  78. Follow: options.Follow,
  79. Since: ctr.State.StartedAt,
  80. Until: options.Until,
  81. Tail: options.Tail,
  82. Timestamps: options.Timestamps,
  83. })
  84. if errdefs.IsNotImplemented(err) {
  85. // ignore
  86. return nil
  87. }
  88. return err
  89. })
  90. }
  91. })
  92. eg.Go(func() error {
  93. return monitor.Start(ctx)
  94. })
  95. }
  96. return eg.Wait()
  97. }
  98. func (s *composeService) logContainer(ctx context.Context, consumer api.LogConsumer, c container.Summary, options api.LogOptions) error {
  99. ctr, err := s.apiClient().ContainerInspect(ctx, c.ID)
  100. if err != nil {
  101. return err
  102. }
  103. name := getContainerNameWithoutProject(c)
  104. return s.doLogContainer(ctx, consumer, name, ctr, options)
  105. }
  106. func (s *composeService) doLogContainer(ctx context.Context, consumer api.LogConsumer, name string, ctr container.InspectResponse, options api.LogOptions) error {
  107. r, err := s.apiClient().ContainerLogs(ctx, ctr.ID, container.LogsOptions{
  108. ShowStdout: true,
  109. ShowStderr: true,
  110. Follow: options.Follow,
  111. Since: options.Since,
  112. Until: options.Until,
  113. Tail: options.Tail,
  114. Timestamps: options.Timestamps,
  115. })
  116. if err != nil {
  117. return err
  118. }
  119. defer r.Close() //nolint:errcheck
  120. w := utils.GetWriter(func(line string) {
  121. consumer.Log(name, line)
  122. })
  123. if ctr.Config.Tty {
  124. _, err = io.Copy(w, r)
  125. } else {
  126. _, err = stdcopy.StdCopy(w, w, r)
  127. }
  128. return err
  129. }