logs.go 4.2 KB

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