2014-06-11 12:48:11 +04:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/siddontang/go-log/log"
|
|
|
|
)
|
|
|
|
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2014-06-12 10:10:29 +04:00
|
|
|
func (l *accessLog) Log(remoteAddr string, usedTime int64, request []byte, err error) {
|
2014-06-11 12:48:11 +04:00
|
|
|
|
2014-06-12 10:10:29 +04:00
|
|
|
format := `%s %q %d [%s]`
|
2014-06-11 12:48:11 +04:00
|
|
|
|
|
|
|
if err == nil {
|
2014-06-12 10:10:29 +04:00
|
|
|
l.l.Info(format, remoteAddr, request, usedTime, "OK")
|
2014-06-11 12:48:11 +04:00
|
|
|
} else {
|
2014-06-12 10:10:29 +04:00
|
|
|
l.l.Info(format, remoteAddr, request, usedTime, err.Error())
|
2014-06-11 12:48:11 +04:00
|
|
|
}
|
|
|
|
}
|