add access log

This commit is contained in:
siddontang 2014-06-11 16:48:11 +08:00
parent 00b5fb4b24
commit 655db07604
5 changed files with 85 additions and 2 deletions

View File

@ -8,5 +8,7 @@
"write_buffer_size": 67108864, "write_buffer_size": 67108864,
"cache_size": 524288000 "cache_size": 524288000
} }
} },
"access_log" : "access.log"
} }

51
server/accesslog.go Normal file
View File

@ -0,0 +1,51 @@
package server
import (
"fmt"
"github.com/siddontang/go-log/log"
"strings"
)
const (
accessTimeFormat = "2006/01/02 15:04:05"
)
type accessLog struct {
l *log.Logger
}
func newAcessLog(baseName string) (*accessLog, error) {
l := new(accessLog)
h, err := log.NewTimeRotatingFileHandler(baseName, log.WhenDay, 1)
if err != nil {
return nil, err
}
l.l = log.New(h, log.Ltime)
return l, nil
}
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]
}
argsStr := fmt.Sprintf(strings.Join(argsFormat, ""), argsI...)
format := `%s [%s%s] %d [%s]`
if err == nil {
l.l.Info(format, remoteAddr, cmd, argsStr, usedTime, "OK")
} else {
l.l.Info(format, remoteAddr, cmd, argsStr, usedTime, err.Error())
}
}

View File

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"github.com/siddontang/ledisdb/ledis" "github.com/siddontang/ledisdb/ledis"
"net" "net"
"path"
"strings" "strings"
) )
@ -18,6 +19,8 @@ type App struct {
quit chan struct{} quit chan struct{}
access *accessLog
//for slave replication //for slave replication
m *master m *master
} }
@ -51,6 +54,18 @@ func NewApp(cfg *Config) (*App, error) {
return nil, err return nil, err
} }
if len(cfg.AccessLog) > 0 {
if path.Dir(cfg.AccessLog) == "." {
app.access, err = newAcessLog(path.Join(cfg.DataDir, cfg.AccessLog))
} else {
app.access, err = newAcessLog(cfg.AccessLog)
}
if err != nil {
return nil, err
}
}
if app.ldb, err = ledis.OpenWithConfig(&cfg.DB); err != nil { if app.ldb, err = ledis.OpenWithConfig(&cfg.DB); err != nil {
return nil, err return nil, err
} }
@ -73,6 +88,10 @@ func (app *App) Close() {
app.m.Close() app.m.Close()
if app.access != nil {
app.access.Close()
}
app.ldb.Close() app.ldb.Close()
} }
@ -91,6 +110,6 @@ func (app *App) Run() {
} }
} }
func (app *App) Ledis() *ledis.DB { func (app *App) Ledis() *ledis.Ledis {
return app.ldb return app.ldb
} }

View File

@ -11,6 +11,7 @@ import (
"runtime" "runtime"
"strconv" "strconv"
"strings" "strings"
"time"
) )
var errReadRequest = errors.New("invalid request protocol") var errReadRequest = errors.New("invalid request protocol")
@ -135,6 +136,8 @@ func (c *client) readRequest() ([][]byte, error) {
func (c *client) handleRequest(req [][]byte) { func (c *client) handleRequest(req [][]byte) {
var err error var err error
start := time.Now()
if len(req) == 0 { if len(req) == 0 {
err = ErrEmptyCommand err = ErrEmptyCommand
} else { } else {
@ -152,6 +155,12 @@ 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)
}
if err != nil { if err != nil {
c.writeError(err) c.writeError(err)
} }

View File

@ -17,6 +17,8 @@ type Config struct {
//set slaveof to enable replication from master //set slaveof to enable replication from master
//empty, no replication //empty, no replication
SlaveOf string `json:"slaveof"` SlaveOf string `json:"slaveof"`
AccessLog string `json:"access_log"`
} }
func NewConfig(data json.RawMessage) (*Config, error) { func NewConfig(data json.RawMessage) (*Config, error) {