github.com/koko1123/flow-go-1@v0.29.6/admin/commands/common/set_log_level.go (about)

     1  package common
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/rs/zerolog"
     7  	"github.com/rs/zerolog/log"
     8  
     9  	"github.com/koko1123/flow-go-1/admin"
    10  	"github.com/koko1123/flow-go-1/admin/commands"
    11  )
    12  
    13  var _ commands.AdminCommand = (*SetLogLevelCommand)(nil)
    14  
    15  type SetLogLevelCommand struct{}
    16  
    17  func (s *SetLogLevelCommand) Handler(_ context.Context, req *admin.CommandRequest) (interface{}, error) {
    18  	level := req.ValidatorData.(zerolog.Level)
    19  	zerolog.SetGlobalLevel(level)
    20  
    21  	log.Info().Msgf("changed log level to %v", level)
    22  	return "ok", nil
    23  }
    24  
    25  // Validator validates the request.
    26  // Returns admin.InvalidAdminReqError for invalid/malformed requests.
    27  func (s *SetLogLevelCommand) Validator(req *admin.CommandRequest) error {
    28  	level, ok := req.Data.(string)
    29  	if !ok {
    30  		return admin.NewInvalidAdminReqFormatError("the input must be a string")
    31  	}
    32  	logLevel, err := zerolog.ParseLevel(level)
    33  	if err != nil {
    34  		return admin.NewInvalidAdminReqErrorf("failed to parse level: %w", err)
    35  	}
    36  	req.ValidatorData = logLevel
    37  	return nil
    38  }