From 8592324a2fe722c6b748aad0e8b43795fa42b6a8 Mon Sep 17 00:00:00 2001 From: siddontang Date: Thu, 12 Jun 2014 14:10:29 +0800 Subject: [PATCH] optimize access log --- server/accesslog.go | 18 ++++-------------- server/client.go | 19 ++++++++++++++++++- 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/server/accesslog.go b/server/accesslog.go index 58b34b7..9e517a8 100644 --- a/server/accesslog.go +++ b/server/accesslog.go @@ -1,9 +1,7 @@ package server import ( - "fmt" "github.com/siddontang/go-log/log" - "strings" ) const ( @@ -31,21 +29,13 @@ func (l *accessLog) Close() { l.l.Close() } -func (l *accessLog) Log(remoteAddr string, usedTime int64, cmd string, args [][]byte, err error) { - argsFormat := make([]string, len(args)) - argsI := make([]interface{}, len(args)) - for i := range args { - argsFormat[i] = " %.24q" - argsI[i] = args[i] - } +func (l *accessLog) Log(remoteAddr string, usedTime int64, request []byte, err error) { - argsStr := fmt.Sprintf(strings.Join(argsFormat, ""), argsI...) - - format := `%s [%s%s] %d [%s]` + format := `%s %q %d [%s]` if err == nil { - l.l.Info(format, remoteAddr, cmd, argsStr, usedTime, "OK") + l.l.Info(format, remoteAddr, request, usedTime, "OK") } else { - l.l.Info(format, remoteAddr, cmd, argsStr, usedTime, err.Error()) + l.l.Info(format, remoteAddr, request, usedTime, err.Error()) } } diff --git a/server/client.go b/server/client.go index ca3d72d..3f5c43c 100644 --- a/server/client.go +++ b/server/client.go @@ -32,6 +32,8 @@ type client struct { reqC chan error syncBuf bytes.Buffer + + logBuf bytes.Buffer } func newClient(c net.Conn, app *App) { @@ -158,7 +160,22 @@ func (c *client) handleRequest(req [][]byte) { duration := time.Since(start) if c.app.access != nil { - c.app.access.Log(c.c.RemoteAddr().String(), duration.Nanoseconds()/1000000, c.cmd, c.args, err) + c.logBuf.Reset() + for i, r := range req { + left := 256 - c.logBuf.Len() + if left <= 0 { + break + } else if len(r) <= left { + c.logBuf.Write(r) + if i != len(req)-1 { + c.logBuf.WriteByte(' ') + } + } else { + c.logBuf.Write(r[0:left]) + } + } + + c.app.access.Log(c.c.RemoteAddr().String(), duration.Nanoseconds()/1000000, c.logBuf.Bytes(), err) } if err != nil {