2022-03-10 09:44:48 +00:00
|
|
|
package log
|
|
|
|
|
|
|
|
import (
|
2024-05-30 08:52:57 +00:00
|
|
|
"encoding"
|
|
|
|
"fmt"
|
2022-03-10 09:44:48 +00:00
|
|
|
"strconv"
|
2024-05-30 08:52:57 +00:00
|
|
|
"strings"
|
2022-03-10 09:44:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Level struct {
|
2024-05-30 08:52:57 +00:00
|
|
|
rank int
|
2022-03-10 09:44:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2024-05-30 08:52:57 +00:00
|
|
|
Never = Level{-1} // A message at this level should never be logged.
|
|
|
|
NotSet = Level{0}
|
|
|
|
Debug = Level{1}
|
|
|
|
Info = Level{2}
|
|
|
|
Warning = Level{3}
|
|
|
|
Error = Level{4}
|
|
|
|
Critical = Level{5}
|
|
|
|
disabled = Level{6} // It shouldn't be possible to define a message at this level.
|
2022-03-10 09:44:48 +00:00
|
|
|
)
|
|
|
|
|
2024-05-30 08:52:57 +00:00
|
|
|
func (l Level) isNotSet() bool {
|
|
|
|
return l.rank == 0
|
|
|
|
}
|
|
|
|
|
2022-03-10 09:44:48 +00:00
|
|
|
func (l Level) LogString() string {
|
|
|
|
switch l.rank {
|
|
|
|
case NotSet.rank:
|
2024-05-30 08:52:57 +00:00
|
|
|
return "NIL"
|
2022-03-10 09:44:48 +00:00
|
|
|
case Debug.rank:
|
2024-05-30 08:52:57 +00:00
|
|
|
return "DBG"
|
2022-03-10 09:44:48 +00:00
|
|
|
case Info.rank:
|
2024-05-30 08:52:57 +00:00
|
|
|
return "INF"
|
2022-03-10 09:44:48 +00:00
|
|
|
case Warning.rank:
|
2024-05-30 08:52:57 +00:00
|
|
|
return "WRN"
|
2022-03-10 09:44:48 +00:00
|
|
|
case Error.rank:
|
2024-05-30 08:52:57 +00:00
|
|
|
return "ERR"
|
2022-03-10 09:44:48 +00:00
|
|
|
case Critical.rank:
|
2024-05-30 08:52:57 +00:00
|
|
|
return "CRT"
|
2022-03-10 09:44:48 +00:00
|
|
|
default:
|
|
|
|
return strconv.FormatInt(int64(l.rank), 10)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l Level) LessThan(r Level) bool {
|
|
|
|
if l.rank == NotSet.rank {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return l.rank < r.rank
|
|
|
|
}
|
2024-05-30 08:52:57 +00:00
|
|
|
|
|
|
|
var _ encoding.TextUnmarshaler = (*Level)(nil)
|
|
|
|
|
|
|
|
func (l *Level) UnmarshalText(text []byte) error {
|
|
|
|
switch strings.ToLower(string(text)) {
|
|
|
|
case "nil", "notset", "unset", "all", "*":
|
|
|
|
*l = NotSet
|
|
|
|
case "dbg", "debug":
|
|
|
|
*l = Debug
|
|
|
|
case "inf", "info":
|
|
|
|
*l = Info
|
|
|
|
case "wrn", "warning", "warn":
|
|
|
|
*l = Warning
|
|
|
|
case "err", "error":
|
|
|
|
*l = Error
|
|
|
|
case "crt", "critical", "crit":
|
|
|
|
*l = Critical
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("unknown log level: %q", text)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|