Skip to content

Commit

Permalink
api: Change logger functions to accept a log entry
Browse files Browse the repository at this point in the history
Rather than accepting a `logrus.FieldLogger` interface type, change all
the `SetLogger()` functions to accept a `logrus.Entry`.

Signed-off-by: James O. D. Hunt <[email protected]>
  • Loading branch information
jodh-intel committed Jul 30, 2018
1 parent dfb758a commit 029e7ca
Show file tree
Hide file tree
Showing 9 changed files with 16 additions and 15 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.FieldLogger(logrus.New())
var virtLog *logrus.Entry

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

deviceApi.SetLogger(virtLog)
}

Expand Down
4 changes: 2 additions & 2 deletions virtcontainers/device/api/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import (
"github.com/kata-containers/runtime/virtcontainers/device/config"
)

var devLogger = logrus.FieldLogger(logrus.New())
var devLogger *logrus.Entry

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

Expand Down
6 changes: 3 additions & 3 deletions virtcontainers/hack/virtc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
vc "github.com/kata-containers/runtime/virtcontainers"
)

var virtcLog = logrus.New()
var virtcLog *logrus.Entry

var listFormat = "%s\t%s\t%s\t%s\n"
var statusFormat = "%s\t%s\n"
Expand Down Expand Up @@ -915,14 +915,14 @@ func main() {
if err != nil {
return err
}
virtcLog.Out = f
virtcLog.Logger.Out = f
}

switch context.GlobalString("log-format") {
case "text":
// retain logrus's default.
case "json":
virtcLog.Formatter = new(logrus.JSONFormatter)
virtcLog.Logger.Formatter = new(logrus.JSONFormatter)
default:
return fmt.Errorf("unknown log-format %q", context.GlobalString("log-format"))
}
Expand Down
2 changes: 1 addition & 1 deletion virtcontainers/implementation.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type VCImpl struct {
}

// SetLogger implements the VC function of the same name.
func (impl *VCImpl) SetLogger(logger logrus.FieldLogger) {
func (impl *VCImpl) SetLogger(logger *logrus.Entry) {
SetLogger(logger)
}

Expand Down
2 changes: 1 addition & 1 deletion virtcontainers/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (

// VC is the Virtcontainers interface
type VC interface {
SetLogger(logger logrus.FieldLogger)
SetLogger(logger *logrus.Entry)
SetFactory(Factory)

CreateSandbox(sandboxConfig SandboxConfig) (VCSandbox, error)
Expand Down
2 changes: 1 addition & 1 deletion virtcontainers/pkg/oci/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func (config *RuntimeConfig) AddKernelParam(p vc.Param) error {
var ociLog = logrus.FieldLogger(logrus.New())

// SetLogger sets the logger for oci package.
func SetLogger(logger logrus.FieldLogger) {
func SetLogger(logger *logrus.Entry) {
ociLog = logger.WithFields(logrus.Fields{
"source": "virtcontainers",
"subsystem": "oci",
Expand Down
2 changes: 1 addition & 1 deletion virtcontainers/pkg/vcmock/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
const mockErrorPrefix = "vcmock forced failure"

// SetLogger implements the VC function of the same name.
func (m *VCMock) SetLogger(logger logrus.FieldLogger) {
func (m *VCMock) SetLogger(logger *logrus.Entry) {
if m.SetLoggerFunc != nil {
m.SetLoggerFunc(logger)
}
Expand Down
2 changes: 1 addition & 1 deletion virtcontainers/pkg/vcmock/mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func TestVCMockSetLogger(t *testing.T) {
m := &VCMock{}
assert.Nil(m.SetLoggerFunc)

logger := logrus.New()
logger := logrus.NewEntry(logrus.New())

assert.Equal(loggerTriggered, 0)
m.SetLogger(logger)
Expand Down
6 changes: 3 additions & 3 deletions virtcontainers/virtcontainers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ func TestMain(m *testing.M) {

flag.Parse()

logger := logrus.New()
logger.Level = logrus.ErrorLevel
logger := logrus.NewEntry(logrus.New())
logger.Logger.Level = logrus.ErrorLevel
for _, arg := range flag.Args() {
if arg == "debug-logs" {
logger.Level = logrus.DebugLevel
logger.Logger.Level = logrus.DebugLevel
}
}
SetLogger(logger)
Expand Down

0 comments on commit 029e7ca

Please sign in to comment.