@@ -18,9 +18,11 @@ package container
1818
1919import (
2020 "context"
21+ "encoding/json"
2122 "fmt"
2223 "os"
2324 "os/signal"
25+ "strings"
2426 "syscall"
2527
2628 containerd "github.com/containerd/containerd/v2/client"
@@ -102,6 +104,44 @@ func Logs(ctx context.Context, client *containerd.Client, container string, opti
102104 }
103105 }
104106
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+
105145 logViewOpts := logging.LogViewOptions {
106146 ContainerID : found .Container .ID (),
107147 Namespace : l [labels .Namespace ],
@@ -112,6 +152,8 @@ func Logs(ctx context.Context, client *containerd.Client, container string, opti
112152 Tail : options .Tail ,
113153 Since : options .Since ,
114154 Until : options .Until ,
155+ Details : options .Details ,
156+ DetailPrefix : detailPrefix ,
115157 }
116158 logViewer , err := logging .InitContainerLogViewer (l , logViewOpts , stopChannel , options .GOptions .Experimental )
117159 if err != nil {
@@ -146,3 +188,25 @@ func getLogPath(ctx context.Context, container containerd.Container) (string, er
146188
147189 return meta .LogPath , nil
148190}
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