@@ -62,7 +62,7 @@ func (s *composeService) Logs(
6262 eg , ctx := errgroup .WithContext (ctx )
6363 for _ , ctr := range containers {
6464 eg .Go (func () error {
65- err := s .logContainers (ctx , consumer , ctr , options )
65+ err := s .logContainer (ctx , consumer , ctr , options )
6666 if errdefs .IsNotImplemented (err ) {
6767 logrus .Warnf ("Can't retrieve logs for %q: %s" , getCanonicalContainerName (ctr ), err .Error ())
6868 return nil
@@ -72,34 +72,21 @@ func (s *composeService) Logs(
7272 }
7373
7474 if options .Follow {
75- containers = containers .filter (isRunning ())
7675 printer := newLogPrinter (consumer )
77- eg .Go (func () error {
78- _ , err := printer .Run (api .CascadeIgnore , "" , nil )
79- return err
80- })
81-
82- for _ , c := range containers {
83- printer .HandleEvent (api.ContainerEvent {
84- Type : api .ContainerEventAttach ,
85- Container : getContainerNameWithoutProject (c ),
86- ID : c .ID ,
87- Service : c .Labels [api .ServiceLabel ],
88- })
89- }
76+ eg .Go (printer .Run )
9077
91- eg .Go (func () error {
92- err := s .watchContainers (ctx , projectName , options .Services , nil , printer .HandleEvent , containers , func (c container.Summary , t time.Time ) error {
93- printer .HandleEvent (api.ContainerEvent {
94- Type : api .ContainerEventAttach ,
95- Container : getContainerNameWithoutProject (c ),
96- ID : c .ID ,
97- Service : c .Labels [api .ServiceLabel ],
98- })
78+ monitor := newMonitor (s .apiClient (), options .Project )
79+ monitor .withListener (func (event api.ContainerEvent ) {
80+ if event .Type == api .ContainerEventStarted {
9981 eg .Go (func () error {
100- err := s .logContainers (ctx , consumer , c , api.LogOptions {
82+ ctr , err := s .apiClient ().ContainerInspect (ctx , event .ID )
83+ if err != nil {
84+ return err
85+ }
86+
87+ err = s .doLogContainer (ctx , consumer , event .Source , ctr , api.LogOptions {
10188 Follow : options .Follow ,
102- Since : t .Format (time .RFC3339Nano ),
89+ Since : time . Unix ( 0 , event . Time ) .Format (time .RFC3339Nano ),
10390 Until : options .Until ,
10491 Tail : options .Tail ,
10592 Timestamps : options .Timestamps ,
@@ -110,31 +97,28 @@ func (s *composeService) Logs(
11097 }
11198 return err
11299 })
113- return nil
114- }, func (c container.Summary , t time.Time ) error {
115- printer .HandleEvent (api.ContainerEvent {
116- Type : api .ContainerEventAttach ,
117- Container : "" , // actual name will be set by start event
118- ID : c .ID ,
119- Service : c .Labels [api .ServiceLabel ],
120- })
121- return nil
122- })
123- printer .Stop ()
124- return err
100+ }
101+ })
102+ eg .Go (func () error {
103+ defer printer .Stop ()
104+ return monitor .Start (ctx )
125105 })
126106 }
127107
128108 return eg .Wait ()
129109}
130110
131- func (s * composeService ) logContainers (ctx context.Context , consumer api.LogConsumer , c container.Summary , options api.LogOptions ) error {
132- cnt , err := s .apiClient ().ContainerInspect (ctx , c .ID )
111+ func (s * composeService ) logContainer (ctx context.Context , consumer api.LogConsumer , c container.Summary , options api.LogOptions ) error {
112+ ctr , err := s .apiClient ().ContainerInspect (ctx , c .ID )
133113 if err != nil {
134114 return err
135115 }
116+ name := getContainerNameWithoutProject (c )
117+ return s .doLogContainer (ctx , consumer , name , ctr , options )
118+ }
136119
137- r , err := s .apiClient ().ContainerLogs (ctx , cnt .ID , container.LogsOptions {
120+ func (s * composeService ) doLogContainer (ctx context.Context , consumer api.LogConsumer , name string , ctr container.InspectResponse , options api.LogOptions ) error {
121+ r , err := s .apiClient ().ContainerLogs (ctx , ctr .ID , container.LogsOptions {
138122 ShowStdout : true ,
139123 ShowStderr : true ,
140124 Follow : options .Follow ,
@@ -148,11 +132,10 @@ func (s *composeService) logContainers(ctx context.Context, consumer api.LogCons
148132 }
149133 defer r .Close () //nolint:errcheck
150134
151- name := getContainerNameWithoutProject (c )
152135 w := utils .GetWriter (func (line string ) {
153136 consumer .Log (name , line )
154137 })
155- if cnt .Config .Tty {
138+ if ctr .Config .Tty {
156139 _ , err = io .Copy (w , r )
157140 } else {
158141 _ , err = stdcopy .StdCopy (w , w , r )
0 commit comments