github.com/yacovm/fabric@v2.0.0-alpha.0.20191128145320-c5d4087dc723+incompatible/common/flogging/levels.go (about)

     1  /*
     2  Copyright IBM Corp. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package flogging
     8  
     9  import (
    10  	"fmt"
    11  	"math"
    12  
    13  	"go.uber.org/zap/zapcore"
    14  )
    15  
    16  const (
    17  	// DisabledLevel represents a disabled log level. Logs at this level should
    18  	// never be emitted.
    19  	DisabledLevel = zapcore.Level(math.MinInt8)
    20  
    21  	// PayloadLevel is used to log the extremely detailed message level debug
    22  	// information.
    23  	PayloadLevel = zapcore.Level(zapcore.DebugLevel - 1)
    24  )
    25  
    26  // NameToLevel converts a level name to a zapcore.Level.  If the level name is
    27  // unknown, zapcore.InfoLevel is returned.
    28  func NameToLevel(level string) zapcore.Level {
    29  	l, err := nameToLevel(level)
    30  	if err != nil {
    31  		return zapcore.InfoLevel
    32  	}
    33  	return l
    34  }
    35  
    36  func nameToLevel(level string) (zapcore.Level, error) {
    37  	switch level {
    38  	case "PAYLOAD", "payload":
    39  		return PayloadLevel, nil
    40  	case "DEBUG", "debug":
    41  		return zapcore.DebugLevel, nil
    42  	case "INFO", "info":
    43  		return zapcore.InfoLevel, nil
    44  	case "WARNING", "WARN", "warning", "warn":
    45  		return zapcore.WarnLevel, nil
    46  	case "ERROR", "error":
    47  		return zapcore.ErrorLevel, nil
    48  	case "DPANIC", "dpanic":
    49  		return zapcore.DPanicLevel, nil
    50  	case "PANIC", "panic":
    51  		return zapcore.PanicLevel, nil
    52  	case "FATAL", "fatal":
    53  		return zapcore.FatalLevel, nil
    54  
    55  	case "NOTICE", "notice":
    56  		return zapcore.InfoLevel, nil // future
    57  	case "CRITICAL", "critical":
    58  		return zapcore.ErrorLevel, nil // future
    59  
    60  	default:
    61  		return DisabledLevel, fmt.Errorf("invalid log level: %s", level)
    62  	}
    63  }
    64  
    65  func IsValidLevel(level string) bool {
    66  	_, err := nameToLevel(level)
    67  	return err == nil
    68  }