logs.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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"
  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. container, err := s.getSpecifiedContainer(ctx, projectName, oneOffExclude, true, options.Services[0], options.Index)
  37. if err != nil {
  38. return err
  39. }
  40. containers = append(containers, container)
  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 _, c := range containers {
  54. c := c
  55. eg.Go(func() error {
  56. err := s.logContainers(ctx, consumer, c, options)
  57. var notImplErr errdefs.ErrNotImplemented
  58. if errors.As(err, &notImplErr) {
  59. logrus.Warnf("Can't retrieve logs for %q: %s", getCanonicalContainerName(c), err.Error())
  60. return nil
  61. }
  62. return err
  63. })
  64. }
  65. if options.Follow {
  66. containers = containers.filter(isRunning())
  67. printer := newLogPrinter(consumer)
  68. eg.Go(func() error {
  69. _, err := printer.Run(false, "", nil)
  70. return err
  71. })
  72. for _, c := range containers {
  73. printer.HandleEvent(api.ContainerEvent{
  74. Type: api.ContainerEventAttach,
  75. Container: getContainerNameWithoutProject(c),
  76. ID: c.ID,
  77. Service: c.Labels[api.ServiceLabel],
  78. })
  79. }
  80. eg.Go(func() error {
  81. err := s.watchContainers(ctx, projectName, options.Services, nil, printer.HandleEvent, containers, func(c types.Container, t time.Time) error {
  82. printer.HandleEvent(api.ContainerEvent{
  83. Type: api.ContainerEventAttach,
  84. Container: getContainerNameWithoutProject(c),
  85. ID: c.ID,
  86. Service: c.Labels[api.ServiceLabel],
  87. })
  88. eg.Go(func() error {
  89. err := s.logContainers(ctx, consumer, c, api.LogOptions{
  90. Follow: options.Follow,
  91. Since: t.Format(time.RFC3339Nano),
  92. Until: options.Until,
  93. Tail: options.Tail,
  94. Timestamps: options.Timestamps,
  95. })
  96. var notImplErr errdefs.ErrNotImplemented
  97. if errors.As(err, &notImplErr) {
  98. // ignore
  99. return nil
  100. }
  101. return err
  102. })
  103. return nil
  104. }, func(c types.Container, t time.Time) error {
  105. printer.HandleEvent(api.ContainerEvent{
  106. Type: api.ContainerEventAttach,
  107. Container: "", // actual name will be set by start event
  108. ID: c.ID,
  109. Service: c.Labels[api.ServiceLabel],
  110. })
  111. return nil
  112. })
  113. printer.Stop()
  114. return err
  115. })
  116. }
  117. return eg.Wait()
  118. }
  119. func (s *composeService) logContainers(ctx context.Context, consumer api.LogConsumer, c types.Container, options api.LogOptions) error {
  120. cnt, err := s.apiClient().ContainerInspect(ctx, c.ID)
  121. if err != nil {
  122. return err
  123. }
  124. r, err := s.apiClient().ContainerLogs(ctx, cnt.ID, types.ContainerLogsOptions{
  125. ShowStdout: true,
  126. ShowStderr: true,
  127. Follow: options.Follow,
  128. Since: options.Since,
  129. Until: options.Until,
  130. Tail: options.Tail,
  131. Timestamps: options.Timestamps,
  132. })
  133. if err != nil {
  134. return err
  135. }
  136. defer r.Close() //nolint:errcheck
  137. name := getContainerNameWithoutProject(c)
  138. w := utils.GetWriter(func(line string) {
  139. consumer.Log(name, line)
  140. })
  141. if cnt.Config.Tty {
  142. _, err = io.Copy(w, r)
  143. } else {
  144. _, err = stdcopy.StdCopy(w, w, r)
  145. }
  146. return err
  147. }