2014-06-11 12:48:11 +04:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2014-09-24 05:46:36 +04:00
|
|
|
"github.com/siddontang/go/log"
|
2014-06-11 12:48:11 +04:00
|
|
|
)
|
|
|
|
|
|
|
|
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 {
|
2015-10-06 12:20:32 +03:00
|
|
|
l.l.Infof(format, remoteAddr, request, usedTime, "OK")
|
2014-06-11 12:48:11 +04:00
|
|
|
} else {
|
2015-10-06 12:20:32 +03:00
|
|
|
l.l.Infof(format, remoteAddr, request, usedTime, err.Error())
|
2014-06-11 12:48:11 +04:00
|
|
|
}
|
|
|
|
}
|