You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
crate/log/logsdk/level.go

105 lines
2.0 KiB
Go

package logsdk
import (
"fmt"
)
// Level 日志等级
type Level int
func (level Level) String() string {
if b, err := level.MarshalText(); err == nil {
return string(b)
}
return "unknown"
}
// ParseLevel 把 level 字符串解析成 Level,
// 支持的字符串: panic, fatal, error, warn, info, debug, trace,
// 传入不支持的字符串返回 LevelInfo.
func ParseLevel(s string) Level {
switch s {
case LevelPanicValue:
return LevelPanic
case LevelFatalValue:
return LevelFatal
case LevelErrorValue:
return LevelError
case LevelWarnValue:
return LevelWarn
case LevelInfoValue:
return LevelInfo
case LevelDebugValue:
return LevelDebug
case LevelTraceValue:
return LevelTrace
default:
return LevelInfo
}
}
func (level Level) MarshalText() ([]byte, error) {
switch level {
case LevelPanic:
return []byte(LevelPanicValue), nil
case LevelFatal:
return []byte(LevelFatalValue), nil
case LevelError:
return []byte(LevelErrorValue), nil
case LevelWarn:
return []byte(LevelWarnValue), nil
case LevelInfo:
return []byte(LevelInfoValue), nil
case LevelDebug:
return []byte(LevelDebugValue), nil
case LevelTrace:
return []byte(LevelTraceValue), nil
case LevelDisabled:
return []byte(levelDisabledValue), nil
}
return nil, fmt.Errorf("not a valid log level %d", level)
}
const (
// LevelCount 日志等级的个数
LevelCount = 7
// levelOffset 是把 LevelInfo 等级的日志值偏移到零值的偏移量
levelOffset = 4
)
const (
// LevelDisabled 表示不处理任何等级的日志,
// 其他日志等级的值越小表示日志严重程度越高.
LevelDisabled Level = iota - levelOffset - 1
LevelPanic
LevelFatal
LevelError
LevelWarn
LevelInfo
LevelDebug
LevelTrace
)
const (
LevelPanicValue = "panic"
LevelFatalValue = "fatal"
LevelErrorValue = "error"
LevelWarnValue = "warn"
LevelInfoValue = "info"
LevelDebugValue = "debug"
LevelTraceValue = "trace"
levelDisabledValue = "disabled"
)
var AllLevels = []Level{
LevelPanic,
LevelFatal,
LevelError,
LevelWarn,
LevelInfo,
LevelDebug,
LevelTrace,
}