mirror of
https://github.com/astaxie/beego.git
synced 2024-11-26 07:01:28 +00:00
Update conn.go
使用json直接初始化相关变量,代码更简单。如果把协议的首字母改大写会更简单,但不好改谢大的协议。
This commit is contained in:
parent
0269a66940
commit
9076ce7d17
42
logs/conn.go
42
logs/conn.go
@ -10,45 +10,29 @@ import (
|
|||||||
type ConnWriter struct {
|
type ConnWriter struct {
|
||||||
lg *log.Logger
|
lg *log.Logger
|
||||||
innerWriter io.WriteCloser
|
innerWriter io.WriteCloser
|
||||||
reconnectOnMsg bool
|
ReconnectOnMsg bool `json:"reconnectOnMsg"`
|
||||||
reconnect bool
|
Reconnect bool `json:"reconnect"`
|
||||||
net string
|
Net string `json:"net"`
|
||||||
addr string
|
Addr string `json:"addr"`
|
||||||
level int
|
Level int `json:"level"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewConn() LoggerInterface {
|
func NewConn() LoggerInterface {
|
||||||
conn := new(ConnWriter)
|
conn := new(ConnWriter)
|
||||||
conn.level = LevelTrace
|
conn.Level = LevelTrace
|
||||||
return conn
|
return conn
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ConnWriter) Init(jsonconfig string) error {
|
func (c *ConnWriter) Init(jsonconfig string) error {
|
||||||
var m map[string]interface{}
|
err := json.Unmarshal([]byte(jsonconfig), c)
|
||||||
err := json.Unmarshal([]byte(jsonconfig), &m)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if rom, ok := m["reconnectOnMsg"]; ok {
|
|
||||||
c.reconnectOnMsg = rom.(bool)
|
|
||||||
}
|
|
||||||
if rc, ok := m["reconnect"]; ok {
|
|
||||||
c.reconnect = rc.(bool)
|
|
||||||
}
|
|
||||||
if nt, ok := m["net"]; ok {
|
|
||||||
c.net = nt.(string)
|
|
||||||
}
|
|
||||||
if addr, ok := m["addr"]; ok {
|
|
||||||
c.addr = addr.(string)
|
|
||||||
}
|
|
||||||
if lv, ok := m["level"]; ok {
|
|
||||||
c.level = int(lv.(float64))
|
|
||||||
}
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ConnWriter) WriteMsg(msg string, level int) error {
|
func (c *ConnWriter) WriteMsg(msg string, level int) error {
|
||||||
if level < c.level {
|
if level < c.Level {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
if c.neddedConnectOnMsg() {
|
if c.neddedConnectOnMsg() {
|
||||||
@ -58,7 +42,7 @@ func (c *ConnWriter) WriteMsg(msg string, level int) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if c.reconnectOnMsg {
|
if c.ReconnectOnMsg {
|
||||||
defer c.innerWriter.Close()
|
defer c.innerWriter.Close()
|
||||||
}
|
}
|
||||||
c.lg.Println(msg)
|
c.lg.Println(msg)
|
||||||
@ -82,7 +66,7 @@ func (c *ConnWriter) connect() error {
|
|||||||
c.innerWriter = nil
|
c.innerWriter = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
conn, err := net.Dial(c.net, c.addr)
|
conn, err := net.Dial(c.Net, c.Addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -97,8 +81,8 @@ func (c *ConnWriter) connect() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *ConnWriter) neddedConnectOnMsg() bool {
|
func (c *ConnWriter) neddedConnectOnMsg() bool {
|
||||||
if c.reconnect {
|
if c.Reconnect {
|
||||||
c.reconnect = false
|
c.Reconnect = false
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -106,7 +90,7 @@ func (c *ConnWriter) neddedConnectOnMsg() bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
return c.reconnectOnMsg
|
return c.ReconnectOnMsg
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
Loading…
Reference in New Issue
Block a user