Skip to content

feat: add --details flag to logs command #4009

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions cmd/nerdctl/container/container_logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ The following containers are supported:
cmd.Flags().StringP("tail", "n", "all", "Number of lines to show from the end of the logs")
cmd.Flags().String("since", "", "Show logs since timestamp (e.g. 2013-01-02T13:23:37Z) or relative (e.g. 42m for 42 minutes)")
cmd.Flags().String("until", "", "Show logs before a timestamp (e.g. 2013-01-02T13:23:37Z) or relative (e.g. 42m for 42 minutes)")
cmd.Flags().Bool("details", false, "Show extra details provided to logs")
return cmd
}

Expand Down Expand Up @@ -88,6 +89,10 @@ func logsOptions(cmd *cobra.Command) (types.ContainerLogsOptions, error) {
if err != nil {
return types.ContainerLogsOptions{}, err
}
details, err := cmd.Flags().GetBool("details")
if err != nil {
return types.ContainerLogsOptions{}, err
}
return types.ContainerLogsOptions{
Stdout: cmd.OutOrStdout(),
Stderr: cmd.OutOrStderr(),
Expand All @@ -97,6 +102,7 @@ func logsOptions(cmd *cobra.Command) (types.ContainerLogsOptions, error) {
Tail: tail,
Since: since,
Until: until,
Details: details,
}, nil
}

Expand Down
32 changes: 32 additions & 0 deletions cmd/nerdctl/container/container_logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,3 +345,35 @@ func TestNoneLoggerHasNoLogURI(t *testing.T) {
testCase.Expected = test.Expects(1, nil, nil)
testCase.Run(t)
}

func TestLogsWithDetails(t *testing.T) {
testCase := nerdtest.Setup()

testCase.Setup = func(data test.Data, helpers test.Helpers) {
helpers.Ensure("run", "-d", "--log-driver", "json-file",
"--log-opt", "max-size=10m",
"--log-opt", "max-file=3",
"--log-opt", "env=ENV",
"--env", "ENV=foo",
"--log-opt", "labels=LABEL",
"--label", "LABEL=bar",
"--name", data.Identifier(), testutil.CommonImage,
"sh", "-ec", "echo baz")
}

testCase.Cleanup = func(data test.Data, helpers test.Helpers) {
helpers.Anyhow("rm", "-f", data.Identifier())
}

testCase.Command = func(data test.Data, helpers test.Helpers) test.TestableCommand {
return helpers.Command("logs", "--details", data.Identifier())
}

testCase.Expected = test.Expects(0, nil, expect.All(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Recently, we merged the ability for expect.Contains to accept multiple arguments.

So, if you want, you can replace this with:

testCase.Expected = test.Expects(0, nil, expect.Contains("ENV=foo", "LABEL=bar", "baz")

Which is more compact / readable.

expect.Contains("ENV=foo"),
expect.Contains("LABEL=bar"),
expect.Contains("baz"),
))

testCase.Run(t)
}
7 changes: 5 additions & 2 deletions docs/command-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -326,8 +326,12 @@ Logging flags:
- :nerd_face: `--log-opt=log-path=<LOG-PATH>`: The log path where the logs are written. The path will be created if it does not exist. If the log file exists, the old file will be renamed to `<LOG-PATH>.1`.
- Default: `<data-root>/<containerd-socket-hash>/<namespace>/<container-id>/<container-id>-json.log`
- Example: `/var/lib/nerdctl/1935db59/containers/default/<container-id>/<container-id>-json.log`
- :whale: `--log-opt labels=production_status,geo`: Applies when starting the Docker daemon. A comma-separated list of logging-related labels this daemon accepts.
- :whale: `--log-opt env=os,customer`: Applies when starting the Docker daemon. A comma-separated list of logging-related environment variables this daemon accepts.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Docker daemon ?

- :whale: `--log-driver=journald`: Writes log messages to `journald`. The `journald` daemon must be running on the host machine.
- :whale: `--log-opt=tag=<TEMPLATE>`: Specify template to set `SYSLOG_IDENTIFIER` value in journald logs.
- :whale: `--log-opt labels=production_status,geo`: Applies when starting the Docker daemon. A comma-separated list of logging-related labels this daemon accepts.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Docker daemon ?

- :whale: `--log-opt env=os,customer`: Applies when starting the Docker daemon. A comma-separated list of logging-related environment variables this daemon accepts.
- :whale: `--log-driver=fluentd`: Writes log messages to `fluentd`. The `fluentd` daemon must be running on the host machine.
- The `fluentd` logging driver supports the following logging options:
- :whale: `--log-opt=fluentd-address=<ADDRESS>`: The address of the `fluentd` daemon, tcp(default) and unix sockets are supported..
Expand Down Expand Up @@ -542,14 +546,13 @@ Usage: `nerdctl logs [OPTIONS] CONTAINER`

Flags:

- :whale: `--details`: Show extra details provided to logs
- :whale: `-f, --follow`: Follow log output
- :whale: `--since`: Show logs since timestamp (e.g. 2013-01-02T13:23:37Z) or relative (e.g. 42m for 42 minutes)
- :whale: `--until`: Show logs before a timestamp (e.g. 2013-01-02T13:23:37Z) or relative (e.g. 42m for 42 minutes)
- :whale: `-t, --timestamps`: Show timestamps
- :whale: `-n, --tail`: Number of lines to show from the end of the logs (default "all")

Unimplemented `docker logs` flags: `--details`

### :whale: nerdctl port

List port mappings or a specific mapping for the container.
Expand Down
2 changes: 2 additions & 0 deletions pkg/api/types/container_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,8 @@ type ContainerLogsOptions struct {
Since string
// Show logs before a timestamp (e.g., 2013-01-02T13:23:37Z) or relative (e.g., 42m for 42 minutes).
Until string
// Details specifies whether to show extra details provided to logs
Details bool
}

// ContainerWaitOptions specifies options for `nerdctl (container) wait`.
Expand Down
85 changes: 85 additions & 0 deletions pkg/cmd/container/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@ package container

import (
"context"
"encoding/json"
"fmt"
"os"
"os/signal"
"sort"
"strings"
"syscall"

containerd "github.com/containerd/containerd/v2/client"
Expand Down Expand Up @@ -102,6 +105,44 @@ func Logs(ctx context.Context, client *containerd.Client, container string, opti
}
}

detailPrefix := ""
if options.Details {
if logConfigJSON, ok := l["nerdctl/log-config"]; ok {
type LogConfig struct {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
type LogConfig struct {
type logConfig struct {

Opts map[string]string `json:"opts"`
}

e, err := getContainerEnvs(ctx, found.Container)
if err != nil {
return err
}

var logConfig LogConfig
var optPairs []string

if err := json.Unmarshal([]byte(logConfigJSON), &logConfig); err == nil {
envOpts, labelOpts := getLogOpts(logConfig.Opts)

for _, v := range envOpts {
if env, ok := e[v]; ok {
optPairs = append(optPairs, fmt.Sprintf("%s=%s", v, env))
}
}

for _, v := range labelOpts {
if label, ok := l[v]; ok {
optPairs = append(optPairs, fmt.Sprintf("%s=%s", v, label))
}
}

if len(optPairs) > 0 {
sort.Strings(optPairs)
detailPrefix = strings.Join(optPairs, ",")
}
}
}
}

logViewOpts := logging.LogViewOptions{
ContainerID: found.Container.ID(),
Namespace: l[labels.Namespace],
Expand All @@ -112,6 +153,8 @@ func Logs(ctx context.Context, client *containerd.Client, container string, opti
Tail: options.Tail,
Since: options.Since,
Until: options.Until,
Details: options.Details,
DetailPrefix: detailPrefix,
Copy link
Member

@fahedouch fahedouch Apr 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

detailPrefix as *string is enough

}
logViewer, err := logging.InitContainerLogViewer(l, logViewOpts, stopChannel, options.GOptions.Experimental)
if err != nil {
Expand Down Expand Up @@ -146,3 +189,45 @@ func getLogPath(ctx context.Context, container containerd.Container) (string, er

return meta.LogPath, nil
}

func getContainerEnvs(ctx context.Context, container containerd.Container) (map[string]string, error) {
envMap := make(map[string]string)

spec, err := container.Spec(ctx)
if err != nil {
return nil, err
}

if spec.Process == nil {
return envMap, nil
}

for _, env := range spec.Process.Env {
parts := strings.SplitN(env, "=", 2)
if len(parts) == 2 {
envMap[parts[0]] = parts[1]
}
}

return envMap, nil
}

func getLogOpts(logOpts map[string]string) ([]string, []string) {
var envOpts []string
var labelOpts []string

for k, v := range logOpts {
lowerKey := strings.ToLower(k)
if lowerKey == "env" {
envNames := strings.Split(v, ",")
envOpts = append(envOpts, envNames...)
}

if lowerKey == "labels" {
labelNames := strings.Split(v, ",")
labelOpts = append(labelOpts, labelNames...)
}
}

return envOpts, labelOpts
}
42 changes: 42 additions & 0 deletions pkg/logging/detail_writer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
Copyright The containerd Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package logging

import "io"

type DetailWriter struct {
w io.Writer
prefix string
}

func NewDetailWriter(w io.Writer, prefix string) io.Writer {
return &DetailWriter{
w: w,
prefix: prefix,
}
}

func (dw *DetailWriter) Write(p []byte) (n int, err error) {
if len(p) > 0 {
if _, err = dw.w.Write([]byte(dw.prefix)); err != nil {
return 0, err
}

return dw.w.Write(p)
}
return 0, nil
}
2 changes: 2 additions & 0 deletions pkg/logging/journald_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ import (

var JournalDriverLogOpts = []string{
Tag,
Env,
Labels,
}

func JournalLogOptsValidate(logOptMap map[string]string) error {
Expand Down
2 changes: 2 additions & 0 deletions pkg/logging/json_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ var JSONDriverLogOpts = []string{
LogPath,
MaxSize,
MaxFile,
Env,
Labels,
}

type JSONLogger struct {
Expand Down
15 changes: 15 additions & 0 deletions pkg/logging/log_viewer.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ type LogViewOptions struct {
// Start/end timestampts to filter logs by.
Since string
Until string

// Details enables showing extra details(env and label) in logs.
Details bool

// DetailPrefix is the prefix added when Details is enabled.
DetailPrefix string
}

func (lvo *LogViewOptions) Validate() error {
Expand Down Expand Up @@ -150,6 +156,15 @@ func InitContainerLogViewer(containerLabels map[string]string, lvopts LogViewOpt

// Prints all logs for this LogViewer's containers to the provided io.Writers.
func (lv *ContainerLogViewer) PrintLogsTo(stdout, stderr io.Writer) error {
if lv.logViewingOptions.Details {
prefix := " "
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why default space ?

if lv.logViewingOptions.DetailPrefix != "" {
prefix = lv.logViewingOptions.DetailPrefix + " "
}

stdout = NewDetailWriter(stdout, prefix)
stderr = NewDetailWriter(stderr, prefix)
}
viewerFunc, err := getLogViewer(lv.loggingConfig.Driver)
if err != nil {
return err
Expand Down
2 changes: 2 additions & 0 deletions pkg/logging/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ const (
MaxSize = "max-size"
MaxFile = "max-file"
Tag = "tag"
Env = "env"
Labels = "labels"
)

type Driver interface {
Expand Down