Chore: improve code architecture
This commit is contained in:
76
log/level.go
Normal file
76
log/level.go
Normal file
@ -0,0 +1,76 @@
|
||||
package log
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
|
||||
yaml "gopkg.in/yaml.v2"
|
||||
)
|
||||
|
||||
var (
|
||||
// LogLevelMapping is a mapping for LogLevel enum
|
||||
LogLevelMapping = map[string]LogLevel{
|
||||
"error": ERROR,
|
||||
"warning": WARNING,
|
||||
"info": INFO,
|
||||
"debug": DEBUG,
|
||||
}
|
||||
)
|
||||
|
||||
const (
|
||||
DEBUG LogLevel = iota
|
||||
INFO
|
||||
WARNING
|
||||
ERROR
|
||||
)
|
||||
|
||||
type LogLevel int
|
||||
|
||||
// UnmarshalYAML unserialize Mode with yaml
|
||||
func (l *LogLevel) UnmarshalYAML(data []byte) error {
|
||||
var tp string
|
||||
yaml.Unmarshal(data, &tp)
|
||||
level, exist := LogLevelMapping[tp]
|
||||
if !exist {
|
||||
return errors.New("invalid mode")
|
||||
}
|
||||
*l = level
|
||||
return nil
|
||||
}
|
||||
|
||||
// MarshalYAML serialize Mode with yaml
|
||||
func (l LogLevel) MarshalYAML() ([]byte, error) {
|
||||
return yaml.Marshal(l.String())
|
||||
}
|
||||
|
||||
// UnmarshalJSON unserialize Mode with json
|
||||
func (l *LogLevel) UnmarshalJSON(data []byte) error {
|
||||
var tp string
|
||||
json.Unmarshal(data, tp)
|
||||
level, exist := LogLevelMapping[tp]
|
||||
if !exist {
|
||||
return errors.New("invalid mode")
|
||||
}
|
||||
*l = level
|
||||
return nil
|
||||
}
|
||||
|
||||
// MarshalJSON serialize Mode with json
|
||||
func (l LogLevel) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(l.String())
|
||||
}
|
||||
|
||||
func (l LogLevel) String() string {
|
||||
switch l {
|
||||
case INFO:
|
||||
return "info"
|
||||
case WARNING:
|
||||
return "warning"
|
||||
case ERROR:
|
||||
return "error"
|
||||
case DEBUG:
|
||||
return "debug"
|
||||
default:
|
||||
return "unknow"
|
||||
}
|
||||
}
|
85
log/log.go
Normal file
85
log/log.go
Normal file
@ -0,0 +1,85 @@
|
||||
package log
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/Dreamacro/clash/common/observable"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
var (
|
||||
logCh = make(chan interface{})
|
||||
source = observable.NewObservable(logCh)
|
||||
level = INFO
|
||||
)
|
||||
|
||||
type Event struct {
|
||||
LogLevel LogLevel
|
||||
Payload string
|
||||
}
|
||||
|
||||
func (e *Event) Type() string {
|
||||
return e.LogLevel.String()
|
||||
}
|
||||
|
||||
func Infoln(format string, v ...interface{}) {
|
||||
event := newLog(INFO, format, v...)
|
||||
logCh <- event
|
||||
print(event)
|
||||
}
|
||||
|
||||
func Warnln(format string, v ...interface{}) {
|
||||
event := newLog(WARNING, format, v...)
|
||||
logCh <- event
|
||||
print(event)
|
||||
}
|
||||
|
||||
func Errorln(format string, v ...interface{}) {
|
||||
event := newLog(ERROR, format, v...)
|
||||
logCh <- event
|
||||
print(event)
|
||||
}
|
||||
|
||||
func Debugln(format string, v ...interface{}) {
|
||||
event := newLog(DEBUG, format, v...)
|
||||
logCh <- event
|
||||
print(event)
|
||||
}
|
||||
|
||||
func Subscribe() observable.Subscription {
|
||||
sub, _ := source.Subscribe()
|
||||
return sub
|
||||
}
|
||||
|
||||
func Level() LogLevel {
|
||||
return level
|
||||
}
|
||||
|
||||
func SetLevel(newLevel LogLevel) {
|
||||
level = newLevel
|
||||
}
|
||||
|
||||
func print(data *Event) {
|
||||
if data.LogLevel < level {
|
||||
return
|
||||
}
|
||||
|
||||
switch data.LogLevel {
|
||||
case INFO:
|
||||
log.Infoln(data.Payload)
|
||||
case WARNING:
|
||||
log.Warnln(data.Payload)
|
||||
case ERROR:
|
||||
log.Errorln(data.Payload)
|
||||
case DEBUG:
|
||||
log.Debugln(data.Payload)
|
||||
}
|
||||
}
|
||||
|
||||
func newLog(logLevel LogLevel, format string, v ...interface{}) *Event {
|
||||
return &Event{
|
||||
LogLevel: logLevel,
|
||||
Payload: fmt.Sprintf(format, v...),
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user