1 Star 1 Fork 2

allan577/go-lib-logger

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
level.go 3.45 KB
一键复制 编辑 原始数据 按行查看 历史
allan577 提交于 4年前 . init
package logger
import (
"fmt"
"strings"
)
const (
// PanicLevel indicates a very serious event at the highest level.
PanicLevel Level = iota + 1
// FatalLevel indicates that an event occurred that the application
// cannot continue to run.
FatalLevel
// ErrorLevel indicates that an event occurred within the application
// but does not affect continued operation.
ErrorLevel
// WarnLevel indicates that a noteworthy event has occurred inside
// the application.
WarnLevel
// InfoLevel represents some general information events.
InfoLevel
// DebugLevel represents some informational events for debugging, and
// very verbose logging.
DebugLevel
// TraceLevel represents the finest granular information event.
TraceLevel
)
// Level is the level of the log.
type Level uint32
// All supported log levels.
var allLevels = map[Level][]string{
PanicLevel: {"panic", "PANIC", "pnc", "PNC"},
FatalLevel: {"fatal", "FATAL", "fat", "FAT"},
ErrorLevel: {"error", "ERROR", "err", "ERR"},
WarnLevel: {"warn", "WARN", "wan", "WAN"},
InfoLevel: {"info", "INFO", "inf", "INF"},
DebugLevel: {"debug", "DEBUG", "dbg", "DBG"},
TraceLevel: {"trace", "TRACE", "tac", "TAC"},
}
// String returns the string form of the current level.
// If the log level is not supported, always returns "unknown".
func (level Level) String() string {
if s, found := allLevels[level]; found {
return s[0]
}
return "unknown"
}
// CapitalString returns the capital string form of the current level.
// If the log level is not supported, always returns "UNKNOWN".
func (level Level) CapitalString() string {
if s, found := allLevels[level]; found {
return s[1]
}
return "UNKNOWN"
}
// ShortString returns the short string form of the current level.
// If the log level is not supported, always returns "uno".
func (level Level) ShortString() string {
if s, found := allLevels[level]; found {
return s[2]
}
return "uno"
}
// ShortCapitalString returns the short capital string form of the current level.
// If the log level is not supported, always returns "UNO".
func (level Level) ShortCapitalString() string {
if s, found := allLevels[level]; found {
return s[3]
}
return "UNO"
}
// IsValid determines whether the current level is valid.
func (level Level) IsValid() bool {
return level <= TraceLevel && level >= PanicLevel
}
// IsEnabled returns whether the given level is included in the current level.
func (level Level) IsEnabled(l Level) bool {
return l <= level && l > 0
}
// ParseLevel parses the log level from the given string.
func ParseLevel(s string) (Level, error) {
switch strings.ToLower(strings.TrimSpace(s)) {
case "panic", "pnc":
return PanicLevel, nil
case "fatal", "fat":
return FatalLevel, nil
case "error", "err":
return ErrorLevel, nil
case "warn", "wan", "warning":
return WarnLevel, nil
case "info", "inf", "echo":
return InfoLevel, nil
case "debug", "dbg":
return DebugLevel, nil
case "trace", "tac", "print":
return TraceLevel, nil
}
// A level zero value is not a supported level.
return 0, fmt.Errorf("invalid log level string %q", s)
}
// MustParseLevel parses the log level from the given string.
// If the given string is invalid, it will panic.
func MustParseLevel(s string) Level {
level, err := ParseLevel(s)
if err != nil {
panic(err)
}
return level
}
// GetAllLevels returns all supported log levels.
func GetAllLevels() []Level {
return []Level{PanicLevel, FatalLevel, ErrorLevel, WarnLevel, InfoLevel, DebugLevel, TraceLevel}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/allan577/go-lib-logger.git
git@gitee.com:allan577/go-lib-logger.git
allan577
go-lib-logger
go-lib-logger
v1.0.0

搜索帮助