github.com/wangyougui/gf/v2@v2.6.5/os/glog/glog_logger_level.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/wangyougui/gf.
     6  
     7  package glog
     8  
     9  import (
    10  	"strings"
    11  
    12  	"github.com/wangyougui/gf/v2/errors/gcode"
    13  	"github.com/wangyougui/gf/v2/errors/gerror"
    14  )
    15  
    16  // Note that the LEVEL_PANI and LEVEL_FATA levels are not used for logging output,
    17  // but for prefix configurations.
    18  const (
    19  	LEVEL_ALL  = LEVEL_DEBU | LEVEL_INFO | LEVEL_NOTI | LEVEL_WARN | LEVEL_ERRO | LEVEL_CRIT
    20  	LEVEL_DEV  = LEVEL_ALL
    21  	LEVEL_PROD = LEVEL_WARN | LEVEL_ERRO | LEVEL_CRIT
    22  	LEVEL_NONE = 0
    23  	LEVEL_DEBU = 1 << iota // 16
    24  	LEVEL_INFO             // 32
    25  	LEVEL_NOTI             // 64
    26  	LEVEL_WARN             // 128
    27  	LEVEL_ERRO             // 256
    28  	LEVEL_CRIT             // 512
    29  	LEVEL_PANI             // 1024
    30  	LEVEL_FATA             // 2048
    31  )
    32  
    33  // defaultLevelPrefixes defines the default level and its mapping prefix string.
    34  var defaultLevelPrefixes = map[int]string{
    35  	LEVEL_DEBU: "DEBU",
    36  	LEVEL_INFO: "INFO",
    37  	LEVEL_NOTI: "NOTI",
    38  	LEVEL_WARN: "WARN",
    39  	LEVEL_ERRO: "ERRO",
    40  	LEVEL_CRIT: "CRIT",
    41  	LEVEL_PANI: "PANI",
    42  	LEVEL_FATA: "FATA",
    43  }
    44  
    45  // levelStringMap defines level string name to its level mapping.
    46  var levelStringMap = map[string]int{
    47  	"ALL":      LEVEL_DEBU | LEVEL_INFO | LEVEL_NOTI | LEVEL_WARN | LEVEL_ERRO | LEVEL_CRIT,
    48  	"DEV":      LEVEL_DEBU | LEVEL_INFO | LEVEL_NOTI | LEVEL_WARN | LEVEL_ERRO | LEVEL_CRIT,
    49  	"DEVELOP":  LEVEL_DEBU | LEVEL_INFO | LEVEL_NOTI | LEVEL_WARN | LEVEL_ERRO | LEVEL_CRIT,
    50  	"PROD":     LEVEL_WARN | LEVEL_ERRO | LEVEL_CRIT,
    51  	"PRODUCT":  LEVEL_WARN | LEVEL_ERRO | LEVEL_CRIT,
    52  	"DEBU":     LEVEL_DEBU | LEVEL_INFO | LEVEL_NOTI | LEVEL_WARN | LEVEL_ERRO | LEVEL_CRIT,
    53  	"DEBUG":    LEVEL_DEBU | LEVEL_INFO | LEVEL_NOTI | LEVEL_WARN | LEVEL_ERRO | LEVEL_CRIT,
    54  	"INFO":     LEVEL_INFO | LEVEL_NOTI | LEVEL_WARN | LEVEL_ERRO | LEVEL_CRIT,
    55  	"NOTI":     LEVEL_NOTI | LEVEL_WARN | LEVEL_ERRO | LEVEL_CRIT,
    56  	"NOTICE":   LEVEL_NOTI | LEVEL_WARN | LEVEL_ERRO | LEVEL_CRIT,
    57  	"WARN":     LEVEL_WARN | LEVEL_ERRO | LEVEL_CRIT,
    58  	"WARNING":  LEVEL_WARN | LEVEL_ERRO | LEVEL_CRIT,
    59  	"ERRO":     LEVEL_ERRO | LEVEL_CRIT,
    60  	"ERROR":    LEVEL_ERRO | LEVEL_CRIT,
    61  	"CRIT":     LEVEL_CRIT,
    62  	"CRITICAL": LEVEL_CRIT,
    63  }
    64  
    65  // SetLevel sets the logging level.
    66  // Note that levels ` LEVEL_CRIT | LEVEL_PANI | LEVEL_FATA ` cannot be removed for logging content,
    67  // which are automatically added to levels.
    68  func (l *Logger) SetLevel(level int) {
    69  	l.config.Level = level | LEVEL_CRIT | LEVEL_PANI | LEVEL_FATA
    70  }
    71  
    72  // GetLevel returns the logging level value.
    73  func (l *Logger) GetLevel() int {
    74  	return l.config.Level
    75  }
    76  
    77  // SetLevelStr sets the logging level by level string.
    78  func (l *Logger) SetLevelStr(levelStr string) error {
    79  	if level, ok := levelStringMap[strings.ToUpper(levelStr)]; ok {
    80  		l.config.Level = level
    81  	} else {
    82  		return gerror.NewCodef(gcode.CodeInvalidParameter, `invalid level string: %s`, levelStr)
    83  	}
    84  	return nil
    85  }
    86  
    87  // SetLevelPrefix sets the prefix string for specified level.
    88  func (l *Logger) SetLevelPrefix(level int, prefix string) {
    89  	l.config.LevelPrefixes[level] = prefix
    90  }
    91  
    92  // SetLevelPrefixes sets the level to prefix string mapping for the logger.
    93  func (l *Logger) SetLevelPrefixes(prefixes map[int]string) {
    94  	for k, v := range prefixes {
    95  		l.config.LevelPrefixes[k] = v
    96  	}
    97  }
    98  
    99  // GetLevelPrefix returns the prefix string for specified level.
   100  func (l *Logger) GetLevelPrefix(level int) string {
   101  	return l.config.LevelPrefixes[level]
   102  }
   103  
   104  // getLevelPrefixWithBrackets returns the prefix string with brackets for specified level.
   105  func (l *Logger) getLevelPrefixWithBrackets(level int) string {
   106  	levelStr := ""
   107  	if s, ok := l.config.LevelPrefixes[level]; ok {
   108  		levelStr = "[" + s + "]"
   109  	}
   110  	return levelStr
   111  }