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

Commit

Permalink
Bridge Cilium log usage to zap
Browse files Browse the repository at this point in the history
Cilium uses logrus instead of zap for logging. This change introducs a simple bridge that should funnel any logs from the Cilium codebase through the zap logger in netreap.

This is part of breaking up cosmonic-labs#33.
  • Loading branch information
deverton-godaddy committed Jan 12, 2024
1 parent 96dbb44 commit e5cc49f
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
71 changes: 71 additions & 0 deletions internal/zaplogrus/zap_logrus_hook.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package zaplogrus

import (
"fmt"

"github.com/sirupsen/logrus"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)

type ZapLogrusHook struct {
logger *zap.Logger
}

func NewZapLogrusHook(logger *zap.Logger) *ZapLogrusHook {
return &ZapLogrusHook{
logger: logger,
}
}

func (h *ZapLogrusHook) Fire(entry *logrus.Entry) error {
zapLevel := h.zapLevel(entry.Level)
if zapLevel == zapcore.InvalidLevel {
return fmt.Errorf("unhandled logrus level %v", entry.Level)
}

if ce := h.logger.Check(zapLevel, entry.Message); ce != nil {
caller := entry.Caller

if caller != nil {
ce.Caller = zapcore.NewEntryCaller(caller.PC, caller.File, caller.Line, caller.PC != 0)
}

fields := make([]zap.Field, 0, len(entry.Data))

for key, value := range entry.Data {
if key == logrus.ErrorKey {
fields = append(fields, zap.Error(value.(error)))
} else {
fields = append(fields, zap.Any(key, value))
}
}

ce.Write(fields...)
}

return nil
}

func (h *ZapLogrusHook) Levels() []logrus.Level {
return logrus.AllLevels
}

func (h *ZapLogrusHook) zapLevel(level logrus.Level) zapcore.Level {
switch level {
case logrus.PanicLevel:
return zapcore.PanicLevel
case logrus.FatalLevel:
return zapcore.FatalLevel
case logrus.ErrorLevel:
return zapcore.ErrorLevel
case logrus.WarnLevel:
return zapcore.WarnLevel
case logrus.InfoLevel:
return zapcore.InfoLevel
case logrus.DebugLevel, logrus.TraceLevel:
return zapcore.DebugLevel
default:
return zapcore.InvalidLevel
}
}
9 changes: 9 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@ package main
import (
"context"
"fmt"
"io"
"log"
"os"
"os/signal"

cilium_client "github.com/cilium/cilium/pkg/client"
cilium_logging "github.com/cilium/cilium/pkg/logging"
consul_api "github.com/hashicorp/consul/api"
nomad_api "github.com/hashicorp/nomad/api"
"github.com/urfave/cli/v2"
"go.uber.org/zap"

"github.com/cosmonic/netreap/internal/policy"
"github.com/cosmonic/netreap/internal/zaplogrus"
"github.com/cosmonic/netreap/reapers"
)

Expand Down Expand Up @@ -62,6 +65,12 @@ func main() {
logger = devlog
}
zap.ReplaceGlobals(logger)

// Bridge Cilium logrus to netreap zap
cilium_logging.DefaultLogger.SetReportCaller(true)
cilium_logging.DefaultLogger.SetOutput(io.Discard)
cilium_logging.DefaultLogger.AddHook(zaplogrus.NewZapLogrusHook(logger))

return nil
},
Action: func(c *cli.Context) error {
Expand Down

0 comments on commit e5cc49f

Please sign in to comment.