2018-11-21 13:47:46 +08:00
|
|
|
package log
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2024-09-19 18:06:36 +08:00
|
|
|
"strings"
|
2018-11-21 13:47:46 +08:00
|
|
|
)
|
|
|
|
|
2021-10-10 23:44:09 +08:00
|
|
|
// LogLevelMapping is a mapping for LogLevel enum
|
|
|
|
var LogLevelMapping = map[string]LogLevel{
|
|
|
|
ERROR.String(): ERROR,
|
|
|
|
WARNING.String(): WARNING,
|
|
|
|
INFO.String(): INFO,
|
|
|
|
DEBUG.String(): DEBUG,
|
|
|
|
SILENT.String(): SILENT,
|
|
|
|
}
|
2018-11-21 13:47:46 +08:00
|
|
|
|
|
|
|
const (
|
|
|
|
DEBUG LogLevel = iota
|
|
|
|
INFO
|
|
|
|
WARNING
|
|
|
|
ERROR
|
2018-12-03 23:41:40 +08:00
|
|
|
SILENT
|
2018-11-21 13:47:46 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type LogLevel int
|
|
|
|
|
2018-12-05 21:13:29 +08:00
|
|
|
// UnmarshalYAML unserialize LogLevel with yaml
|
2022-03-16 12:10:13 +08:00
|
|
|
func (l *LogLevel) UnmarshalYAML(unmarshal func(any) error) error {
|
2018-11-21 13:47:46 +08:00
|
|
|
var tp string
|
2018-12-05 21:13:29 +08:00
|
|
|
unmarshal(&tp)
|
2024-09-19 18:06:36 +08:00
|
|
|
level, exist := LogLevelMapping[strings.ToLower(tp)]
|
2018-11-21 13:47:46 +08:00
|
|
|
if !exist {
|
|
|
|
return errors.New("invalid mode")
|
|
|
|
}
|
|
|
|
*l = level
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-12-05 21:13:29 +08:00
|
|
|
// UnmarshalJSON unserialize LogLevel with json
|
2018-11-21 13:47:46 +08:00
|
|
|
func (l *LogLevel) UnmarshalJSON(data []byte) error {
|
|
|
|
var tp string
|
2018-11-28 10:38:30 +08:00
|
|
|
json.Unmarshal(data, &tp)
|
2024-09-19 18:06:36 +08:00
|
|
|
level, exist := LogLevelMapping[strings.ToLower(tp)]
|
2018-11-21 13:47:46 +08:00
|
|
|
if !exist {
|
|
|
|
return errors.New("invalid mode")
|
|
|
|
}
|
|
|
|
*l = level
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-09-19 18:06:36 +08:00
|
|
|
// UnmarshalText unserialize LogLevel
|
|
|
|
func (l *LogLevel) UnmarshalText(data []byte) error {
|
|
|
|
level, exist := LogLevelMapping[strings.ToLower(string(data))]
|
|
|
|
if !exist {
|
|
|
|
return errors.New("invalid mode")
|
|
|
|
}
|
|
|
|
*l = level
|
|
|
|
return nil
|
2018-11-21 13:47:46 +08:00
|
|
|
}
|
|
|
|
|
2020-03-15 19:40:39 +08:00
|
|
|
// MarshalYAML serialize LogLevel with yaml
|
2022-03-16 12:10:13 +08:00
|
|
|
func (l LogLevel) MarshalYAML() (any, error) {
|
2020-03-15 19:40:39 +08:00
|
|
|
return l.String(), nil
|
|
|
|
}
|
|
|
|
|
2024-09-19 18:06:36 +08:00
|
|
|
// MarshalJSON serialize LogLevel with json
|
|
|
|
func (l LogLevel) MarshalJSON() ([]byte, error) {
|
|
|
|
return json.Marshal(l.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalText serialize LogLevel
|
|
|
|
func (l LogLevel) MarshalText() ([]byte, error) {
|
|
|
|
return []byte(l.String()), nil
|
|
|
|
}
|
|
|
|
|
2018-11-21 13:47:46 +08:00
|
|
|
func (l LogLevel) String() string {
|
|
|
|
switch l {
|
|
|
|
case INFO:
|
|
|
|
return "info"
|
|
|
|
case WARNING:
|
|
|
|
return "warning"
|
|
|
|
case ERROR:
|
|
|
|
return "error"
|
|
|
|
case DEBUG:
|
|
|
|
return "debug"
|
2018-12-03 23:41:40 +08:00
|
|
|
case SILENT:
|
|
|
|
return "silent"
|
2018-11-21 13:47:46 +08:00
|
|
|
default:
|
2018-12-05 21:13:29 +08:00
|
|
|
return "unknown"
|
2018-11-21 13:47:46 +08:00
|
|
|
}
|
|
|
|
}
|