-
Notifications
You must be signed in to change notification settings - Fork 649
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
- :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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.. | ||
|
@@ -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. | ||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -18,9 +18,12 @@ package container | |||||
|
||||||
import ( | ||||||
"context" | ||||||
"encoding/json" | ||||||
"fmt" | ||||||
"os" | ||||||
"os/signal" | ||||||
"sort" | ||||||
"strings" | ||||||
"syscall" | ||||||
|
||||||
containerd "github.com/containerd/containerd/v2/client" | ||||||
|
@@ -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 { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
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], | ||||||
|
@@ -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, | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||
} | ||||||
logViewer, err := logging.InitContainerLogViewer(l, logViewOpts, stopChannel, options.GOptions.Experimental) | ||||||
if err != nil { | ||||||
|
@@ -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 | ||||||
} |
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 | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 { | ||
|
@@ -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 := " " | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
There was a problem hiding this comment.
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:
Which is more compact / readable.