Merge pull request #28 from appleboy/log

Fix #25 support log.
This commit is contained in:
Bo-Yi Wu 2016-04-05 15:22:21 +08:00
commit 0964b40f17
7 changed files with 185 additions and 1 deletions

1
.gitignore vendored
View File

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

View File

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

View File

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

View File

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

View File

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

76
gorush/log.go Normal file
View File

@ -0,0 +1,76 @@
package gopush
import (
"errors"
"github.com/Sirupsen/logrus"
"os"
)
func InitLog() error {
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
if err := SetLogLevel(LogAccess, PushConf.Log.AccessLevel); err != nil {
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())
}
if err = SetLogOut(LogAccess, PushConf.Log.AccessLog); err != nil {
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 nil
}
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
}

77
gorush/log_test.go Normal file
View File

@ -0,0 +1,77 @@
package gopush
import (
"github.com/Sirupsen/logrus"
"github.com/stretchr/testify/assert"
"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())
}