Skip to content

Commit

Permalink
logging: Allow SetLogger to be called multiple times
Browse files Browse the repository at this point in the history
Now that the `SetLogger()` functions accept a `logrus.Entry`, they can
access the fields that have already been set for the logger and
re-apply them if `SetLogger()` is called multiple times.

This fixes a bug whereby the logger functions -- which are necessarily
called multiple times [1] -- previously ended up applying any new fields
the specified logger contained, but erroneously removing any additional
fields added since `SetLogger()` was last called.

Partially fixes kata-containers#519.

--
[1] - kata-containers#468

Signed-off-by: James O. D. Hunt <[email protected]>
  • Loading branch information
jodh-intel committed Jul 30, 2018
1 parent 029e7ca commit 58448bb
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 10 deletions.
5 changes: 3 additions & 2 deletions virtcontainers/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ func init() {
runtime.LockOSThread()
}

var virtLog *logrus.Entry
var virtLog = logrus.WithField("source", "virtcontainers")

// SetLogger sets the logger for virtcontainers package.
func SetLogger(logger *logrus.Entry) {
virtLog = logger.WithField("source", "virtcontainers")
fields := virtLog.Data
virtLog = logger.WithFields(fields)

deviceApi.SetLogger(virtLog)
}
Expand Down
7 changes: 4 additions & 3 deletions virtcontainers/device/api/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,17 @@ import (
"github.com/kata-containers/runtime/virtcontainers/device/config"
)

var devLogger *logrus.Entry
var devLogger = logrus.WithField("subsystem", "device")

// SetLogger sets the logger for device api package.
func SetLogger(logger *logrus.Entry) {
devLogger = logger
fields := devLogger.Data
devLogger = logger.WithFields(fields)
}

// DeviceLogger returns logger for device management
func DeviceLogger() *logrus.Entry {
return devLogger.WithField("subsystem", "device")
return devLogger
}

// DeviceReceiver is an interface used for accepting devices
Expand Down
11 changes: 6 additions & 5 deletions virtcontainers/pkg/oci/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,15 @@ func (config *RuntimeConfig) AddKernelParam(p vc.Param) error {
return config.HypervisorConfig.AddKernelParam(p)
}

var ociLog = logrus.FieldLogger(logrus.New())
var ociLog = logrus.WithFields(logrus.Fields{
"source": "virtcontainers",
"subsystem": "oci",
})

// SetLogger sets the logger for oci package.
func SetLogger(logger *logrus.Entry) {
ociLog = logger.WithFields(logrus.Fields{
"source": "virtcontainers",
"subsystem": "oci",
})
fields := ociLog.Data
ociLog = logger.WithFields(fields)
}

func cmdEnvs(spec CompatOCISpec, envs []vc.EnvVar) []vc.EnvVar {
Expand Down

0 comments on commit 58448bb

Please sign in to comment.