Skip to content

Commit

Permalink
feat: ability to customize default logger. upgrade httplog version to v2
Browse files Browse the repository at this point in the history
  • Loading branch information
adolfo-cia committed May 21, 2024
1 parent f9f0a29 commit 97c0428
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 43 deletions.
13 changes: 3 additions & 10 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
module github.com/tomiok/webh

go 1.20
go 1.21

require (
github.com/go-chi/chi/v5 v5.0.8
github.com/go-chi/chi/v5 v5.0.12
github.com/go-chi/cors v1.2.1
github.com/go-chi/httplog v0.3.0
)

require (
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/rs/zerolog v1.27.0 // indirect
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
github.com/go-chi/httplog/v2 v2.0.11
)
23 changes: 4 additions & 19 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,21 +1,6 @@
github.com/coreos/go-systemd/v22 v22.3.3-0.20220203105225-a9a7ef127534/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/go-chi/chi/v5 v5.0.7/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
github.com/go-chi/chi/v5 v5.0.8 h1:lD+NLqFcAi1ovnVZpsnObHGW4xb4J8lNmoYVfECH1Y0=
github.com/go-chi/chi/v5 v5.0.8/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
github.com/go-chi/chi/v5 v5.0.12 h1:9euLV5sTrTNTRUU9POmDUvfxyj6LAABLUcEWO+JJb4s=
github.com/go-chi/chi/v5 v5.0.12/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
github.com/go-chi/cors v1.2.1 h1:xEC8UT3Rlp2QuWNEr4Fs/c2EAGVKBwy/1vHx3bppil4=
github.com/go-chi/cors v1.2.1/go.mod h1:sSbTewc+6wYHBBCW7ytsFSn836hqM7JxpglAy2Vzc58=
github.com/go-chi/httplog v0.3.0 h1:KW9UMJmjo1JQb5WnOWFc5KftSP4YxZRAQk60biarfIA=
github.com/go-chi/httplog v0.3.0/go.mod h1:/pIXuFSrOdc5heKIJRA5Q2mW7cZCI2RySqFZNFoZjKg=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/mattn/go-colorable v0.1.12 h1:jF+Du6AlPIjs2BiUiQlKOX0rt3SujHxPnksPKZbaA40=
github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/rs/xid v1.3.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.27.0 h1:1T7qCieN22GVc8S4Q2yuexzBb1EqjbgjSH9RohbMjKs=
github.com/rs/zerolog v1.27.0/go.mod h1:7frBqO0oezxmnO7GF86FY++uy8I0Tk/If5ni1G9Qc0U=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
github.com/go-chi/httplog/v2 v2.0.11 h1:eu6kYksMEJzBcOP+ba/iYudc0m5rv4VvBAzroJMkaY4=
github.com/go-chi/httplog/v2 v2.0.11/go.mod h1:/XXdxicJsp4BA5fapgIC3VuTD+z0Z/VzukoB3VDc1YE=
33 changes: 20 additions & 13 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@ package webh

import (
"context"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/go-chi/cors"
"github.com/go-chi/httplog"
"log"
"log/slog"
"net/http"
"os"
"os/signal"
"syscall"
"time"

"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/go-chi/cors"
"github.com/go-chi/httplog/v2"
)

type Option func(options *options)
Expand Down Expand Up @@ -88,16 +90,21 @@ func WithCompress(level int, types ...string) Option {
}

// WithRequestLogger add a JSON logger to your web server.
func WithRequestLogger(serviceName string) Option {
func WithRequestLogger(serviceName string, loggerOptions ...httplog.Options) Option {
loggerOption := httplog.Options{
LogLevel: slog.LevelInfo,
LevelFieldName: "LEVEL",
JSON: true,
Concise: true,
TimeFieldFormat: time.ANSIC,
TimeFieldName: "at",
}
if len(loggerOptions) > 0 {
loggerOption = loggerOptions[0]
}

return func(opt *options) {
logger := httplog.NewLogger(serviceName, httplog.Options{
LogLevel: "INFO",
LevelFieldName: "LEVEL",
JSON: true,
Concise: true,
TimeFieldFormat: time.ANSIC,
TimeFieldName: "at",
})
logger := httplog.NewLogger(serviceName, loggerOption)
opt.logger = httplog.RequestLogger(logger)
}
}
Expand Down
3 changes: 2 additions & 1 deletion server_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package webh_test

import (
"github.com/tomiok/webh"
"net/http"
"syscall"
"testing"
"time"

"github.com/tomiok/webh"
)

func Test_serverCreate(t *testing.T) {
Expand Down

0 comments on commit 97c0428

Please sign in to comment.