Skip to content

Commit

Permalink
Merge pull request kata-containers#521 from bergwolf/log
Browse files Browse the repository at this point in the history
factory: add SetLogger API
  • Loading branch information
bergwolf authored Jul 27, 2018
2 parents 2c3215c + 9a497fe commit cfbc974
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
5 changes: 5 additions & 0 deletions cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -146,6 +147,7 @@ func init() {
kataLog = logrus.WithFields(logrus.Fields{
"name": name,
"source": "runtime",
"arch": arch,
"pid": os.Getpid(),
})

Expand Down Expand Up @@ -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)
}
Expand Down
9 changes: 9 additions & 0 deletions virtcontainers/factory/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
22 changes: 22 additions & 0 deletions virtcontainers/factory/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"io/ioutil"
"testing"

"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"

vc "github.com/kata-containers/runtime/virtcontainers"
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit cfbc974

Please sign in to comment.