github.com/facebookincubator/go-belt@v0.0.0-20230703220935-39cd348f1a38/tool/experimental/errmon/implementation/logger/errmon.go (about)

     1  // Copyright 2022 Meta Platforms, Inc. and affiliates.
     2  //
     3  // Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
     4  //
     5  // 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
     6  //
     7  // 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
     8  //
     9  // 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
    10  //
    11  // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    12  
    13  package logger
    14  
    15  import (
    16  	"fmt"
    17  
    18  	errmonadapter "github.com/facebookincubator/go-belt/tool/experimental/errmon/adapter"
    19  	errmontypes "github.com/facebookincubator/go-belt/tool/experimental/errmon/types"
    20  	"github.com/facebookincubator/go-belt/tool/logger"
    21  )
    22  
    23  // Emitter is an implementation of errmon.Emitter which just prints the events
    24  // through a given Logger.
    25  type Emitter struct {
    26  	Logger logger.Logger
    27  }
    28  
    29  // NewEmitter returns a new instance of Emitter.
    30  func NewEmitter(logger logger.Logger) *Emitter {
    31  	return &Emitter{
    32  		Logger: logger,
    33  	}
    34  }
    35  
    36  // New returns an instance of errmon.ErrorMonitor which prints all the events
    37  // through a given Logger.
    38  func New(logger logger.Logger, opts ...Option) errmontypes.ErrorMonitor {
    39  	return errmonadapter.ErrorMonitorFromEmitter(
    40  		NewEmitter(logger),
    41  		options(opts).Config().CallerFrameFilter,
    42  	)
    43  }
    44  
    45  // Default is an overridable function which returns an ErrorMonitor with the default configuration.
    46  var Default = func() errmontypes.ErrorMonitor {
    47  	return New(logger.Default())
    48  }
    49  
    50  // Flush implements errmon.Emitter
    51  func (*Emitter) Flush() {}
    52  
    53  // Emit implements errmon.Emitter
    54  func (h *Emitter) Emit(ev *errmontypes.Event) {
    55  	switch {
    56  	case ev.Exception.IsPanic:
    57  		ev.Entry.Message = fmt.Sprintf("got panic with argument: %v", ev.Exception.PanicValue)
    58  	case ev.Exception.Error != nil:
    59  		ev.Entry.Message = ev.Exception.Error.Error()
    60  	}
    61  	level := eventLevel(ev)
    62  	ev.Entry.Level = level
    63  	h.Logger.Log(level, &ev.Entry)
    64  }
    65  
    66  func eventLevel(ev *errmontypes.Event) logger.Level {
    67  	if ev.Level < logger.LevelError {
    68  		return logger.LevelError
    69  	}
    70  	return ev.Level
    71  }