Merge pull request #521 from bergwolf/log

factory: add SetLogger API
This commit is contained in:
Peng Tao 2018-07-27 15:52:24 +08:00 committed by GitHub
commit cfbc974fec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 36 additions and 0 deletions

View File

@ -17,6 +17,7 @@ import (
"syscall"
vc "github.com/kata-containers/runtime/virtcontainers"
vf "github.com/kata-containers/runtime/virtcontainers/factory"
"github.com/kata-containers/runtime/virtcontainers/pkg/oci"
specs "github.com/opencontainers/runtime-spec/specs-go"
"github.com/sirupsen/logrus"
@ -146,6 +147,7 @@ func init() {
kataLog = logrus.WithFields(logrus.Fields{
"name": name,
"source": "runtime",
"arch": arch,
"pid": os.Getpid(),
})
@ -194,6 +196,9 @@ func setExternalLoggers(logger *logrus.Entry) {
// Set virtcontainers logger.
vci.SetLogger(logger)
// Set vm factory logger.
vf.SetLogger(logger)
// Set the OCI package logger.
oci.SetLogger(logger)
}

View File

@ -67,6 +67,15 @@ func NewFactory(config Config, fetchOnly bool) (vc.Factory, error) {
return &factory{b}, nil
}
// SetLogger sets the logger for the factory.
func SetLogger(logger logrus.FieldLogger) {
fields := logrus.Fields{
"source": "virtcontainers",
}
factoryLogger = logger.WithFields(fields)
}
func (f *factory) log() *logrus.Entry {
return factoryLogger.WithField("subsystem", "factory")
}

View File

@ -9,6 +9,7 @@ import (
"io/ioutil"
"testing"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
vc "github.com/kata-containers/runtime/virtcontainers"
@ -66,6 +67,27 @@ func TestNewFactory(t *testing.T) {
assert.Error(err)
}
func TestFactorySetLogger(t *testing.T) {
assert := assert.New(t)
testLog := logrus.WithFields(logrus.Fields{"testfield": "foobar"})
testLog.Level = logrus.DebugLevel
SetLogger(testLog)
var config Config
config.VMConfig.HypervisorConfig = vc.HypervisorConfig{
KernelPath: "foo",
ImagePath: "bar",
}
vf, err := NewFactory(config, false)
assert.Nil(err)
f, ok := vf.(*factory)
assert.True(ok)
assert.Equal(f.log().Logger.Level, testLog.Logger.Level)
}
func TestVMConfigValid(t *testing.T) {
assert := assert.New(t)