Skip to content

Commit

Permalink
#25 Create request log format
Browse files Browse the repository at this point in the history
  • Loading branch information
ybkuroki committed Oct 25, 2020
1 parent ddaa781 commit c7c91be
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 28 deletions.
4 changes: 1 addition & 3 deletions application.develop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ extension:
security_enabled: true

log:
format: ${time_rfc3339} [${level}] ${remote_ip} ${method} ${uri} ${status}
level: 1
file_path:
request_log_format: ${remote_ip} ${account_name} ${uri} ${method} ${status}

security:
exclude_path:
Expand Down
4 changes: 1 addition & 3 deletions application.docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ extension:
security_enabled: true

log:
format: ${time_rfc3339} [${level}] ${remote_ip} ${method} ${uri} ${status}
level: 2
file_path: ./application.log
request_log_format: ${remote_ip} ${account_name} ${uri} ${method} ${status}

security:
exclude_path:
Expand Down
4 changes: 1 addition & 3 deletions application.k8s.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ extension:
security_enabled: true

log:
format: ${time_rfc3339} [${level}] ${remote_ip} ${method} ${uri} ${status}
level: 2
file_path: ./application.log
request_log_format: ${remote_ip} ${account_name} ${uri} ${method} ${status}

security:
exclude_path:
Expand Down
5 changes: 1 addition & 4 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"

"github.com/jinzhu/configor"
"github.com/labstack/gommon/log"
)

