Skip to content

Commit

Permalink
add access log (#19)
Browse files Browse the repository at this point in the history
Co-authored-by: czhuang <[email protected]>
  • Loading branch information
CatherineZhuang and czhuang authored Dec 1, 2020
1 parent 8961813 commit b4d5f4a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ require (
github.com/imdario/mergo v0.3.7 // indirect
github.com/json-iterator/go v1.1.7 // indirect
github.com/kr/pretty v0.2.0 // indirect
github.com/mash/go-accesslog v1.2.0
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826
github.com/pkg/errors v0.8.1
github.com/sirupsen/logrus v1.4.2
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfn
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/mash/go-accesslog v1.2.0 h1:NRbA2PqSLjY8UUZAWAzuCVjidKBISzdc5IjJ0gdJe8g=
github.com/mash/go-accesslog v1.2.0/go.mod h1:DAbGQzio0KX16krP/3uouoTPxGbzcPjFAb948zazOgg=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
Expand Down
18 changes: 18 additions & 0 deletions pkg/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ package log

import (
"io"
"net/http"
"os"
"path/filepath"

"github.com/mash/go-accesslog"
"github.com/sirupsen/logrus"
"gopkg.in/natefinch/lumberjack.v2"
)
Expand Down Expand Up @@ -70,6 +72,22 @@ func InitLogger(logFile, level string) {
log = newLogger(logFile, level)
}

type AccessLogger struct {
access *logrus.Logger
}

func (l *AccessLogger) Log(record accesslog.LogRecord) {
l.access.Printf("%s %s %d %v %v", record.Method, record.Uri, record.Status, record.ElapsedTime, record.CustomRecords)
}

// InitAccessLogger returns a handler that wraps the supplied delegate with access logging.
func InitAccessLogger(h http.Handler, logFile, level string) http.Handler {
l := &AccessLogger{
access: newLogger(logFile, level),
}
return accesslog.NewLoggingHandler(h, l)
}

// Debugf - Debugf function
func Debugf(format string, args ...interface{}) {
log.Debugf(format, args...)
Expand Down

0 comments on commit b4d5f4a

Please sign in to comment.