logs.go 4.4 KB

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