This repository has been archived by the owner on Oct 30, 2024. It is now read-only.
forked from cosmonic-labs/netreap
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
1 parent
96dbb44
commit e5cc49f
Showing
2 changed files
with
80 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters