Skip to content

Commit b4d5f4a

Browse files
CatherineZhuangczhuang
andauthored
add access log (#19)
Co-authored-by: czhuang <[email protected]>
1 parent 8961813 commit b4d5f4a

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ require (
1515
github.com/imdario/mergo v0.3.7 // indirect
1616
github.com/json-iterator/go v1.1.7 // indirect
1717
github.com/kr/pretty v0.2.0 // indirect
18+
github.com/mash/go-accesslog v1.2.0
1819
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826
1920
github.com/pkg/errors v0.8.1
2021
github.com/sirupsen/logrus v1.4.2

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfn
6969
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
7070
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
7171
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
72+
github.com/mash/go-accesslog v1.2.0 h1:NRbA2PqSLjY8UUZAWAzuCVjidKBISzdc5IjJ0gdJe8g=
73+
github.com/mash/go-accesslog v1.2.0/go.mod h1:DAbGQzio0KX16krP/3uouoTPxGbzcPjFAb948zazOgg=
7274
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
7375
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
7476
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=

pkg/log/log.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@ package log
1717

1818
import (
1919
"io"
20+
"net/http"
2021
"os"
2122
"path/filepath"
2223

24+
"github.com/mash/go-accesslog"
2325
"github.com/sirupsen/logrus"
2426
"gopkg.in/natefinch/lumberjack.v2"
2527
)
@@ -70,6 +72,22 @@ func InitLogger(logFile, level string) {
7072
log = newLogger(logFile, level)
7173
}
7274

75+
type AccessLogger struct {
76+
access *logrus.Logger
77+
}
78+
79+
func (l *AccessLogger) Log(record accesslog.LogRecord) {
80+
l.access.Printf("%s %s %d %v %v", record.Method, record.Uri, record.Status, record.ElapsedTime, record.CustomRecords)
81+
}
82+
83+
// InitAccessLogger returns a handler that wraps the supplied delegate with access logging.
84+
func InitAccessLogger(h http.Handler, logFile, level string) http.Handler {
85+
l := &AccessLogger{
86+
access: newLogger(logFile, level),
87+
}
88+
return accesslog.NewLoggingHandler(h, l)
89+
}
90+
7391
// Debugf - Debugf function
7492
func Debugf(format string, args ...interface{}) {
7593
log.Debugf(format, args...)

0 commit comments

Comments
 (0)