@@ -6,6 +6,7 @@ package stack
6
6
7
7
import (
8
8
"context"
9
+ "errors"
9
10
"fmt"
10
11
"os"
11
12
"path/filepath"
@@ -75,7 +76,17 @@ func dumpStackLogs(ctx context.Context, options DumpOptions) ([]DumpResult, erro
75
76
}
76
77
}
77
78
79
+ var logsPath string
80
+ if options .Output != "" {
81
+ logsPath = filepath .Join (options .Output , "logs" )
82
+ err = os .MkdirAll (logsPath , 0755 )
83
+ if err != nil {
84
+ return nil , fmt .Errorf ("can't create output location (path: %s): %w" , logsPath , err )
85
+ }
86
+ }
87
+
78
88
var results []DumpResult
89
+ var containerErrors error
79
90
for _ , serviceName := range services {
80
91
if len (options .Services ) > 0 && ! slices .Contains (options .Services , serviceName ) {
81
92
continue
@@ -85,7 +96,8 @@ func dumpStackLogs(ctx context.Context, options DumpOptions) ([]DumpResult, erro
85
96
86
97
content , err := dockerComposeLogsSince (ctx , serviceName , options .Profile , options .Since )
87
98
if err != nil {
88
- return nil , fmt .Errorf ("can't fetch service logs (service: %s): %v" , serviceName , err )
99
+ containerErrors = errors .Join (containerErrors , fmt .Errorf ("can't fetch service logs (service: %s): %v" , serviceName , err ))
100
+ continue
89
101
}
90
102
if options .Output == "" {
91
103
results = append (results , DumpResult {
@@ -99,30 +111,30 @@ func dumpStackLogs(ctx context.Context, options DumpOptions) ([]DumpResult, erro
99
111
ServiceName : serviceName ,
100
112
}
101
113
102
- logsPath := filepath .Join (options .Output , "logs" )
103
- err = os .MkdirAll (logsPath , 0755 )
104
- if err != nil {
105
- return nil , fmt .Errorf ("can't create output location (path: %s): %w" , logsPath , err )
106
- }
107
-
108
114
logPath , err := writeLogFiles (logsPath , serviceName , content )
109
115
if err != nil {
110
- return nil , fmt .Errorf ("can't write log files for service %q: %w" , serviceName , err )
116
+ containerErrors = errors .Join (containerErrors , fmt .Errorf ("can't write log files for service %q: %w" , serviceName , err ))
117
+ continue
111
118
}
112
119
result .LogsFile = logPath
113
120
114
121
switch serviceName {
115
122
case elasticAgentService , fleetServerService :
116
123
logPath , err := copyDockerInternalLogs (serviceName , logsPath , options .Profile )
117
124
if err != nil {
118
- return nil , fmt .Errorf ("can't copy internal logs (service: %s): %w" , serviceName , err )
125
+ containerErrors = errors .Join (containerErrors , fmt .Errorf ("can't copy internal logs for service %q: %w" , serviceName , err ))
126
+ continue
119
127
}
120
128
result .InternalLogsDir = logPath
121
129
}
122
130
123
131
results = append (results , result )
124
132
}
125
133
134
+ if containerErrors != nil {
135
+ return nil , fmt .Errorf ("failed to dump stack logs: %w" , containerErrors )
136
+ }
137
+
126
138
return results , nil
127
139
}
128
140
0 commit comments