From 2946dd95dca3b12062fd0c90dd9b21099b6780ad Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Tue, 5 Apr 2016 13:15:47 +0800 Subject: [PATCH] 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 +}