Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
Bo-Yi Wu 2016-04-05 13:15:47 +08:00
parent 5055ca693d
commit 2946dd95dc
6 changed files with 97 additions and 1 deletions

1
.gitignore vendored
View File

@ -29,3 +29,4 @@ config.yaml
bin/* bin/*
.DS_Store .DS_Store
coverage.out coverage.out
logs/*

View File

@ -19,3 +19,9 @@ ios:
pem_cert_path: "cert.pem" pem_cert_path: "cert.pem"
pem_key_path: "key.pem" pem_key_path: "key.pem"
production: false production: false
log:
access_log: "stdout"
access_level: "debug"
error_log: "stderr"
error_level: "error"

View File

@ -49,8 +49,11 @@ func main() {
gopush.PushConf.Core.Port = *port gopush.PushConf.Core.Port = *port
} }
gopush.InitLog()
if err = gopush.CheckPushConf(); err != nil { if err = gopush.CheckPushConf(); err != nil {
log.Printf("Check Conf Error: '%v'", err) log.Println(err)
gopush.LogError.Fatal(err)
return return
} }

View File

@ -11,6 +11,7 @@ type ConfYaml struct {
Api SectionApi `yaml:"api"` Api SectionApi `yaml:"api"`
Android SectionAndroid `yaml:"android"` Android SectionAndroid `yaml:"android"`
Ios SectionIos `yaml:"ios"` Ios SectionIos `yaml:"ios"`
Log SectionLog `yaml:"log"`
} }
type SectionCore struct { type SectionCore struct {
@ -39,6 +40,13 @@ type SectionIos struct {
Production bool `yaml:"production"` 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 { func BuildDefaultPushConf() ConfYaml {
var conf ConfYaml var conf ConfYaml
@ -64,6 +72,12 @@ func BuildDefaultPushConf() ConfYaml {
conf.Ios.PemKeyPath = "key.pem" conf.Ios.PemKeyPath = "key.pem"
conf.Ios.Production = false conf.Ios.Production = false
// log
conf.Log.AccessLog = "stdout"
conf.Log.AccessLevel = "debug"
conf.Log.ErrorLog = "stderr"
conf.Log.ErrorLevel = "error"
return conf return conf
} }

View File

@ -2,6 +2,7 @@ package gopush
import ( import (
"crypto/tls" "crypto/tls"
"github.com/Sirupsen/logrus"
apns "github.com/sideshow/apns2" apns "github.com/sideshow/apns2"
) )
@ -9,4 +10,6 @@ var (
PushConf ConfYaml PushConf ConfYaml
CertificatePemIos tls.Certificate CertificatePemIos tls.Certificate
ApnsClient *apns.Client ApnsClient *apns.Client
LogAccess *logrus.Logger
LogError *logrus.Logger
) )

69
gorush/log.go Normal file
View File

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