logs.go 3.9 KB

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