@@ -18,9 +18,11 @@ package container
18
18
19
19
import (
20
20
"context"
21
+ "encoding/json"
21
22
"fmt"
22
23
"os"
23
24
"os/signal"
25
+ "strings"
24
26
"syscall"
25
27
26
28
containerd "github.com/containerd/containerd/v2/client"
@@ -102,6 +104,44 @@ func Logs(ctx context.Context, client *containerd.Client, container string, opti
102
104
}
103
105
}
104
106
107
+ detailPrefix := ""
108
+ if options .Details {
109
+ if logConfigJSON , ok := l ["nerdctl/log-config" ]; ok {
110
+ type LogConfig struct {
111
+ Opts map [string ]string `json:"opts"`
112
+ }
113
+
114
+ e , err := getContainerEnvs (ctx , found .Container )
115
+ if err != nil {
116
+ return err
117
+ }
118
+
119
+ var logConfig LogConfig
120
+ if err := json .Unmarshal ([]byte (logConfigJSON ), & logConfig ); err == nil {
121
+ var optPairs []string
122
+
123
+ for k , v := range logConfig .Opts {
124
+ lowerKey := strings .ToLower (k )
125
+ if lowerKey == "env" {
126
+ if env , ok := e [v ]; ok {
127
+ optPairs = append (optPairs , fmt .Sprintf ("%s=%s" , v , env ))
128
+ }
129
+ }
130
+
131
+ if lowerKey == "labels" {
132
+ if label , ok := l [v ]; ok {
133
+ optPairs = append (optPairs , fmt .Sprintf ("%s=%s" , v , label ))
134
+ }
135
+ }
136
+ }
137
+
138
+ if len (optPairs ) > 0 {
139
+ detailPrefix = strings .Join (optPairs , "," )
140
+ }
141
+ }
142
+ }
143
+ }
144
+
105
145
logViewOpts := logging.LogViewOptions {
106
146
ContainerID : found .Container .ID (),
107
147
Namespace : l [labels .Namespace ],
@@ -112,6 +152,8 @@ func Logs(ctx context.Context, client *containerd.Client, container string, opti
112
152
Tail : options .Tail ,
113
153
Since : options .Since ,
114
154
Until : options .Until ,
155
+ Details : options .Details ,
156
+ DetailPrefix : detailPrefix ,
115
157
}
116
158
logViewer , err := logging .InitContainerLogViewer (l , logViewOpts , stopChannel , options .GOptions .Experimental )
117
159
if err != nil {
@@ -146,3 +188,25 @@ func getLogPath(ctx context.Context, container containerd.Container) (string, er
146
188
147
189
return meta .LogPath , nil
148
190
}
191
+
192
+ func getContainerEnvs (ctx context.Context , container containerd.Container ) (map [string ]string , error ) {
193
+ envMap := make (map [string ]string )
194
+
195
+ spec , err := container .Spec (ctx )
196
+ if err != nil {
197
+ return nil , err
198
+ }
199
+
200
+ if spec .Process == nil {
201
+ return envMap , nil
202
+ }
203
+
204
+ for _ , env := range spec .Process .Env {
205
+ parts := strings .SplitN (env , "=" , 2 )
206
+ if len (parts ) == 2 {
207
+ envMap [parts [0 ]] = parts [1 ]
208
+ }
209
+ }
210
+
211
+ return envMap , nil
212
+ }
0 commit comments