add log testing.

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
Bo-Yi Wu 2016-04-05 14:50:08 +08:00
parent 2946dd95dc
commit 449ce80649
3 changed files with 105 additions and 15 deletions

View File

@ -49,7 +49,13 @@ func main() {
gopush.PushConf.Core.Port = *port gopush.PushConf.Core.Port = *port
} }
gopush.InitLog()
if err = gopush.InitLog(); err != nil {
log.Println(err)
return
}
if err = gopush.CheckPushConf(); err != nil { if err = gopush.CheckPushConf(); err != nil {
log.Println(err) log.Println(err)

View File

@ -2,11 +2,11 @@ package gopush
import ( import (
"github.com/Sirupsen/logrus" "github.com/Sirupsen/logrus"
"log"
"os" "os"
"errors"
) )
func InitLog() { func InitLog() error {
var err error var err error
@ -25,22 +25,23 @@ func InitLog() {
} }
// set logger // set logger
err = SetLogLevel(LogAccess, PushConf.Log.AccessLevel) if err := SetLogLevel(LogAccess, PushConf.Log.AccessLevel); err != nil {
if err != nil { return errors.New("Set access log level error: "+ err.Error())
log.Fatal(err)
} }
err = SetLogLevel(LogError, PushConf.Log.ErrorLevel)
if err != nil { if err := SetLogLevel(LogError, PushConf.Log.ErrorLevel); err != nil {
log.Fatal(err) return errors.New("Set error log level error: "+ err.Error())
} }
err = SetLogOut(LogAccess, PushConf.Log.AccessLog)
if err != nil { if err = SetLogOut(LogAccess, PushConf.Log.AccessLog); err != nil {
log.Fatal(err) return errors.New("Set access log path error: "+ err.Error())
} }
err = SetLogOut(LogError, PushConf.Log.ErrorLog)
if err != nil { if err = SetLogOut(LogError, PushConf.Log.ErrorLog); err != nil {
log.Fatal(err) return errors.New("Set error log path error: "+ err.Error())
} }
return nil
} }
func SetLogOut(log *logrus.Logger, outString string) error { func SetLogOut(log *logrus.Logger, outString string) error {
@ -51,19 +52,25 @@ func SetLogOut(log *logrus.Logger, outString string) error {
log.Out = os.Stderr log.Out = os.Stderr
default: default:
f, err := os.OpenFile(outString, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644) f, err := os.OpenFile(outString, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644)
if err != nil { if err != nil {
return err return err
} }
log.Out = f log.Out = f
} }
return nil return nil
} }
func SetLogLevel(log *logrus.Logger, levelString string) error { func SetLogLevel(log *logrus.Logger, levelString string) error {
level, err := logrus.ParseLevel(levelString) level, err := logrus.ParseLevel(levelString)
if err != nil { if err != nil {
return err return err
} }
log.Level = level log.Level = level
return nil return nil
} }

77
gorush/log_test.go Normal file
View File

@ -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())
}