code.vegaprotocol.io/vega@v0.79.0/libs/zap/level.go (about)

     1  // Copyright (C) 2023 Gobalsky Labs Limited
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU Affero General Public License as
     5  // published by the Free Software Foundation, either version 3 of the
     6  // License, or (at your option) any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU Affero General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU Affero General Public License
    14  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    15  
    16  package zap
    17  
    18  import (
    19  	"fmt"
    20  	"strings"
    21  
    22  	"go.uber.org/zap"
    23  	"go.uber.org/zap/zapcore"
    24  )
    25  
    26  var SupportedLogLevels = []string{
    27  	zapcore.DebugLevel.String(),
    28  	zapcore.InfoLevel.String(),
    29  	zapcore.WarnLevel.String(),
    30  	zapcore.ErrorLevel.String(),
    31  }
    32  
    33  func IsSupportedLogLevel(level string) bool {
    34  	for _, supported := range SupportedLogLevels {
    35  		if level == supported {
    36  			return true
    37  		}
    38  	}
    39  	return false
    40  }
    41  
    42  func EnsureIsSupportedLogLevel(level string) error {
    43  	if !IsSupportedLogLevel(level) {
    44  		return fmt.Errorf("unsupported log level %q, supported levels: %s", level, strings.Join(SupportedLogLevels, ", "))
    45  	}
    46  	return nil
    47  }
    48  
    49  func parseLevel(level string) (zap.AtomicLevel, error) {
    50  	if err := EnsureIsSupportedLogLevel(level); err != nil {
    51  		return zap.AtomicLevel{}, err
    52  	}
    53  
    54  	l := new(zapcore.Level)
    55  
    56  	if err := l.UnmarshalText([]byte(level)); err != nil {
    57  		return zap.AtomicLevel{}, fmt.Errorf("couldn't parse log level: %w", err)
    58  	}
    59  
    60  	return zap.NewAtomicLevelAt(*l), nil
    61  }