From 018a6ba0417112504fcb3225e714d59bbc19b099 Mon Sep 17 00:00:00 2001 From: Dreamacro <305009791@qq.com> Date: Fri, 22 Jun 2018 13:15:20 +0800 Subject: [PATCH] Improve: add log level --- hub/server.go | 40 +++++++++++++++++++++++++++++++++++----- tunnel/log.go | 20 ++++++++++---------- tunnel/tunnel.go | 2 +- 3 files changed, 46 insertions(+), 16 deletions(-) diff --git a/hub/server.go b/hub/server.go index cea853b8..5c9f4038 100644 --- a/hub/server.go +++ b/hub/server.go @@ -21,11 +21,6 @@ type Traffic struct { Down int64 `json:"down"` } -type Log struct { - Type string `json:"type"` - Payload string `json:"payload"` -} - type Error struct { 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) { + 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() sub, err := src.Subscribe() defer src.UnSubscribe(sub) @@ -74,6 +100,10 @@ func getLogs(w http.ResponseWriter, r *http.Request) { render.Status(r, http.StatusOK) for elm := range sub { log := elm.(tunnel.Log) + if log.LogLevel > level { + continue + } + if err := json.NewEncoder(w).Encode(Log{ Type: log.Type(), Payload: log.Payload, diff --git a/tunnel/log.go b/tunnel/log.go index a4fb6840..2021a354 100644 --- a/tunnel/log.go +++ b/tunnel/log.go @@ -7,21 +7,21 @@ import ( ) const ( - INFO LogType = iota + ERROR LogLevel = iota WARNING - ERROR + INFO DEBUG ) -type LogType int +type LogLevel int type Log struct { - LogType LogType - Payload string + LogLevel LogLevel + Payload string } func (l *Log) Type() string { - switch l.LogType { + switch l.LogLevel { case INFO: return "Info" case WARNING: @@ -36,7 +36,7 @@ func (l *Log) Type() string { } func print(data Log) { - switch data.LogType { + switch data.LogLevel { case INFO: log.Infoln(data.Payload) 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{ - LogType: logType, - Payload: fmt.Sprintf(format, v...), + LogLevel: logLevel, + Payload: fmt.Sprintf(format, v...), } } diff --git a/tunnel/tunnel.go b/tunnel/tunnel.go index e02a9b5d..d1d581ab 100644 --- a/tunnel/tunnel.go +++ b/tunnel/tunnel.go @@ -184,7 +184,7 @@ func (t *Tunnel) match(addr *C.Addr) C.Proxy { if !ok { 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 } }