diff options
Diffstat (limited to 'modules/log/conn.go')
-rw-r--r-- | modules/log/conn.go | 122 |
1 files changed, 69 insertions, 53 deletions
diff --git a/modules/log/conn.go b/modules/log/conn.go index edb149dfa5..d48bb4b334 100644 --- a/modules/log/conn.go +++ b/modules/log/conn.go @@ -1,4 +1,5 @@ // Copyright 2014 The Gogs Authors. All rights reserved. +// Copyright 2019 The Gitea Authors. All rights reserved. // Use of this source code is governed by a MIT-style // license that can be found in the LICENSE file. @@ -7,73 +8,60 @@ package log import ( "encoding/json" "io" - "log" "net" ) -// ConnWriter implements LoggerInterface. -// it writes messages in keep-live tcp connection. -type ConnWriter struct { - lg *log.Logger +type connWriter struct { innerWriter io.WriteCloser ReconnectOnMsg bool `json:"reconnectOnMsg"` Reconnect bool `json:"reconnect"` Net string `json:"net"` Addr string `json:"addr"` - Level int `json:"level"` -} - -// NewConn creates new ConnWrite returning as LoggerInterface. -func NewConn() LoggerInterface { - conn := new(ConnWriter) - conn.Level = TRACE - return conn } -// Init inits connection writer with json config. -// json config only need key "level". -func (cw *ConnWriter) Init(jsonconfig string) error { - return json.Unmarshal([]byte(jsonconfig), cw) +// Close the inner writer +func (i *connWriter) Close() error { + if i.innerWriter != nil { + return i.innerWriter.Close() + } + return nil } -// WriteMsg writes message in connection. -// if connection is down, try to re-connect. -func (cw *ConnWriter) WriteMsg(msg string, skip, level int) error { - if cw.Level > level { - return nil - } - if cw.neededConnectOnMsg() { - if err := cw.connect(); err != nil { - return err +// Write the data to the connection +func (i *connWriter) Write(p []byte) (int, error) { + if i.neededConnectOnMsg() { + if err := i.connect(); err != nil { + return 0, err } } - if cw.ReconnectOnMsg { - defer cw.innerWriter.Close() + if i.ReconnectOnMsg { + defer i.innerWriter.Close() } - cw.lg.Println(msg) - return nil -} -// Flush no things for this implementation -func (cw *ConnWriter) Flush() { + return i.innerWriter.Write(p) } -// Destroy destroy connection writer and close tcp listener. -func (cw *ConnWriter) Destroy() { - if cw.innerWriter == nil { - return +func (i *connWriter) neededConnectOnMsg() bool { + if i.Reconnect { + i.Reconnect = false + return true } - cw.innerWriter.Close() + + if i.innerWriter == nil { + return true + } + + return i.ReconnectOnMsg } -func (cw *ConnWriter) connect() error { - if cw.innerWriter != nil { - cw.innerWriter.Close() - cw.innerWriter = nil +func (i *connWriter) connect() error { + if i.innerWriter != nil { + i.innerWriter.Close() + i.innerWriter = nil } - conn, err := net.Dial(cw.Net, cw.Addr) + conn, err := net.Dial(i.Net, i.Addr) if err != nil { return err } @@ -82,22 +70,50 @@ func (cw *ConnWriter) connect() error { tcpConn.SetKeepAlive(true) } - cw.innerWriter = conn - cw.lg = log.New(conn, "", log.Ldate|log.Ltime) + i.innerWriter = conn return nil } -func (cw *ConnWriter) neededConnectOnMsg() bool { - if cw.Reconnect { - cw.Reconnect = false - return true - } +// ConnLogger implements LoggerProvider. +// it writes messages in keep-live tcp connection. +type ConnLogger struct { + BaseLogger + ReconnectOnMsg bool `json:"reconnectOnMsg"` + Reconnect bool `json:"reconnect"` + Net string `json:"net"` + Addr string `json:"addr"` +} - if cw.innerWriter == nil { - return true +// NewConn creates new ConnLogger returning as LoggerProvider. +func NewConn() LoggerProvider { + conn := new(ConnLogger) + conn.Level = TRACE + return conn +} + +// Init inits connection writer with json config. +// json config only need key "level". +func (log *ConnLogger) Init(jsonconfig string) error { + err := json.Unmarshal([]byte(jsonconfig), log) + if err != nil { + return err } + log.createLogger(&connWriter{ + ReconnectOnMsg: log.ReconnectOnMsg, + Reconnect: log.Reconnect, + Net: log.Net, + Addr: log.Addr, + }, log.Level) + return nil +} + +// Flush does nothing for this implementation +func (log *ConnLogger) Flush() { +} - return cw.ReconnectOnMsg +// GetName returns the default name for this implementation +func (log *ConnLogger) GetName() string { + return "conn" } func init() { |