Skip to content
This repository has been archived by the owner on Apr 11, 2024. It is now read-only.

use zap instead of logrus #44

Merged
merged 12 commits into from
Sep 28, 2016
44 changes: 19 additions & 25 deletions cmd/gaurun/gaurun.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ package main

import (
"flag"
"fmt"
"io/ioutil"
"log"

"github.com/Sirupsen/logrus"
"github.com/mercari/gaurun/gaurun"
)

Expand All @@ -25,17 +24,10 @@ func main() {
// set default parameters
gaurun.ConfGaurun = gaurun.BuildDefaultConf()

// init logger
gaurun.LogAccess = logrus.New()
gaurun.LogError = logrus.New()

gaurun.LogAccess.Formatter = new(gaurun.GaurunFormatter)
gaurun.LogError.Formatter = new(gaurun.GaurunFormatter)

// load configuration
conf, err := gaurun.LoadConf(gaurun.ConfGaurun, *confPath)
if err != nil {
gaurun.LogError.Fatal(err)
gaurun.LogSetupError(err)
}
gaurun.ConfGaurun = conf

Expand All @@ -55,48 +47,50 @@ func main() {
}

// set logger
err = gaurun.SetLogLevel(gaurun.LogAccess, "info")
accessLogger, err := gaurun.InitLog(gaurun.ConfGaurun.Log.AccessLog)
if err != nil {
log.Fatal(err)
gaurun.LogSetupError(err)
}
err = gaurun.SetLogLevel(gaurun.LogError, gaurun.ConfGaurun.Log.Level)
errorLogger, err := gaurun.InitLog(gaurun.ConfGaurun.Log.ErrorLog)
if err != nil {
log.Fatal(err)
gaurun.LogSetupError(err)
}
err = gaurun.SetLogOut(gaurun.LogAccess, gaurun.ConfGaurun.Log.AccessLog)
if err != nil {
log.Fatal(err)

if err := gaurun.SetLogLevel(accessLogger, "info"); err != nil {
gaurun.LogSetupError(err)
}
err = gaurun.SetLogOut(gaurun.LogError, gaurun.ConfGaurun.Log.ErrorLog)
if err != nil {
log.Fatal(err)
if err := gaurun.SetLogLevel(errorLogger, gaurun.ConfGaurun.Log.Level); err != nil {
gaurun.LogSetupError(err)
}

gaurun.LogAccess = accessLogger
gaurun.LogError = errorLogger

if !gaurun.ConfGaurun.Ios.Enabled && !gaurun.ConfGaurun.Android.Enabled {
gaurun.LogError.Fatal("What do you want to do?")
gaurun.LogSetupError(fmt.Errorf("What do you want to do?"))
}

if gaurun.ConfGaurun.Ios.Enabled {
gaurun.CertificatePemIos.Cert, err = ioutil.ReadFile(gaurun.ConfGaurun.Ios.PemCertPath)
if err != nil {
gaurun.LogError.Fatal("A certification file for iOS is not found.")
gaurun.LogSetupError(fmt.Errorf("A certification file for iOS is not found."))
}

gaurun.CertificatePemIos.Key, err = ioutil.ReadFile(gaurun.ConfGaurun.Ios.PemKeyPath)
if err != nil {
gaurun.LogError.Fatal("A key file for iOS is not found.")
gaurun.LogSetupError(fmt.Errorf("A key file for iOS is not found."))
}

}

if gaurun.ConfGaurun.Android.Enabled {
if gaurun.ConfGaurun.Android.ApiKey == "" {
gaurun.LogError.Fatal("APIKey for Android is empty.")
gaurun.LogSetupError(fmt.Errorf("APIKey for Android is empty."))
}
}

if err := gaurun.InitHttpClient(); err != nil {
gaurun.LogError.Fatal("failed to init http client")
gaurun.LogSetupError(fmt.Errorf("failed to init http client"))
}
gaurun.InitStat()
gaurun.StartPushWorkers(gaurun.ConfGaurun.Core.WorkerNum, gaurun.ConfGaurun.Core.QueueNum)
Expand Down
11 changes: 4 additions & 7 deletions cmd/gaurun_recover/gaurun_recover.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"net"
"net/http"
"os"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -95,7 +94,7 @@ func main() {
// load configuration
conf, err := gaurun.LoadConf(gaurun.ConfGaurun, *confPath)
if err != nil {
log.Fatal(err)
gaurun.LogSetupError(err)
}
gaurun.ConfGaurun = conf

Expand All @@ -112,11 +111,9 @@ func main() {
for scanner.Scan() {
var logPush gaurun.LogPushEntry
line := scanner.Text()
idx := strings.Index(line, " ")
JSONStr := line[idx+1:]
err := json.Unmarshal([]byte(JSONStr), &logPush)
err := json.Unmarshal([]byte(line), &logPush)
if err != nil {
log.Printf("JSON parse error(%s)", JSONStr)
log.Printf("JSON parse error(%s)", line)
continue
}
if logPush.Type == "accepted-request" {
Expand All @@ -143,7 +140,7 @@ func main() {
gaurun.ConfGaurun.Ios.PemKeyPath,
)
if err != nil {
log.Fatal(err)
gaurun.LogSetupError(err)
}
APNSClient.Timeout = time.Duration(gaurun.ConfGaurun.Ios.Timeout) * time.Second

Expand Down
4 changes: 2 additions & 2 deletions gaurun/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func ConfigPushersHandler(w http.ResponseWriter, r *http.Request) {

values, err := url.ParseQuery(r.URL.RawQuery)
if err != nil {
LogError.Error(err)
LogError.Error(err.Error())
sendResponse(w, "url parameters could not be parsed", http.StatusBadRequest)
return
}
Expand All @@ -143,7 +143,7 @@ func ConfigPushersHandler(w http.ResponseWriter, r *http.Request) {

newPusherMax, err := strconv.ParseInt(in, 0, 64)
if err != nil {
LogError.Error(err)
LogError.Error(err.Error())
sendResponse(w, "malformed value", http.StatusBadRequest)
return
}
Expand Down
6 changes: 3 additions & 3 deletions gaurun/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ import (
"net/http"
"sync"

"github.com/Sirupsen/logrus"
"github.com/mercari/gcm"
"github.com/uber-go/zap"
)

var (
ConfGaurun ConfToml
QueueNotification chan RequestGaurunNotification
CertificatePemIos CertificatePem
LogAccess *logrus.Logger
LogError *logrus.Logger
LogAccess zap.Logger
LogError zap.Logger
StatGaurun StatApp
// for numbering push
OnceNumbering sync.Once
Expand Down
152 changes: 80 additions & 72 deletions gaurun/log.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
package gaurun

import (
"bytes"
"encoding/json"
"fmt"
"log"
"math"
"os"
"strconv"
"sync/atomic"
"time"

"github.com/Sirupsen/logrus"
"github.com/uber-go/zap"
)

type LogReq struct {
Expand Down Expand Up @@ -41,61 +38,49 @@ type LogPushEntry struct {
Expiry int `json:"expiry,omitempty"`
}

type GaurunFormatter struct {
}

func (f *GaurunFormatter) Format(entry *logrus.Entry) ([]byte, error) {
b := &bytes.Buffer{}
fmt.Fprintf(b, "[%s] ", entry.Level.String())
fmt.Fprintf(b, "%s", entry.Message)
b.WriteByte('\n')
return b.Bytes(), nil
}

func InitLog() *logrus.Logger {
return logrus.New()
}

func SetLogOut(log *logrus.Logger, outString string) error {
func InitLog(outString string) (zap.Logger, error) {
var writer zap.WriteSyncer
switch outString {
case "stdout":
log.Out = os.Stdout
writer = os.Stdout
case "stderr":
log.Out = os.Stderr
writer = os.Stderr
default:
f, err := os.OpenFile(outString, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644)
if err != nil {
return err
return nil, err
}
log.Out = f
writer = f
}
return nil

encoder := zap.NewJSONEncoder(
zap.RFC3339Formatter("time"),
)
return zap.New(encoder, zap.Output(writer), zap.ErrorOutput(writer)), nil
}

func SetLogLevel(log *logrus.Logger, levelString string) error {
level, err := logrus.ParseLevel(levelString)
if err != nil {
func SetLogLevel(log zap.Logger, levelString string) error {
var level zap.Level
if err := level.UnmarshalText([]byte(levelString)); err != nil {
return err
}
log.Level = level
log.SetLevel(level)
return nil
}

// LogSetupError output error log with log package and exit immediately.
func LogSetupError(err error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LogSetupFatal() looks better to me than LogSetupError().

log.Fatal(err)
}

func LogAcceptedRequest(uri, method, proto string, length int64) {
log := &LogReq{
Type: "accepted-request",
Time: time.Now().Format("2006/01/02 15:04:05 MST"),
URI: uri,
Method: method,
Proto: proto,
ContentLength: length,
}
logJSON, err := json.Marshal(log)
if err != nil {
LogError.Error("Marshaling JSON error")
return
}
LogAccess.Info(string(logJSON))
LogAccess.Info("",
zap.String("type", "accepted-request"),
zap.String("uri", uri),
zap.String("method", method),
zap.String("proto", proto),
zap.Int64("content_length", length),
)
}

func LogPush(id uint64, status, token string, ptime float64, req RequestGaurunNotification, errPush error) {
Expand All @@ -107,45 +92,68 @@ func LogPush(id uint64, status, token string, ptime float64, req RequestGaurunNo
plat = "android"
}

ptime3 := fmt.Sprintf("%.3f", ptime)
ptime, _ = strconv.ParseFloat(ptime3, 64)
ptime = math.Floor(ptime*1000) / 1000 // %.3f conversion

errMsg := ""
if errPush != nil {
errMsg = errPush.Error()
}

log := &LogPushEntry{
Type: status,
Time: time.Now().Format("2006/01/02 15:04:05 MST"),
ID: id,
Platform: plat,
Token: token,
Message: req.Message,
Ptime: ptime,
Error: errMsg,
CollapseKey: req.CollapseKey,
DelayWhileIdle: req.DelayWhileIdle,
TimeToLive: req.TimeToLive,
Badge: req.Badge,
Sound: req.Sound,
ContentAvailable: req.ContentAvailable,
Expiry: req.Expiry,
}
logJSON, err := json.Marshal(log)
if err != nil {
LogError.Error("Marshaling JSON error")
return
}

var logger func(string, ...zap.Field)
switch status {
case StatusAcceptedPush:
fallthrough
case StatusSucceededPush:
LogAccess.Info(string(logJSON))
logger = LogAccess.Info
case StatusFailedPush:
LogError.Error(string(logJSON))
logger = LogError.Error
}

// omitempty handling for device dependent values
collapseKey := zap.Skip()
if req.CollapseKey != "" {
collapseKey = zap.String("collapse_key", req.CollapseKey)
}
delayWhileIdle := zap.Skip()
if req.DelayWhileIdle {
delayWhileIdle = zap.Bool("delay_while_idle", req.DelayWhileIdle)
}
timeToLive := zap.Skip()
if req.TimeToLive != 0 {
timeToLive = zap.Int("time_to_live", req.TimeToLive)
}
badge := zap.Skip()
if req.Badge != 0 {
badge = zap.Int("badge", req.Badge)
}
sound := zap.Skip()
if req.Sound != "" {
sound = zap.String("sound", req.Sound)
}
contentAvailable := zap.Skip()
if req.ContentAvailable {
contentAvailable = zap.Bool("content_available", req.ContentAvailable)
}
expiry := zap.Skip()
if req.Expiry != 0 {
expiry = zap.Int("expiry", req.Expiry)
}

logger(req.Message,
zap.Uint64("id", id),
zap.String("platform", plat),
zap.String("token", token),
zap.String("type", status),
zap.Float64("ptime", ptime),
zap.String("error", errMsg),
collapseKey,
delayWhileIdle,
timeToLive,
badge,
sound,
contentAvailable,
expiry,
)
}

func numberingPush() uint64 {
Expand Down
Loading