update package beego/logs

This commit is contained in:
fatedier
2016-08-11 16:32:05 +08:00
parent 11fe4b1d8b
commit 740691b080
8 changed files with 573 additions and 305 deletions

View File

@@ -17,14 +17,14 @@ package logs
import (
"encoding/json"
"io"
"log"
"net"
"time"
)
// ConnWriter implements LoggerInterface.
// connWriter implements LoggerInterface.
// it writes messages in keep-live tcp connection.
type ConnWriter struct {
lg *log.Logger
type connWriter struct {
lg *logWriter
innerWriter io.WriteCloser
ReconnectOnMsg bool `json:"reconnectOnMsg"`
Reconnect bool `json:"reconnect"`
@@ -33,26 +33,26 @@ type ConnWriter struct {
Level int `json:"level"`
}
// create new ConnWrite returning as LoggerInterface.
func NewConn() LoggerInterface {
conn := new(ConnWriter)
// NewConn create new ConnWrite returning as LoggerInterface.
func NewConn() Logger {
conn := new(connWriter)
conn.Level = LevelTrace
return conn
}
// init connection writer with json config.
// Init init connection writer with json config.
// json config only need key "level".
func (c *ConnWriter) Init(jsonconfig string) error {
return json.Unmarshal([]byte(jsonconfig), c)
func (c *connWriter) Init(jsonConfig string) error {
return json.Unmarshal([]byte(jsonConfig), c)
}
// write message in connection.
// WriteMsg write message in connection.
// if connection is down, try to re-connect.
func (c *ConnWriter) WriteMsg(msg string, level int) error {
func (c *connWriter) WriteMsg(when time.Time, msg string, level int) error {
if level > c.Level {
return nil
}
if c.neddedConnectOnMsg() {
if c.needToConnectOnMsg() {
err := c.connect()
if err != nil {
return err
@@ -62,23 +62,24 @@ func (c *ConnWriter) WriteMsg(msg string, level int) error {
if c.ReconnectOnMsg {
defer c.innerWriter.Close()
}
c.lg.Println(msg)
c.lg.println(when, msg)
return nil
}
// implementing method. empty.
func (c *ConnWriter) Flush() {
// Flush implementing method. empty.
func (c *connWriter) Flush() {
}
// destroy connection writer and close tcp listener.
func (c *ConnWriter) Destroy() {
// Destroy destroy connection writer and close tcp listener.
func (c *connWriter) Destroy() {
if c.innerWriter != nil {
c.innerWriter.Close()
}
}
func (c *ConnWriter) connect() error {
func (c *connWriter) connect() error {
if c.innerWriter != nil {
c.innerWriter.Close()
c.innerWriter = nil
@@ -94,11 +95,11 @@ func (c *ConnWriter) connect() error {
}
c.innerWriter = conn
c.lg = log.New(conn, "", log.Ldate|log.Ltime)
c.lg = newLogWriter(conn)
return nil
}
func (c *ConnWriter) neddedConnectOnMsg() bool {
func (c *connWriter) needToConnectOnMsg() bool {
if c.Reconnect {
c.Reconnect = false
return true