From 2946dd95dca3b12062fd0c90dd9b21099b6780ad Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Tue, 5 Apr 2016 13:15:47 +0800 Subject: [PATCH 1/3] add log. Signed-off-by: Bo-Yi Wu --- .gitignore | 1 + config/config.yaml | 6 ++++ gorush.go | 5 +++- gorush/config.go | 14 ++++++++++ gorush/global.go | 3 ++ gorush/log.go | 69 ++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 gorush/log.go diff --git a/.gitignore b/.gitignore index 611c651..891024d 100644 --- a/.gitignore +++ b/.gitignore @@ -29,3 +29,4 @@ config.yaml bin/* .DS_Store coverage.out +logs/* diff --git a/config/config.yaml b/config/config.yaml index 8426ebd..b89d37f 100644 --- a/config/config.yaml +++ b/config/config.yaml @@ -19,3 +19,9 @@ ios: pem_cert_path: "cert.pem" pem_key_path: "key.pem" production: false + +log: + access_log: "stdout" + access_level: "debug" + error_log: "stderr" + error_level: "error" diff --git a/gorush.go b/gorush.go index 299ad33..6768469 100644 --- a/gorush.go +++ b/gorush.go @@ -49,8 +49,11 @@ func main() { gopush.PushConf.Core.Port = *port } + gopush.InitLog() + if err = gopush.CheckPushConf(); err != nil { - log.Printf("Check Conf Error: '%v'", err) + log.Println(err) + gopush.LogError.Fatal(err) return } diff --git a/gorush/config.go b/gorush/config.go index 26d0a45..851f9d0 100644 --- a/gorush/config.go +++ b/gorush/config.go @@ -11,6 +11,7 @@ type ConfYaml struct { Api SectionApi `yaml:"api"` Android SectionAndroid `yaml:"android"` Ios SectionIos `yaml:"ios"` + Log SectionLog `yaml:"log"` } type SectionCore struct { @@ -39,6 +40,13 @@ type SectionIos struct { Production bool `yaml:"production"` } +type SectionLog struct { + AccessLog string `yaml:"access_log"` + AccessLevel string `yaml:"access_level"` + ErrorLog string `yaml:"error_log"` + ErrorLevel string `yaml:"error_level"` +} + func BuildDefaultPushConf() ConfYaml { var conf ConfYaml @@ -64,6 +72,12 @@ func BuildDefaultPushConf() ConfYaml { conf.Ios.PemKeyPath = "key.pem" conf.Ios.Production = false + // log + conf.Log.AccessLog = "stdout" + conf.Log.AccessLevel = "debug" + conf.Log.ErrorLog = "stderr" + conf.Log.ErrorLevel = "error" + return conf } diff --git a/gorush/global.go b/gorush/global.go index 35974a6..3bd9e40 100644 --- a/gorush/global.go +++ b/gorush/global.go @@ -2,6 +2,7 @@ package gopush import ( "crypto/tls" + "github.com/Sirupsen/logrus" apns "github.com/sideshow/apns2" ) @@ -9,4 +10,6 @@ var ( PushConf ConfYaml CertificatePemIos tls.Certificate ApnsClient *apns.Client + LogAccess *logrus.Logger + LogError *logrus.Logger ) diff --git a/gorush/log.go b/gorush/log.go new file mode 100644 index 0000000..efa1343 --- /dev/null +++ b/gorush/log.go @@ -0,0 +1,69 @@ +package gopush + +import ( + "github.com/Sirupsen/logrus" + "log" + "os" +) + +func InitLog() { + + var err error + + // init logger + LogAccess = logrus.New() + LogError = logrus.New() + + LogAccess.Formatter = &logrus.TextFormatter{ + ForceColors: true, + FullTimestamp: true, + } + + LogError.Formatter = &logrus.TextFormatter{ + ForceColors: true, + FullTimestamp: true, + } + + // set logger + err = SetLogLevel(LogAccess, PushConf.Log.AccessLevel) + if err != nil { + log.Fatal(err) + } + err = SetLogLevel(LogError, PushConf.Log.ErrorLevel) + if err != nil { + log.Fatal(err) + } + err = SetLogOut(LogAccess, PushConf.Log.AccessLog) + if err != nil { + log.Fatal(err) + } + err = SetLogOut(LogError, PushConf.Log.ErrorLog) + if err != nil { + log.Fatal(err) + } +} + +func SetLogOut(log *logrus.Logger, outString string) error { + switch outString { + case "stdout": + log.Out = os.Stdout + case "stderr": + 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 +} From 449ce806493b32b3363235aa1ade9d5400485faf Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Tue, 5 Apr 2016 14:50:08 +0800 Subject: [PATCH 2/3] 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()) +} From 1a6e697416e05bd60c6105258313b71d17ca98cc Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Tue, 5 Apr 2016 15:04:50 +0800 Subject: [PATCH 3/3] [ci skip] fix format. Signed-off-by: Bo-Yi Wu --- gorush.go | 2 -- gorush/log.go | 10 +++++----- gorush/log_test.go | 2 +- 3 files changed, 6 insertions(+), 8 deletions(-) diff --git a/gorush.go b/gorush.go index d604088..5eb77c9 100644 --- a/gorush.go +++ b/gorush.go @@ -49,8 +49,6 @@ func main() { gopush.PushConf.Core.Port = *port } - - if err = gopush.InitLog(); err != nil { log.Println(err) diff --git a/gorush/log.go b/gorush/log.go index fc26755..41157fd 100644 --- a/gorush/log.go +++ b/gorush/log.go @@ -1,9 +1,9 @@ package gopush import ( + "errors" "github.com/Sirupsen/logrus" "os" - "errors" ) func InitLog() error { @@ -26,19 +26,19 @@ func InitLog() error { // set logger if err := SetLogLevel(LogAccess, PushConf.Log.AccessLevel); err != nil { - return errors.New("Set access log level error: "+ err.Error()) + return errors.New("Set access log level error: " + err.Error()) } if err := SetLogLevel(LogError, PushConf.Log.ErrorLevel); err != nil { - return errors.New("Set error log level error: "+ err.Error()) + return errors.New("Set error log level error: " + err.Error()) } if err = SetLogOut(LogAccess, PushConf.Log.AccessLog); err != nil { - return errors.New("Set access log path error: "+ err.Error()) + return errors.New("Set access log path error: " + err.Error()) } if err = SetLogOut(LogError, PushConf.Log.ErrorLog); err != nil { - return errors.New("Set error log path error: "+ err.Error()) + return errors.New("Set error log path error: " + err.Error()) } return nil diff --git a/gorush/log_test.go b/gorush/log_test.go index 806ad76..42ecb76 100644 --- a/gorush/log_test.go +++ b/gorush/log_test.go @@ -1,8 +1,8 @@ package gopush import ( - "github.com/stretchr/testify/assert" "github.com/Sirupsen/logrus" + "github.com/stretchr/testify/assert" "testing" )