2014-07-27 00:37:18 +02:00
|
|
|
// Copyright 2014 The Gogs Authors. All rights reserved.
|
2019-04-02 09:48:31 +02:00
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2014-07-27 00:37:18 +02:00
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package log
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
)
|
|
|
|
|
2019-04-02 09:48:31 +02:00
|
|
|
type connWriter struct {
|
2014-07-27 00:37:18 +02:00
|
|
|
innerWriter io.WriteCloser
|
|
|
|
ReconnectOnMsg bool `json:"reconnectOnMsg"`
|
|
|
|
Reconnect bool `json:"reconnect"`
|
|
|
|
Net string `json:"net"`
|
|
|
|
Addr string `json:"addr"`
|
|
|
|
}
|
|
|
|
|
2019-04-02 09:48:31 +02:00
|
|
|
// Close the inner writer
|
|
|
|
func (i *connWriter) Close() error {
|
|
|
|
if i.innerWriter != nil {
|
|
|
|
return i.innerWriter.Close()
|
|
|
|
}
|
|
|
|
return nil
|
2014-07-27 00:37:18 +02:00
|
|
|
}
|
|
|
|
|
2019-04-02 09:48:31 +02:00
|
|
|
// 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
|
2014-07-27 00:37:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-02 09:48:31 +02:00
|
|
|
if i.ReconnectOnMsg {
|
|
|
|
defer i.innerWriter.Close()
|
2014-07-27 00:37:18 +02:00
|
|
|
}
|
|
|
|
|
2019-04-02 09:48:31 +02:00
|
|
|
return i.innerWriter.Write(p)
|
2014-07-27 00:37:18 +02:00
|
|
|
}
|
|
|
|
|
2019-04-02 09:48:31 +02:00
|
|
|
func (i *connWriter) neededConnectOnMsg() bool {
|
|
|
|
if i.Reconnect {
|
|
|
|
i.Reconnect = false
|
|
|
|
return true
|
2014-07-27 00:37:18 +02:00
|
|
|
}
|
2019-04-02 09:48:31 +02:00
|
|
|
|
|
|
|
if i.innerWriter == nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return i.ReconnectOnMsg
|
2014-07-27 00:37:18 +02:00
|
|
|
}
|
|
|
|
|
2019-04-02 09:48:31 +02:00
|
|
|
func (i *connWriter) connect() error {
|
|
|
|
if i.innerWriter != nil {
|
|
|
|
i.innerWriter.Close()
|
|
|
|
i.innerWriter = nil
|
2014-07-27 00:37:18 +02:00
|
|
|
}
|
|
|
|
|
2019-04-02 09:48:31 +02:00
|
|
|
conn, err := net.Dial(i.Net, i.Addr)
|
2014-07-27 00:37:18 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if tcpConn, ok := conn.(*net.TCPConn); ok {
|
|
|
|
tcpConn.SetKeepAlive(true)
|
|
|
|
}
|
|
|
|
|
2019-04-02 09:48:31 +02:00
|
|
|
i.innerWriter = conn
|
2014-07-27 00:37:18 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-04-02 09:48:31 +02:00
|
|
|
// 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"`
|
|
|
|
}
|
2014-07-27 00:37:18 +02:00
|
|
|
|
2019-04-02 09:48:31 +02:00
|
|
|
// 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
|
2014-07-27 00:37:18 +02:00
|
|
|
}
|
2019-04-02 09:48:31 +02:00
|
|
|
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() {
|
|
|
|
}
|
2014-07-27 00:37:18 +02:00
|
|
|
|
2019-04-02 09:48:31 +02:00
|
|
|
// GetName returns the default name for this implementation
|
|
|
|
func (log *ConnLogger) GetName() string {
|
|
|
|
return "conn"
|
2014-07-27 00:37:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
Register("conn", NewConn)
|
|
|
|
}
|