-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlogger.go
76 lines (67 loc) · 2 KB
/
logger.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package cs
import (
"encoding/json"
"fmt"
)
type printLogger interface {
Debug(...interface{})
}
type consoleLogger struct{}
func (consoleLogger) Debug(a ...interface{}) {
fmt.Println(a...)
}
const loggerCtxKey = "__logger_middleware_instence__"
const loggerNameCtxKey = "__logger_middleware_name__"
func getLogDataString(v interface{}) string {
data := ""
switch v.(type) {
case nil:
data = "nil"
case string:
data = v.(string)
case []byte:
data = string(v.([]byte))
default:
bt, _ := json.Marshal(v)
data = string(bt)
}
return data
}
// AccessLogger 打印请求响应中间件
// 2 个可选参数,如果参数是 printLogger 接口类型则用于设置打印日志的 Logger 实例,如果是 string 类型则用于设置日志前缀
// AccessLogger("MySRV") 设置名称
// AccessLogger("MySRV", logger) 设置名称和打日志的实例
// AccessLogger(logger, "MySRV") 设置名称和打日志的实例
// AccessLogger(logger) 设置打日志的实例
// AccessLogger(123) 无效参数,不会产生异常,等价于没有参数
func (s *Srv) AccessLogger(args ...interface{}) HandlerFunc {
var logger printLogger = consoleLogger{}
name := "SRV"
for _, v := range args {
if n, ok := v.(string); ok {
name = n
} else if l, ok := v.(printLogger); ok {
logger = l
}
}
s.UsePush(func(c *Context) error {
logger.Debug(fmt.Sprintf("%s PUSH SID=%s CMD=%s %s", name, c.SID, c.Cmd, getLogDataString(c.Data)))
return nil
})
return func(c *Context) {
if c.Get(loggerCtxKey) == nil {
c.Set(loggerCtxKey, logger)
c.Set(loggerNameCtxKey, name)
}
if (c.Cmd == CmdConnected ||
c.Cmd == CmdClosed ||
c.Cmd == CmdHeartbeat) &&
(len(c.handlers) == len(c.Srv.middleware)) { // 有路由监听的话就要打印
c.Next()
return
}
logger.Debug(fmt.Sprintf("%s RECV SID=%s CMD=%s SEQ=%s %s", name, c.SID, c.Cmd, c.Seqno, string(c.RawData)))
c.Next()
logger.Debug(fmt.Sprintf("%s RESP SID=%s CMD=%s SEQ=%s %s", name, c.SID, c.Cmd, c.Seqno, getLogDataString(c.Data)))
}
}