Improve: add log level

This commit is contained in:
Dreamacro 2018-06-22 13:15:20 +08:00
parent 9c2ace1f91
commit 018a6ba041
3 changed files with 46 additions and 16 deletions

View File

@ -21,11 +21,6 @@ type Traffic struct {
Down int64 `json:"down"` Down int64 `json:"down"`
} }
type Log struct {
Type string `json:"type"`
Payload string `json:"payload"`
}
type Error struct { type Error struct {
Error string `json:"error"` Error string `json:"error"`
} }
@ -60,7 +55,38 @@ func traffic(w http.ResponseWriter, r *http.Request) {
} }
} }
type GetLogs struct {
Level string `json:"level"`
}
type Log struct {
Type string `json:"type"`
Payload string `json:"payload"`
}
func getLogs(w http.ResponseWriter, r *http.Request) { func getLogs(w http.ResponseWriter, r *http.Request) {
req := &GetLogs{}
render.DecodeJSON(r.Body, req)
if req.Level == "" {
req.Level = "info"
}
mapping := map[string]tunnel.LogLevel{
"info": tunnel.INFO,
"debug": tunnel.DEBUG,
"error": tunnel.ERROR,
"warning": tunnel.WARNING,
}
level, ok := mapping[req.Level]
if !ok {
w.WriteHeader(http.StatusBadRequest)
render.JSON(w, r, Error{
Error: "Level error",
})
return
}
src := tun.Log() src := tun.Log()
sub, err := src.Subscribe() sub, err := src.Subscribe()
defer src.UnSubscribe(sub) defer src.UnSubscribe(sub)
@ -74,6 +100,10 @@ func getLogs(w http.ResponseWriter, r *http.Request) {
render.Status(r, http.StatusOK) render.Status(r, http.StatusOK)
for elm := range sub { for elm := range sub {
log := elm.(tunnel.Log) log := elm.(tunnel.Log)
if log.LogLevel > level {
continue
}
if err := json.NewEncoder(w).Encode(Log{ if err := json.NewEncoder(w).Encode(Log{
Type: log.Type(), Type: log.Type(),
Payload: log.Payload, Payload: log.Payload,

View File

@ -7,21 +7,21 @@ import (
) )
const ( const (
INFO LogType = iota ERROR LogLevel = iota
WARNING WARNING
ERROR INFO
DEBUG DEBUG
) )
type LogType int type LogLevel int
type Log struct { type Log struct {
LogType LogType LogLevel LogLevel
Payload string Payload string
} }
func (l *Log) Type() string { func (l *Log) Type() string {
switch l.LogType { switch l.LogLevel {
case INFO: case INFO:
return "Info" return "Info"
case WARNING: case WARNING:
@ -36,7 +36,7 @@ func (l *Log) Type() string {
} }
func print(data Log) { func print(data Log) {
switch data.LogType { switch data.LogLevel {
case INFO: case INFO:
log.Infoln(data.Payload) log.Infoln(data.Payload)
case WARNING: case WARNING:
@ -59,9 +59,9 @@ func (t *Tunnel) subscribeLogs() {
} }
} }
func newLog(logType LogType, format string, v ...interface{}) Log { func newLog(logLevel LogLevel, format string, v ...interface{}) Log {
return Log{ return Log{
LogType: logType, LogLevel: logLevel,
Payload: fmt.Sprintf(format, v...), Payload: fmt.Sprintf(format, v...),
} }
} }

View File

@ -184,7 +184,7 @@ func (t *Tunnel) match(addr *C.Addr) C.Proxy {
if !ok { if !ok {
continue continue
} }
t.logCh <- newLog(INFO, "%v match %d using %s", addr.String(), rule.RuleType(), rule.Adapter()) t.logCh <- newLog(INFO, "%v match %s using %s", addr.String(), rule.RuleType().String(), rule.Adapter())
return a return a
} }
} }