logs.go 4.1 KB

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