From 449ce806493b32b3363235aa1ade9d5400485faf Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Tue, 5 Apr 2016 14:50:08 +0800 Subject: [PATCH] add log testing. Signed-off-by: Bo-Yi Wu --- gorush.go | 8 ++++- gorush/log.go | 35 ++++++++++++--------- gorush/log_test.go | 77 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 105 insertions(+), 15 deletions(-) create mode 100644 gorush/log_test.go diff --git a/gorush.go b/gorush.go index 6768469..d604088 100644 --- a/gorush.go +++ b/gorush.go @@ -49,7 +49,13 @@ func main() { gopush.PushConf.Core.Port = *port } - gopush.InitLog() + + + if err = gopush.InitLog(); err != nil { + log.Println(err) + + return + } if err = gopush.CheckPushConf(); err != nil { log.Println(err) diff --git a/gorush/log.go b/gorush/log.go index efa1343..fc26755 100644 --- a/gorush/log.go +++ b/gorush/log.go @@ -2,11 +2,11 @@ package gopush import ( "github.com/Sirupsen/logrus" - "log" "os" + "errors" ) -func InitLog() { +func InitLog() error { var err error @@ -25,22 +25,23 @@ func InitLog() { } // set logger - err = SetLogLevel(LogAccess, PushConf.Log.AccessLevel) - if err != nil { - log.Fatal(err) + if err := SetLogLevel(LogAccess, PushConf.Log.AccessLevel); err != nil { + return errors.New("Set access log level error: "+ err.Error()) } - err = SetLogLevel(LogError, PushConf.Log.ErrorLevel) - if err != nil { - log.Fatal(err) + + if err := SetLogLevel(LogError, PushConf.Log.ErrorLevel); err != nil { + return errors.New("Set error log level error: "+ err.Error()) } - err = SetLogOut(LogAccess, PushConf.Log.AccessLog) - if err != nil { - log.Fatal(err) + + if err = SetLogOut(LogAccess, PushConf.Log.AccessLog); err != nil { + return errors.New("Set access log path error: "+ err.Error()) } - err = SetLogOut(LogError, PushConf.Log.ErrorLog) - if err != nil { - log.Fatal(err) + + if err = SetLogOut(LogError, PushConf.Log.ErrorLog); err != nil { + return errors.New("Set error log path error: "+ err.Error()) } + + return nil } func SetLogOut(log *logrus.Logger, outString string) error { @@ -51,19 +52,25 @@ func SetLogOut(log *logrus.Logger, outString string) error { log.Out = os.Stderr default: f, err := os.OpenFile(outString, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644) + if err != nil { return err } + log.Out = f } + return nil } func SetLogLevel(log *logrus.Logger, levelString string) error { level, err := logrus.ParseLevel(levelString) + if err != nil { return err } + log.Level = level + return nil } diff --git a/gorush/log_test.go b/gorush/log_test.go new file mode 100644 index 0000000..806ad76 --- /dev/null +++ b/gorush/log_test.go @@ -0,0 +1,77 @@ +package gopush + +import ( + "github.com/stretchr/testify/assert" + "github.com/Sirupsen/logrus" + "testing" +) + +func TestSetLogLevel(t *testing.T) { + log := logrus.New() + + err := SetLogLevel(log, "debug") + assert.Nil(t, err) + + err = SetLogLevel(log, "invalid") + assert.Equal(t, "not a valid logrus Level: \"invalid\"", err.Error()) +} + +func TestSetLogOut(t *testing.T) { + log := logrus.New() + + err := SetLogOut(log, "stdout") + assert.Nil(t, err) + + err = SetLogOut(log, "stderr") + assert.Nil(t, err) + + err = SetLogOut(log, "access.log") + assert.Nil(t, err) + + // missing create logs folder. + err = SetLogOut(log, "logs/access.log") + assert.NotNil(t, err) +} + +func TestInitDefaultLog(t *testing.T) { + PushConf = BuildDefaultPushConf() + + // no errors on default config + assert.Nil(t, InitLog()) + + PushConf.Log.AccessLevel = "invalid" + + assert.NotNil(t, InitLog()) +} + +func TestAccessLevel(t *testing.T) { + PushConf = BuildDefaultPushConf() + + PushConf.Log.AccessLevel = "invalid" + + assert.NotNil(t, InitLog()) +} + +func TestErrorLevel(t *testing.T) { + PushConf = BuildDefaultPushConf() + + PushConf.Log.ErrorLevel = "invalid" + + assert.NotNil(t, InitLog()) +} + +func TestAccessLogPath(t *testing.T) { + PushConf = BuildDefaultPushConf() + + PushConf.Log.AccessLog = "logs/access.log" + + assert.NotNil(t, InitLog()) +} + +func TestErrorLogPath(t *testing.T) { + PushConf = BuildDefaultPushConf() + + PushConf.Log.ErrorLog = "logs/error.log" + + assert.NotNil(t, InitLog()) +}