github.com/jxskiss/gopkg/v2@v2.14.9-0.20240514120614-899f3e7952b4/zlog/level.go (about)

     1  package zlog
     2  
     3  import (
     4  	"strconv"
     5  	"strings"
     6  
     7  	"go.uber.org/zap/zapcore"
     8  
     9  	"github.com/jxskiss/gopkg/v2/zlog/internal/terminal"
    10  )
    11  
    12  // Level is an alias type of zapcore.Level.
    13  type Level = zapcore.Level
    14  
    15  const (
    16  	// TraceLevel logs are the most fine-grained information which helps
    17  	// developer "tracing" their code.
    18  	// Users can expect this level to be very verbose, you can use it,
    19  	// for example, to annotate each step in an algorithm or each individual
    20  	// calling with parameters in your program.
    21  	// TraceLevel logs should be disabled in production.
    22  	TraceLevel Level = -2
    23  
    24  	// DebugLevel logs are typically voluminous, and are usually disabled in
    25  	// production.
    26  	DebugLevel = zapcore.DebugLevel
    27  	// InfoLevel is the default logging priority.
    28  	InfoLevel = zapcore.InfoLevel
    29  	// WarnLevel logs are more important than Info, but don't need individual
    30  	// human review.
    31  	WarnLevel = zapcore.WarnLevel
    32  	// ErrorLevel logs are high-priority. If an application is running smoothly,
    33  	// it shouldn't generate any error-level logs.
    34  	ErrorLevel = zapcore.ErrorLevel
    35  	// DPanicLevel logs are particularly important errors. In development the
    36  	// logger panics after writing the message.
    37  	DPanicLevel = zapcore.DPanicLevel
    38  	// PanicLevel logs a message, then panics.
    39  	PanicLevel = zapcore.PanicLevel
    40  	// FatalLevel logs a message, then calls os.Exit(1).
    41  	FatalLevel = zapcore.FatalLevel
    42  )
    43  
    44  func unmarshalLevel(l *Level, text string) bool {
    45  	switch text {
    46  	case "trace", "TRACE":
    47  		*l = TraceLevel
    48  	case "debug", "DEBUG":
    49  		*l = DebugLevel
    50  	case "info", "INFO", "": // make the zero value useful
    51  		*l = InfoLevel
    52  	case "warn", "warning", "WARN", "WARNING":
    53  		*l = WarnLevel
    54  	case "error", "ERROR":
    55  		*l = ErrorLevel
    56  	case "dpanic", "DPANIC":
    57  		*l = DPanicLevel
    58  	case "panic", "PANIC":
    59  		*l = PanicLevel
    60  	case "fatal", "FATAL":
    61  		*l = FatalLevel
    62  	default:
    63  		str := text
    64  		if (strings.HasPrefix(str, "Level(") || strings.HasPrefix(str, "LEVEL(")) &&
    65  			strings.HasSuffix(str, ")") {
    66  			str = str[6 : len(str)-1]
    67  		}
    68  		i, err := strconv.Atoi(str)
    69  		if err != nil {
    70  			return false
    71  		}
    72  		*l = Level(i)
    73  	}
    74  	return true
    75  }
    76  
    77  func encodeLevelLowercase(lv zapcore.Level, enc zapcore.PrimitiveArrayEncoder) {
    78  	if lv == TraceLevel {
    79  		enc.AppendString("trace")
    80  	} else {
    81  		enc.AppendString(lv.String())
    82  	}
    83  }
    84  
    85  func encodeLevelCapital(lv zapcore.Level, enc zapcore.PrimitiveArrayEncoder) {
    86  	if lv == TraceLevel {
    87  		enc.AppendString("TRACE")
    88  	} else {
    89  		enc.AppendString(lv.CapitalString())
    90  	}
    91  }
    92  
    93  var colorCapitalLevels = [...]string{
    94  	0: terminal.Gray.Format("TRACE"),
    95  	1: terminal.Magenta.Format("DEBUG"),
    96  	2: terminal.Cyan.Format("INFO"),
    97  	3: terminal.Yellow.Format("WARN"),
    98  	4: terminal.Red.Format("ERROR"),
    99  }
   100  
   101  func encodeLevelColorCapital(lv zapcore.Level, enc zapcore.PrimitiveArrayEncoder) {
   102  	if lv >= TraceLevel && lv <= ErrorLevel {
   103  		enc.AppendString(colorCapitalLevels[lv+2])
   104  	} else if lv < TraceLevel {
   105  		enc.AppendString(terminal.Gray.Format(lv.CapitalString()))
   106  	} else { // lv > ErrorLevel
   107  		enc.AppendString(terminal.Red.Format(lv.CapitalString()))
   108  	}
   109  }