blob: 0fc956743ff8ef145ddab53114f74b30e8b403a9 [file] [log] [blame]
// Copyright 2021 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build !disable_events
// Package elogrus provides a logrus Formatter for events.
// To use for the global logger:
// logrus.SetFormatter(elogrus.NewFormatter(exporter))
// logrus.SetOutput(io.Discard)
// and for a Logger instance:
// logger.SetFormatter(elogrus.NewFormatter(exporter))
// logger.SetOutput(io.Discard)
// If you call elogging.SetExporter, then you can pass nil
// for the exporter above and it will use the global one.
package elogrus
import (
type formatter struct{}
func NewFormatter() logrus.Formatter {
return &formatter{}
var _ logrus.Formatter = (*formatter)(nil)
// Format writes an entry to an event.Exporter. It always returns nil (see below).
// If e.Context is non-nil, Format gets the exporter from the context. Otherwise
// it uses the default exporter.
// Logrus first calls the Formatter to get a []byte, then writes that to the
// output. That doesn't work for events, so we subvert it by having the
// Formatter export the event (and thereby write it). That is why the logrus
// Output io.Writer should be set to io.Discard.
func (f *formatter) Format(e *logrus.Entry) ([]byte, error) {
ctx := e.Context
if ctx == nil {
ctx = context.Background()
b := event.To(ctx).At(e.Time)
for k, v := range e.Data {
return nil, nil
func convertLevel(level logrus.Level) event.Label {
switch level {
case logrus.PanicLevel:
return severity.Of(severity.FatalLevel + 1)
case logrus.FatalLevel:
return severity.Fatal
case logrus.ErrorLevel:
return severity.Error
case logrus.WarnLevel:
return severity.Warning
case logrus.InfoLevel:
return severity.Info
case logrus.DebugLevel:
return severity.Debug
case logrus.TraceLevel:
return severity.Trace
return severity.Trace