logs.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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/docker/compose/v2/pkg/api"
  18. "github.com/docker/compose/v2/pkg/utils"
  19. "github.com/docker/docker/api/types"
  20. "github.com/docker/docker/pkg/stdcopy"
  21. "golang.org/x/sync/errgroup"
  22. )
  23. func (s *composeService) Logs(ctx context.Context, projectName string, consumer api.LogConsumer, options api.LogOptions) error {
  24. containers, err := s.getContainers(ctx, projectName, oneOffExclude, true, options.Services...)
  25. if err != nil {
  26. return err
  27. }
  28. eg, ctx := errgroup.WithContext(ctx)
  29. for _, c := range containers {
  30. c := c
  31. eg.Go(func() error {
  32. return s.logContainers(ctx, consumer, c, options)
  33. })
  34. }
  35. if options.Follow {
  36. printer := newLogPrinter(consumer)
  37. eg.Go(func() error {
  38. for _, c := range containers {
  39. printer.HandleEvent(api.ContainerEvent{
  40. Type: api.ContainerEventAttach,
  41. Container: getContainerNameWithoutProject(c),
  42. Service: c.Labels[api.ServiceLabel],
  43. })
  44. }
  45. return nil
  46. })
  47. eg.Go(func() error {
  48. return s.watchContainers(ctx, projectName, options.Services, printer.HandleEvent, containers, func(c types.Container) error {
  49. printer.HandleEvent(api.ContainerEvent{
  50. Type: api.ContainerEventAttach,
  51. Container: getContainerNameWithoutProject(c),
  52. Service: c.Labels[api.ServiceLabel],
  53. })
  54. return s.logContainers(ctx, consumer, c, options)
  55. })
  56. })
  57. eg.Go(func() error {
  58. _, err := printer.Run(ctx, false, "", nil)
  59. return err
  60. })
  61. }
  62. return eg.Wait()
  63. }
  64. func (s *composeService) logContainers(ctx context.Context, consumer api.LogConsumer, c types.Container, options api.LogOptions) error {
  65. cnt, err := s.apiClient().ContainerInspect(ctx, c.ID)
  66. if err != nil {
  67. return err
  68. }
  69. service := c.Labels[api.ServiceLabel]
  70. r, err := s.apiClient().ContainerLogs(ctx, cnt.ID, types.ContainerLogsOptions{
  71. ShowStdout: true,
  72. ShowStderr: true,
  73. Follow: options.Follow,
  74. Since: options.Since,
  75. Until: options.Until,
  76. Tail: options.Tail,
  77. Timestamps: options.Timestamps,
  78. })
  79. if err != nil {
  80. return err
  81. }
  82. defer r.Close() // nolint errcheck
  83. name := getContainerNameWithoutProject(c)
  84. w := utils.GetWriter(func(line string) {
  85. consumer.Log(name, service, line)
  86. })
  87. if cnt.Config.Tty {
  88. _, err = io.Copy(w, r)
  89. } else {
  90. _, err = stdcopy.StdCopy(w, w, r)
  91. }
  92. return err
  93. }