logging: Allow SetLogger to be called multiple times

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 #519.

--
[1] - https://github.com/kata-containers/runtime/pull/468

Signed-off-by: James O. D. Hunt <james.o.hunt@intel.com>
This commit is contained in:
James O. D. Hunt 2018-07-26 14:47:42 +01:00
parent 029e7ca680
commit 58448bbcb8
3 changed files with 13 additions and 10 deletions

View File

@ -19,11 +19,12 @@ func init() {
runtime.LockOSThread() runtime.LockOSThread()
} }
var virtLog *logrus.Entry var virtLog = logrus.WithField("source", "virtcontainers")
// SetLogger sets the logger for virtcontainers package. // SetLogger sets the logger for virtcontainers package.
func SetLogger(logger *logrus.Entry) { func SetLogger(logger *logrus.Entry) {
virtLog = logger.WithField("source", "virtcontainers") fields := virtLog.Data
virtLog = logger.WithFields(fields)
deviceApi.SetLogger(virtLog) deviceApi.SetLogger(virtLog)
} }

View File

@ -12,16 +12,17 @@ import (
"github.com/kata-containers/runtime/virtcontainers/device/config" "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. // SetLogger sets the logger for device api package.
func SetLogger(logger *logrus.Entry) { func SetLogger(logger *logrus.Entry) {
devLogger = logger fields := devLogger.Data
devLogger = logger.WithFields(fields)
} }
// DeviceLogger returns logger for device management // DeviceLogger returns logger for device management
func DeviceLogger() *logrus.Entry { func DeviceLogger() *logrus.Entry {
return devLogger.WithField("subsystem", "device") return devLogger
} }
// DeviceReceiver is an interface used for accepting devices // DeviceReceiver is an interface used for accepting devices

View File

@ -128,14 +128,15 @@ func (config *RuntimeConfig) AddKernelParam(p vc.Param) error {
return config.HypervisorConfig.AddKernelParam(p) 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. // SetLogger sets the logger for oci package.
func SetLogger(logger *logrus.Entry) { func SetLogger(logger *logrus.Entry) {
ociLog = logger.WithFields(logrus.Fields{ fields := ociLog.Data
"source": "virtcontainers", ociLog = logger.WithFields(fields)
"subsystem": "oci",
})
} }
func cmdEnvs(spec CompatOCISpec, envs []vc.EnvVar) []vc.EnvVar { func cmdEnvs(spec CompatOCISpec, envs []vc.EnvVar) []vc.EnvVar {