use zap for logging (#1112)

Use zap for logging
This commit is contained in:
Jon Carl
2019-03-14 21:11:15 +05:30
committed by Vishal
parent c717f182b6
commit 0fc864f230
98 changed files with 2031 additions and 1223 deletions
+7 -6
View File
@@ -17,25 +17,26 @@ limitations under the License.
package timer
import (
"log"
"github.com/pkg/errors"
"go.uber.org/zap"
"github.com/fission/fission/crd"
"github.com/fission/fission/publisher"
)
func Start(routerUrl string) error {
func Start(logger *zap.Logger, routerUrl string) error {
fissionClient, _, _, err := crd.MakeFissionClient()
if err != nil {
return err
return errors.Wrap(err, "failed to get fission or kubernetes client")
}
err = fissionClient.WaitForCRDs()
if err != nil {
log.Fatalf("Error waiting for CRDs: %v", err)
return errors.Wrap(err, "error waiting for CRDs")
}
poster := publisher.MakeWebhookPublisher(routerUrl)
MakeTimerSync(fissionClient, MakeTimer(poster))
poster := publisher.MakeWebhookPublisher(logger, routerUrl)
MakeTimerSync(logger, fissionClient, MakeTimer(logger, poster))
return nil
}
+6 -5
View File
@@ -17,9 +17,8 @@ limitations under the License.
package timer
import (
"log"
"github.com/robfig/cron"
"go.uber.org/zap"
"github.com/fission/fission"
"github.com/fission/fission/crd"
@@ -34,6 +33,7 @@ const (
type (
Timer struct {
logger *zap.Logger
triggers map[string]*timerTriggerWithCron
requestChannel chan *timerRequest
publisher *publisher.Publisher
@@ -53,8 +53,9 @@ type (
}
)
func MakeTimer(publisher publisher.Publisher) *Timer {
func MakeTimer(logger *zap.Logger, publisher publisher.Publisher) *Timer {
timer := &Timer{
logger: logger.Named("timer"),
triggers: make(map[string]*timerTriggerWithCron),
requestChannel: make(chan *timerRequest),
publisher: &publisher,
@@ -114,7 +115,7 @@ func (timer *Timer) syncCron(triggers []crd.TimeTrigger) error {
if _, found := triggerMap[k]; !found {
if v.cron != nil {
v.cron.Stop()
log.Printf("Cron for time trigger %s stopped", v.trigger.Metadata.Name)
timer.logger.Info("cron for time trigger stopped", zap.String("trigger", v.trigger.Metadata.Name))
}
delete(timer.triggers, k)
}
@@ -136,6 +137,6 @@ func (timer *Timer) newCron(t crd.TimeTrigger) *cron.Cron {
(*timer.publisher).Publish("", headers, fission.UrlForFunction(t.Spec.FunctionReference.Name, t.Metadata.Namespace))
})
c.Start()
log.Printf("Add new cron for time trigger %v", t.Metadata.Name)
timer.logger.Info("added new cron for time trigger", zap.String("trigger", t.Metadata.Name))
return c
}
+6 -4
View File
@@ -17,9 +17,9 @@ limitations under the License.
package timer
import (
"log"
"time"
"go.uber.org/zap"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/fission/fission"
@@ -28,13 +28,15 @@ import (
type (
TimerSync struct {
logger *zap.Logger
fissionClient *crd.FissionClient
timer *Timer
}
)
func MakeTimerSync(fissionClient *crd.FissionClient, timer *Timer) *TimerSync {
func MakeTimerSync(logger *zap.Logger, fissionClient *crd.FissionClient, timer *Timer) *TimerSync {
ws := &TimerSync{
logger: logger.Named("timer_sync"),
fissionClient: fissionClient,
timer: timer,
}
@@ -47,11 +49,11 @@ func (ws *TimerSync) syncSvc() {
triggers, err := ws.fissionClient.TimeTriggers(metav1.NamespaceAll).List(metav1.ListOptions{})
if err != nil {
if fission.IsNetworkError(err) {
log.Printf("Encounter network error, retry again: %v", err)
ws.logger.Info("encountered a network error - will retry", zap.Error(err))
time.Sleep(5 * time.Second)
continue
}
log.Fatalf("Failed get time trigger list: %v", err)
ws.logger.Fatal("failed to get time trigger list", zap.Error(err))
}
ws.timer.Sync(triggers.Items)