From 032a7e0c1188d3507b8d9a9571f2446a43cf775b Mon Sep 17 00:00:00 2001 From: msglm Date: Fri, 27 Oct 2023 07:08:25 -0500 Subject: apply https://github.com/42wim/matterbridge/pull/1864 --- bridge/matrix/zerolog_abstraction.go | 43 ++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 bridge/matrix/zerolog_abstraction.go (limited to 'bridge/matrix/zerolog_abstraction.go') diff --git a/bridge/matrix/zerolog_abstraction.go b/bridge/matrix/zerolog_abstraction.go new file mode 100644 index 00000000..3d74a4e0 --- /dev/null +++ b/bridge/matrix/zerolog_abstraction.go @@ -0,0 +1,43 @@ +package bmatrix + +import ( + "errors" + + "github.com/sirupsen/logrus" + + "github.com/rs/zerolog" +) + +var levels_zerolog2logrus = map[zerolog.Level]logrus.Level{ + zerolog.DebugLevel: logrus.DebugLevel, + zerolog.InfoLevel: logrus.InfoLevel, + zerolog.WarnLevel: logrus.WarnLevel, + zerolog.FatalLevel: logrus.FatalLevel, + zerolog.PanicLevel: logrus.PanicLevel, + zerolog.ErrorLevel: logrus.ErrorLevel, + zerolog.TraceLevel: logrus.TraceLevel, +} + +// an abstraction for zerolog so we can pipe its output to logrus.Entry, that is used in matterbridge +type zerologWrapper struct { + inner *logrus.Entry +} + +func (w zerologWrapper) Write(p []byte) (n int, err error) { + return w.inner.Logger.Writer().Write(p) +} + +func (w zerologWrapper) WriteLevel(level zerolog.Level, p []byte) (n int, err error) { + if logrus_level, present := levels_zerolog2logrus[level]; present { + return w.inner.Logger.WriterLevel(logrus_level).Write(p) + } + // drop the message if we haven't a matching level + return 0, errors.New("Unsupported logging level") +} + +func NewZerologWrapper(entry *logrus.Entry) zerolog.Logger { + wrapper := zerologWrapper{inner: entry} + log := zerolog.New(wrapper) + + return log +} -- cgit v1.2.3