diff --git a/cmd/nerdctl/container/container_logs.go b/cmd/nerdctl/container/container_logs.go index 4c8a3095bb9..0543e97e0eb 100644 --- a/cmd/nerdctl/container/container_logs.go +++ b/cmd/nerdctl/container/container_logs.go @@ -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 } @@ -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(), @@ -97,6 +102,7 @@ func logsOptions(cmd *cobra.Command) (types.ContainerLogsOptions, error) { Tail: tail, Since: since, Until: until, + Details: details, }, nil } diff --git a/cmd/nerdctl/container/container_logs_test.go b/cmd/nerdctl/container/container_logs_test.go index 94c561fafb4..2d195c698fd 100644 --- a/cmd/nerdctl/container/container_logs_test.go +++ b/cmd/nerdctl/container/container_logs_test.go @@ -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( + expect.Contains("ENV=foo"), + expect.Contains("LABEL=bar"), + expect.Contains("baz"), + )) + + testCase.Run(t) +} diff --git a/docs/command-reference.md b/docs/command-reference.md index a4d2f7af7be..d381b9e227c 100644 --- a/docs/command-reference.md +++ b/docs/command-reference.md @@ -326,8 +326,12 @@ Logging flags: - :nerd_face: `--log-opt=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 `.1`. - Default: `////-json.log` - Example: `/var/lib/nerdctl/1935db59/containers/default//-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. - :whale: `--log-driver=journald`: Writes log messages to `journald`. The `journald` daemon must be running on the host machine. - :whale: `--log-opt=tag=