Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sure file logging works #3601

Merged
merged 1 commit into from
Jan 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions go/lib/log/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ go_test(
embed = [":go_default_library"],
deps = [
"//go/lib/log/mock_log:go_default_library",
"//go/lib/xtest:go_default_library",
"@com_github_golang_mock//gomock:go_default_library",
"@com_github_inconshreveable_log15//:go_default_library",
"@com_github_smartystreets_goconvey//convey:go_default_library",
"@com_github_stretchr_testify//assert:go_default_library",
],
)
6 changes: 6 additions & 0 deletions go/lib/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ var (
func SetupLogFile(name string, logDir string, logLevel string, logSize int, logAge int,
logBackups int, logFlush int, compress bool) error {

if err := os.MkdirAll(logDir, os.ModePerm); err != nil {
return common.NewBasicError("Unable create log directory:", err)
}

logLvl, err := log15.LvlFromString(changeTraceToDebug(logLevel))
if err != nil {
return common.NewBasicError("Unable to parse log.level flag:", err)
Expand Down Expand Up @@ -141,6 +145,7 @@ func setHandlers() {
log15.Root().SetHandler(handler)
}

// LogPanicAndExit catches panics and logs them.
func LogPanicAndExit() {
if msg := recover(); msg != nil {
log15.Crit("Panic", "msg", msg, "stack", string(debug.Stack()))
Expand All @@ -150,6 +155,7 @@ func LogPanicAndExit() {
}
}

// Flush writes the logs to the underlying buffer.
func Flush() {
if logBuf != nil {
logBuf.Flush()
Expand Down
94 changes: 62 additions & 32 deletions go/lib/log/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,52 +15,82 @@
package log_test

import (
"path/filepath"
"testing"

"github.com/golang/mock/gomock"
"github.com/inconshreveable/log15"
. "github.com/smartystreets/goconvey/convey"
"github.com/stretchr/testify/assert"

"github.com/scionproto/scion/go/lib/log"
"github.com/scionproto/scion/go/lib/log/mock_log"
"github.com/scionproto/scion/go/lib/xtest"
)

func TestSetupLogConsole(t *testing.T) {
tmpDir, cleanF := xtest.MustTempDir("", "test-folder")
defer cleanF()

tests := map[string]struct {
dir string
assertErr assert.ErrorAssertionFunc
}{
"cannot create, errors": {
dir: "/sys/aka/doesnt/exist",
assertErr: assert.Error,
},

"can create, nil": {
dir: filepath.Join(tmpDir, "new"),
assertErr: assert.NoError,
},
}

for td, tc := range tests {
t.Run(td, func(t *testing.T) {
err := log.SetupLogFile("test", tc.dir, "debug", 0, 0, 0, 0, false)
tc.assertErr(t, err)
})
}
}

func TestTraceFilterHandler(t *testing.T) {
Convey("Given a base handler...", t, func() {
t.Log("Given a base handler...")

t.Run("by default...", func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mockHandler := mock_log.NewMockHandler(ctrl)
logger := log.Root()
Convey("by default...", func() {
logger.SetHandler(mockHandler)
var msgSeenByMockHandler string
mockHandler.EXPECT().Log(gomock.Any()).Do(func(record *log15.Record) {
msgSeenByMockHandler = record.Msg
})
Convey("debug messages are printed", func() {
logger.Debug("foo")
So(msgSeenByMockHandler, ShouldEqual, "foo")
})
Convey("trace messages are printed", func() {
logger.Trace("foo")
So(msgSeenByMockHandler, ShouldEqual, log.TraceMsgPrefix+"foo")
})
})
Convey("if wrapped by a trace filter handler...", func() {
handler := log.FilterTraceHandler(mockHandler)
logger.SetHandler(handler)
Convey("debug messages are printed", func() {
var msgSeenByMockHandler string
mockHandler.EXPECT().Log(gomock.Any()).Do(func(record *log15.Record) {
msgSeenByMockHandler = record.Msg
})
logger.Debug("foo")
So(msgSeenByMockHandler, ShouldEqual, "foo")
})
Convey("trace messages are not printed", func() {
logger.Trace("foo")
})
})
logger.SetHandler(mockHandler)
var msgSeenByMockHandler string
mockHandler.EXPECT().Log(gomock.Any()).Do(func(record *log15.Record) {
msgSeenByMockHandler = record.Msg
}).AnyTimes()
t.Log("debug messages are printed")
logger.Debug("foo")
assert.Equal(t, msgSeenByMockHandler, "foo")
t.Log("trace messages are printed")
logger.Trace("foo")
assert.Equal(t, msgSeenByMockHandler, log.TraceMsgPrefix+"foo")
})

t.Run("if wrapped by a trace filter handler...", func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mockHandler := mock_log.NewMockHandler(ctrl)
logger := log.Root()
handler := log.FilterTraceHandler(mockHandler)
logger.SetHandler(handler)
t.Log("debug messages are printed")
var msgSeenByMockHandler string
mockHandler.EXPECT().Log(gomock.Any()).Do(func(record *log15.Record) {
msgSeenByMockHandler = record.Msg
}).AnyTimes()
logger.Debug("foo")
assert.Equal(t, msgSeenByMockHandler, "foo")
t.Log("trace messages are not printed")
logger.Trace("foo")
})

}