From 79e8da0675bc0ab5ed19c862d998f5415dd93baa Mon Sep 17 00:00:00 2001 From: "James O. D. Hunt" Date: Wed, 4 Jul 2018 14:45:43 +0100 Subject: [PATCH 1/3] create: Remove redundant logging code Don't add the container ID as a log fields as it is already a field (added on #453). Signed-off-by: James O. D. Hunt --- cli/create.go | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/cli/create.go b/cli/create.go index 165bf6b69..e7b4fda14 100644 --- a/cli/create.go +++ b/cli/create.go @@ -16,7 +16,6 @@ import ( vc "github.com/kata-containers/runtime/virtcontainers" "github.com/kata-containers/runtime/virtcontainers/pkg/oci" - "github.com/sirupsen/logrus" "github.com/urfave/cli" ) @@ -283,11 +282,7 @@ func createContainer(ociSpec oci.CompatOCISpec, containerID, bundlePath, func createCgroupsFiles(containerID string, cgroupsDirPath string, cgroupsPathList []string, pid int) error { if len(cgroupsPathList) == 0 { - fields := logrus.Fields{ - "container": containerID, - "pid": pid, - } - kataLog.WithFields(fields).Info("Cgroups files not created because cgroupsPath was empty") + kataLog.WithField("pid", pid).Info("Cgroups files not created because cgroupsPath was empty") return nil } From 03d4d4937c92e85e7348ae87585a99f552e17d7d Mon Sep 17 00:00:00 2001 From: "James O. D. Hunt" Date: Wed, 4 Jul 2018 14:48:43 +0100 Subject: [PATCH 2/3] logging: Add function to handle external loggers Created a new `setExternalLogger()` which sets (or resets) the logger used by the external packages which allow a logger to be specified. Signed-off-by: James O. D. Hunt --- cli/main.go | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/cli/main.go b/cli/main.go index e5b7f61e2..99a4a890e 100644 --- a/cli/main.go +++ b/cli/main.go @@ -187,6 +187,16 @@ func setupSignalHandler() { }() } +// setExternalLoggers registers the specified logger with the external +// packages which accept a logger to handle their own logging. +func setExternalLoggers(logger *logrus.Entry) { + // Set virtcontainers logger. + vci.SetLogger(logger) + + // Set the OCI package logger. + oci.SetLogger(logger) +} + // beforeSubcommands is the function to perform preliminary checks // before command-line parsing occurs. func beforeSubcommands(context *cli.Context) error { @@ -225,11 +235,7 @@ func beforeSubcommands(context *cli.Context) error { return fmt.Errorf("unknown log-format %q", context.GlobalString("log-format")) } - // Set virtcontainers logger. - vci.SetLogger(kataLog) - - // Set the OCI package logger. - oci.SetLogger(kataLog) + setExternalLoggers(kataLog) ignoreLogging := false From 64e2ca35dde4f9f0e7a69b458350ee331823bac0 Mon Sep 17 00:00:00 2001 From: "James O. D. Hunt" Date: Wed, 4 Jul 2018 14:49:58 +0100 Subject: [PATCH 3/3] logging: Reset external loggers when cid+sid available Once `containerID` and `sandboxID` fields are available, re-register the logger with the external packages to ensure they too display these important fields. Fixes #467. Signed-off-by: James O. D. Hunt --- cli/create.go | 3 +++ cli/delete.go | 2 ++ cli/events.go | 2 ++ cli/exec.go | 2 ++ cli/kill.go | 2 ++ cli/pause.go | 2 ++ cli/ps.go | 2 ++ cli/start.go | 2 ++ cli/state.go | 2 ++ 9 files changed, 19 insertions(+) diff --git a/cli/create.go b/cli/create.go index e7b4fda14..858d4233f 100644 --- a/cli/create.go +++ b/cli/create.go @@ -89,6 +89,7 @@ func create(containerID, bundlePath, console, pidFilePath string, detach bool, var err error kataLog = kataLog.WithField("container", containerID) + setExternalLoggers(kataLog) // Checks the MUST and MUST NOT from OCI runtime specification if bundlePath, err = validCreateParams(containerID, bundlePath); err != nil { @@ -240,6 +241,7 @@ func createSandbox(ociSpec oci.CompatOCISpec, runtimeConfig oci.RuntimeConfig, } kataLog = kataLog.WithField("sandbox", sandbox.ID()) + setExternalLoggers(kataLog) containers := sandbox.GetAllContainers() if len(containers) != 1 { @@ -267,6 +269,7 @@ func createContainer(ociSpec oci.CompatOCISpec, containerID, bundlePath, } kataLog = kataLog.WithField("sandbox", sandboxID) + setExternalLoggers(kataLog) _, c, err := vci.CreateContainer(sandboxID, contConfig) if err != nil { diff --git a/cli/delete.go b/cli/delete.go index 0d83314e9..31ee87227 100644 --- a/cli/delete.go +++ b/cli/delete.go @@ -64,6 +64,8 @@ func delete(containerID string, force bool) error { "sandbox": sandboxID, }) + setExternalLoggers(kataLog) + containerID = status.ID containerType, err := oci.GetContainerType(status.Annotations) diff --git a/cli/events.go b/cli/events.go index 7da633cc9..67dfc56aa 100644 --- a/cli/events.go +++ b/cli/events.go @@ -156,6 +156,8 @@ information is displayed once every 5 seconds.`, "sandbox": sandboxID, }) + setExternalLoggers(kataLog) + if status.State.State == vc.StateStopped { return fmt.Errorf("container with id %s is not running", status.ID) } diff --git a/cli/exec.go b/cli/exec.go index 1e71c90d2..2d5d1f28a 100644 --- a/cli/exec.go +++ b/cli/exec.go @@ -194,6 +194,8 @@ func execute(context *cli.Context) error { "sandbox": sandboxID, }) + setExternalLoggers(kataLog) + // Retrieve OCI spec configuration. ociSpec, err := oci.GetOCIConfig(status) if err != nil { diff --git a/cli/kill.go b/cli/kill.go index 5d93df052..f66de9be7 100644 --- a/cli/kill.go +++ b/cli/kill.go @@ -104,6 +104,8 @@ func kill(containerID, signal string, all bool) error { "sandbox": sandboxID, }) + setExternalLoggers(kataLog) + signum, err := processSignal(signal) if err != nil { return err diff --git a/cli/pause.go b/cli/pause.go index 4b9a49087..2cec0beae 100644 --- a/cli/pause.go +++ b/cli/pause.go @@ -55,6 +55,8 @@ func toggleContainerPause(containerID string, pause bool) (err error) { "sandbox": sandboxID, }) + setExternalLoggers(kataLog) + if pause { err = vci.PauseContainer(sandboxID, containerID) } else { diff --git a/cli/ps.go b/cli/ps.go index 4dfab940f..2d0817270 100644 --- a/cli/ps.go +++ b/cli/ps.go @@ -61,6 +61,8 @@ func ps(containerID, format string, args []string) error { "sandbox": sandboxID, }) + setExternalLoggers(kataLog) + // container MUST be running if status.State.State != vc.StateRunning { return fmt.Errorf("Container %s is not running", containerID) diff --git a/cli/start.go b/cli/start.go index 7da8a5a28..24723209a 100644 --- a/cli/start.go +++ b/cli/start.go @@ -52,6 +52,8 @@ func start(containerID string) (vc.VCSandbox, error) { "sandbox": sandboxID, }) + setExternalLoggers(kataLog) + containerID = status.ID containerType, err := oci.GetContainerType(status.Annotations) diff --git a/cli/state.go b/cli/state.go index 57412e618..f10010584 100644 --- a/cli/state.go +++ b/cli/state.go @@ -36,6 +36,8 @@ instance of a container.`, func state(containerID string) error { kataLog = kataLog.WithField("container", containerID) + setExternalLoggers(kataLog) + // Checks the MUST and MUST NOT from OCI runtime specification status, _, err := getExistingContainerInfo(containerID) if err != nil {