// Config represents the composition of yml settings.
Expand All @@ -31,9 +30,7 @@ type Config struct {
SecurityEnabled bool `yaml:"security_enabled" default:"false"`
}
Log struct {
Format string `default:"${time_rfc3339} [${level}] ${remote_ip} ${method} ${uri} ${status}"`
Level log.Lvl `default:"2"`
FilePath string `yaml:"file_path"`
RequestLogFormat string `yaml:"request_log_format" default:"${remote_ip} ${account_name} ${uri} ${method} ${status}"`
}
Security struct {
ExculdePath []string `yaml:"exclude_path"`
Expand Down
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@ require (
github.com/jinzhu/gorm v1.9.12
github.com/labstack/echo-contrib v0.9.0
github.com/labstack/echo/v4 v4.1.16
github.com/labstack/gommon v0.3.0
github.com/leodido/go-urn v1.2.0 // indirect
github.com/lib/pq v1.7.0 // indirect
github.com/mattn/go-colorable v0.1.7 // indirect
github.com/mattn/go-sqlite3 v2.0.3+incompatible // indirect
github.com/stretchr/testify v1.6.1
github.com/valyala/fasttemplate v1.1.1 // indirect
github.com/valyala/fasttemplate v1.2.1
go.uber.org/multierr v1.6.0 // indirect
go.uber.org/zap v1.16.0
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9
Expand Down
10 changes: 8 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,10 @@ github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7V
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
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/labstack/echo-contrib v0.9.0 h1:hKBA2SnxdxR7sghH0J04zq/pImnKRmgvmQ6MvY9hug4=
github.com/labstack/echo-contrib v0.9.0/go.mod h1:TsFE5Vv0LRpZLoh4mMmaaAxzcTH+1CBFiUtVhwlegzU=
Expand Down Expand Up @@ -132,8 +134,8 @@ github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyC
github.com/valyala/fasttemplate v1.0.1/go.mod h1:UQGH1tvbgY+Nz5t2n7tXsz52dQxojPUpymEIMZ47gx8=
github.com/valyala/fasttemplate v1.1.0 h1:RZqt0yGBsps8NGvLSGW804QQqCUYYLsaOjTVHy1Ocw4=
github.com/valyala/fasttemplate v1.1.0/go.mod h1:UQGH1tvbgY+Nz5t2n7tXsz52dQxojPUpymEIMZ47gx8=
github.com/valyala/fasttemplate v1.1.1 h1:o7HedfIt0u4jgkQnk0jkUOUI45c0CbFgcOOVUpo/txI=
github.com/valyala/fasttemplate v1.1.1/go.mod h1:UQGH1tvbgY+Nz5t2n7tXsz52dQxojPUpymEIMZ47gx8=
github.com/valyala/fasttemplate v1.2.1 h1:TVEnxayobAdVkhQfrfes2IzOB6o+z4roRkPF52WA1u4=
github.com/valyala/fasttemplate v1.2.1/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ=
go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
go.uber.org/atomic v1.6.0 h1:Ezj3JGmsOnG1MoRWQkPBsKLe9DwWD9QeXzTRzzldNVk=
go.uber.org/atomic v1.6.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ=
Expand All @@ -156,6 +158,7 @@ golang.org/x/crypto v0.0.0-20200221231518-2aa609cf4a9d h1:1ZiEyfaQIg3Qh0EoqpwAak
golang.org/x/crypto v0.0.0-20200221231518-2aa609cf4a9d/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 h1:psW17arqaxU48Z5kZ0CQnkZWQJsqcURM6tKiBApRjXI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/lint v0.0.0-20190930215403-16217165b5de h1:5hukYrvBGR8/eNkX5mdUezrA6JiaEZDtJb9Ei+1LlBs=
golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/mod v0.0.0-20190513183733-4bf6d317e70e/go.mod h1:mXi4GBBbnImb6dmsKGUJ2LatrhH/nqhxcFungHvyanc=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand Down Expand Up @@ -197,6 +200,7 @@ golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3
golang.org/x/tools v0.0.0-20190608022120-eacb66d2a7c3/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc=
golang.org/x/tools v0.0.0-20190621195816-6e04913cbbac/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc=
golang.org/x/tools v0.0.0-20191029041327-9cc4af7d6b2c/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20191029190741-b9c20aec41a5 h1:hKsoRgsbwY1NafxrwTs+k64bikrLBkAgPir1TNCj3Zs=
golang.org/x/tools v0.0.0-20191029190741-b9c20aec41a5/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/appengine v1.4.0 h1:/wp5JvzpHIxhs/dumFmF7BXTf3Z+dd4uXta4kVyO508=
Expand All @@ -206,6 +210,7 @@ gopkg.in/boj/redistore.v1 v1.0.0-20160128113310-fc113767cd6b h1:U/Uqd1232+wrnHOv
gopkg.in/boj/redistore.v1 v1.0.0-20160128113310-fc113767cd6b/go.mod h1:fgfIZMlsafAHpspcks2Bul+MWUNw/2dyQmjC2faKjtg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/go-playground/assert.v1 v1.2.1 h1:xoYuJVE7KT85PYWrN730RguIQO0ePzVRfFMXadIrXTM=
Expand All @@ -220,4 +225,5 @@ gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
honnef.co/go/tools v0.0.1-2019.2.3 h1:3JgtbtFHMiCmsznwGVTUWbgGov+pVqnlf1dEJTNAXeM=
honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg=
2 changes: 1 addition & 1 deletion logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func InitLogger() {
sugar := zap.Sugar()
// set package varriable logger.
logger = newLogger(sugar)
logger.zap.Infof("Success to read zap logger configuration")
logger.zap.Infof("Success to read zap logger configuration: zaplogger." + *config.GetEnv() + ".yml")
}

// ==============================================================
Expand Down
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func main() {
config.Load()
logger.InitLogger()
middleware.InitLoggerMiddleware(e)
e.Logger.Info("Loaded this configuration : application." + *config.GetEnv() + ".yml")
logger.GetZapLogger().Infof("Loaded this configuration : application." + *config.GetEnv() + ".yml")

repository.InitDB()
db := repository.GetDB()
Expand All @@ -27,7 +27,7 @@ func main() {
router.Init(e, config.GetConfig())
middleware.InitSessionMiddleware(e, config.GetConfig())
if err := e.Start(":8080"); err != nil {
e.Logger.Error(err)
logger.GetZapLogger().Errorf(err.Error())
}

defer db.Close()
Expand Down
32 changes: 26 additions & 6 deletions middleware/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ package middleware

import (
"fmt"
"io"
"net/http"
"regexp"
"strconv"

"github.com/gorilla/sessions"
"github.com/labstack/echo-contrib/session"
"github.com/labstack/echo/v4"
"github.com/valyala/fasttemplate"
"github.com/ybkuroki/go-webapp-sample/config"
"github.com/ybkuroki/go-webapp-sample/logger"
mySession "github.com/ybkuroki/go-webapp-sample/session"
Expand Down Expand Up @@ -47,11 +50,28 @@ func RequestLoggerMiddleware(next echo.HandlerFunc) echo.HandlerFunc {
if err := next(c); err != nil {
c.Error(err)
}
if account := mySession.GetAccount(c); account != nil {
logger.GetZapLogger().Infof("%s %s %s %d", account.Name, req.RequestURI, req.Method, res.Status)
} else {
logger.GetZapLogger().Infof("%s %s %s %d", "None", req.RequestURI, req.Method, res.Status)
}

template := fasttemplate.New(config.GetConfig().Log.RequestLogFormat, "${", "}")
logstr := template.ExecuteFuncString(func(w io.Writer, tag string) (int, error) {
switch tag {
case "remote_ip":
return w.Write([]byte(c.RealIP()))
case "account_name":
if account := mySession.GetAccount(c); account != nil {
return w.Write([]byte(account.Name))
}
return w.Write([]byte("None"))
case "uri":
return w.Write([]byte(req.RequestURI))
case "method":
return w.Write([]byte(req.Method))
case "status":
return w.Write([]byte(strconv.Itoa(res.Status)))
default:
return w.Write([]byte(""))
}
})
logger.GetZapLogger().Debugf(logstr)
return nil
}
}
Expand All @@ -60,7 +80,7 @@ func RequestLoggerMiddleware(next echo.HandlerFunc) echo.HandlerFunc {
// ref: https://echo.labstack.com/cookbook/middleware
func ActionLoggerMiddleware(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
logger.GetZapLogger().Debug(c.Path() + " Action Start")
logger.GetZapLogger().Debugf(c.Path() + " Action Start")
if err := next(c); err != nil {
c.Error(err)
}
Expand Down
3 changes: 1 addition & 2 deletions test/unittest_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ func Prepare() *echo.Echo {
conf.Database.Migration = true
conf.Extension.MasterGenerator = true
conf.Extension.SecurityEnabled = false
conf.Log.Format = "${time_rfc3339} [${level}] ${remote_ip} ${method} ${uri} ${status}"
conf.Log.Level = 1
conf.Log.RequestLogFormat = "${remote_ip} ${account_name} ${uri} ${method} ${status}"
config.SetConfig(conf)

logger.InitLogger()
Expand Down

0 comments on commit c7c91be

Please sign in to comment.