This repository has been archived on 2025-04-28. You can view files and clone it, but cannot push or open issues or pull requests.
CMS/1.Software/UI/gateway-ui/log/level.go
2024-11-19 17:19:21 +08:00

51 lines
762 B
Go

package logger
import (
"errors"
"strings"
)
const (
Error Level = iota + 1
Warn
Info
Debug
)
var ErrInvalidLogLevel = errors.New("unrecognized log level")
// 指定打印日志时的级别
type Level int
var levels = map[Level]string{
Error: "error",
Warn: "warn",
Info: "info",
Debug: "debug",
}
func (lvl Level) String() string {
return levels[lvl]
}
func (lvl Level) isAllowed(logLevel Level) bool {
return lvl <= logLevel
}
// 根据给入的字符串返回日志级别的int值
func (lvl *Level) UnmarshalText(text string) error {
switch strings.ToLower(text) {
case "debug":
*lvl = Debug
case "info":
*lvl = Info
case "warn":
*lvl = Warn
case "error":
*lvl = Error
default:
return ErrInvalidLogLevel
}
return nil
}