1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-24 23:40:55 +00:00
Beego/logs/conn.go

118 lines
2.5 KiB
Go
Raw Normal View History

2014-08-18 08:41:43 +00:00
// Copyright 2014 beego Author. All Rights Reserved.
2014-07-03 15:40:21 +00:00
//
2014-08-18 08:41:43 +00:00
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
2014-07-03 15:40:21 +00:00
//
2014-08-18 08:41:43 +00:00
// http://www.apache.org/licenses/LICENSE-2.0
2014-07-03 15:40:21 +00:00
//
2014-08-18 08:41:43 +00:00
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
2013-08-27 15:48:58 +00:00
package logs
import (
"encoding/json"
"io"
"net"
2016-01-23 08:24:58 +00:00
"time"
2013-08-27 15:48:58 +00:00
)
2015-09-11 15:08:24 +00:00
// connWriter implements LoggerInterface.
2013-12-30 15:32:57 +00:00
// it writes messages in keep-live tcp connection.
2015-09-11 15:08:24 +00:00
type connWriter struct {
2016-02-03 06:42:38 +00:00
lg *logWriter
2013-08-27 15:48:58 +00:00
innerWriter io.WriteCloser
ReconnectOnMsg bool `json:"reconnectOnMsg"`
Reconnect bool `json:"reconnect"`
Net string `json:"net"`
Addr string `json:"addr"`
Level int `json:"level"`
2013-08-27 15:48:58 +00:00
}
2015-09-11 15:08:24 +00:00
// NewConn create new ConnWrite returning as LoggerInterface.
func NewConn() Logger {
conn := new(connWriter)
conn.Level = LevelTrace
2013-08-27 15:48:58 +00:00
return conn
}
2015-09-11 15:08:24 +00:00
// Init init connection writer with json config.
2013-12-30 15:32:57 +00:00
// json config only need key "level".
2016-02-03 06:42:38 +00:00
func (c *connWriter) Init(jsonConfig string) error {
return json.Unmarshal([]byte(jsonConfig), c)
2013-08-27 15:48:58 +00:00
}
2015-09-11 15:08:24 +00:00
// WriteMsg write message in connection.
2013-12-30 15:32:57 +00:00
// if connection is down, try to re-connect.
2016-01-23 08:24:58 +00:00
func (c *connWriter) WriteMsg(when time.Time, msg string, level int) error {
if level > c.Level {
2013-08-27 15:48:58 +00:00
return nil
}
2016-02-03 06:42:38 +00:00
if c.needToConnectOnMsg() {
2013-08-27 15:48:58 +00:00
err := c.connect()
if err != nil {
return err
}
}
if c.ReconnectOnMsg {
2013-08-27 15:48:58 +00:00
defer c.innerWriter.Close()
}
2016-01-23 08:24:58 +00:00
2019-03-12 07:51:43 +00:00
c.lg.writeln(when, msg)
2013-08-27 15:48:58 +00:00
return nil
}
2015-09-11 15:08:24 +00:00
// Flush implementing method. empty.
func (c *connWriter) Flush() {
2013-11-27 09:50:10 +00:00
}
2015-09-11 15:08:24 +00:00
// Destroy destroy connection writer and close tcp listener.
func (c *connWriter) Destroy() {
2015-02-23 03:42:46 +00:00
if c.innerWriter != nil {
c.innerWriter.Close()
2013-08-27 15:48:58 +00:00
}
}
2015-09-11 15:08:24 +00:00
func (c *connWriter) connect() error {
2013-08-27 15:48:58 +00:00
if c.innerWriter != nil {
c.innerWriter.Close()
c.innerWriter = nil
}
conn, err := net.Dial(c.Net, c.Addr)
2013-08-27 15:48:58 +00:00
if err != nil {
return err
}
2013-12-07 05:26:22 +00:00
if tcpConn, ok := conn.(*net.TCPConn); ok {
2013-08-27 15:48:58 +00:00
tcpConn.SetKeepAlive(true)
}
c.innerWriter = conn
2016-02-03 06:42:38 +00:00
c.lg = newLogWriter(conn)
2013-08-27 15:48:58 +00:00
return nil
}
2016-02-03 06:42:38 +00:00
func (c *connWriter) needToConnectOnMsg() bool {
if c.Reconnect {
c.Reconnect = false
2013-08-27 15:48:58 +00:00
return true
}
if c.innerWriter == nil {
return true
}
return c.ReconnectOnMsg
2013-08-27 15:48:58 +00:00
}
func init() {
2016-03-24 09:38:26 +00:00
Register(AdapterConn, NewConn)
2013-08-27 15:48:58 +00:00
}