github.com/filecoin-project/bacalhau@v0.3.23-0.20230228154132-45c989550ace/pkg/publicapi/handlerwrapper/log_handler.go (about)

     1  package handlerwrapper
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/filecoin-project/bacalhau/pkg/model"
     7  	"github.com/filecoin-project/bacalhau/pkg/system"
     8  	"github.com/rs/zerolog/log"
     9  )
    10  
    11  type JSONLogHandler struct {
    12  }
    13  
    14  func NewJSONLogHandler() *JSONLogHandler {
    15  	return &JSONLogHandler{}
    16  }
    17  
    18  func (h *JSONLogHandler) Handle(ctx context.Context, ri *HTTPRequestInfo) {
    19  	jsonBytes, err := model.JSONMarshalWithMax(ri)
    20  	if err != nil {
    21  		log.Ctx(ctx).Info().Err(err).Msgf("failed to marshal request info %+v", ri)
    22  	}
    23  	if ri.StatusCode >= 400 { //nolint:gomnd
    24  		log.Ctx(ctx).Error().Msg(string(jsonBytes))
    25  	} else {
    26  		// TODO: #830 Same as #829 in pkg/eventhandler/chained_handlers.go
    27  		if system.GetEnvironment() == system.EnvironmentTest ||
    28  			system.GetEnvironment() == system.EnvironmentDev {
    29  			return
    30  		}
    31  		log.Ctx(ctx).Info().Msg(string(jsonBytes))
    32  	}
    33  }