optimize access log

This commit is contained in:
siddontang 2014-06-12 14:10:29 +08:00
parent 52c55988de
commit 8592324a2f
2 changed files with 22 additions and 15 deletions

View File

@ -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())
}
}

View File

@ -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 {