github.com/volts-dev/volts@v0.0.0-20240120094013-5e9c65924106/logger/level.go (about)

     1  package logger
     2  
     3  import "runtime"
     4  
     5  type Level int8
     6  type Brush func(string) string
     7  
     8  var Reset = "\033[0m"
     9  var Red = "\033[31m"
    10  var Green = "\033[32m"
    11  var Yellow = "\033[33m"
    12  var Blue = "\033[34m"
    13  var Purple = "\033[35m"
    14  var Cyan = "\033[36m"
    15  var Gray = "\033[37m"
    16  var White = "\033[97m"
    17  
    18  // RFC5424 log message levels.
    19  const (
    20  	LevelAttack    Level = iota // under attack
    21  	LevelCritical               //
    22  	LevelAlert                  //
    23  	LevelEmergency              //
    24  	LevelNone                   // logger is close
    25  	LevelInfo                   //
    26  	LevelWarn                   //
    27  	LevelError                  //
    28  	LevelTrace                  // 跟踪
    29  	LevelDebug                  // under debug mode
    30  )
    31  
    32  func (lv Level) Color(msg string) string {
    33  	if runtime.GOOS == "windows" {
    34  		return ""
    35  	}
    36  
    37  	color := ""
    38  	switch lv {
    39  	case LevelAttack:
    40  		color = Red
    41  	case LevelCritical:
    42  		color = Red
    43  	case LevelAlert:
    44  		color = Yellow
    45  	case LevelEmergency:
    46  		color = Yellow
    47  	case LevelInfo:
    48  		color = White
    49  	case LevelWarn:
    50  		color = Yellow
    51  	case LevelError:
    52  		color = Red
    53  	case LevelDebug:
    54  		color = Purple
    55  	case LevelTrace:
    56  		color = Blue
    57  	default:
    58  		color = White
    59  	}
    60  	return color + msg + Reset
    61  }
    62  
    63  func (lv Level) String() string {
    64  	switch lv {
    65  	case LevelAttack:
    66  		return "attack"
    67  	case LevelCritical:
    68  		return "critical"
    69  	case LevelAlert:
    70  		return "alert"
    71  	case LevelEmergency:
    72  		return "emergency"
    73  	case LevelInfo:
    74  		return "info"
    75  	case LevelWarn:
    76  		return "warn"
    77  	case LevelError:
    78  		return "error"
    79  	case LevelDebug:
    80  		return "debug"
    81  	case LevelTrace:
    82  		return "trace"
    83  	}
    84  	return "unknow"
    85  }