logs.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. "github.com/containerd/errdefs"
  18. "github.com/moby/moby/api/pkg/stdcopy"
  19. "github.com/moby/moby/api/types/container"
  20. "github.com/moby/moby/client"
  21. "github.com/sirupsen/logrus"
  22. "golang.org/x/sync/errgroup"
  23. "github.com/docker/compose/v5/pkg/api"
  24. "github.com/docker/compose/v5/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. monitor := newMonitor(s.apiClient(), projectName)
  65. if len(options.Services) > 0 {
  66. monitor.withServices(options.Services)
  67. } else if options.Project != nil {
  68. monitor.withServices(options.Project.ServiceNames())
  69. }
  70. monitor.withListener(printer.HandleEvent)
  71. monitor.withListener(func(event api.ContainerEvent) {
  72. if event.Type == api.ContainerEventStarted {
  73. eg.Go(func() error {
  74. res, err := s.apiClient().ContainerInspect(ctx, event.ID, client.ContainerInspectOptions{})
  75. if err != nil {
  76. return err
  77. }
  78. err = s.doLogContainer(ctx, consumer, event.Source, res.Container, api.LogOptions{
  79. Follow: options.Follow,
  80. Since: res.Container.State.StartedAt,
  81. Until: options.Until,
  82. Tail: options.Tail,
  83. Timestamps: options.Timestamps,
  84. })
  85. if errdefs.IsNotImplemented(err) {
  86. // ignore
  87. return nil
  88. }
  89. return err
  90. })
  91. }
  92. })
  93. eg.Go(func() error {
  94. // pass ctx so monitor will immediately stop on SIGINT
  95. return monitor.Start(ctx)
  96. })
  97. }
  98. return eg.Wait()
  99. }
  100. func (s *composeService) logContainer(ctx context.Context, consumer api.LogConsumer, c container.Summary, options api.LogOptions) error {
  101. res, err := s.apiClient().ContainerInspect(ctx, c.ID, client.ContainerInspectOptions{})
  102. if err != nil {
  103. return err
  104. }
  105. name := getContainerNameWithoutProject(c)
  106. return s.doLogContainer(ctx, consumer, name, res.Container, options)
  107. }
  108. func (s *composeService) doLogContainer(ctx context.Context, consumer api.LogConsumer, name string, ctr container.InspectResponse, options api.LogOptions) error {
  109. r, err := s.apiClient().ContainerLogs(ctx, ctr.ID, client.ContainerLogsOptions{
  110. ShowStdout: true,
  111. ShowStderr: true,
  112. Follow: options.Follow,
  113. Since: options.Since,
  114. Until: options.Until,
  115. Tail: options.Tail,
  116. Timestamps: options.Timestamps,
  117. })
  118. if err != nil {
  119. return err
  120. }
  121. defer r.Close() //nolint:errcheck
  122. w := utils.GetWriter(func(line string) {
  123. consumer.Log(name, line)
  124. })
  125. if ctr.Config.Tty {
  126. _, err = io.Copy(w, r)
  127. } else {
  128. _, err = stdcopy.StdCopy(w, w, r)
  129. }
  130. return err
  131. }