Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

log/slogを使う #27

Merged
merged 2 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/task-test-go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ jobs:
uses: actions/checkout@v4
- name: Setup Go
uses: actions/setup-go@v5
with:
go-version-file: go.mod
- uses: golangci/golangci-lint-action@v4
with:
skip-pkg-cache: true # https://github.com/golangci/golangci-lint-action/issues/677
Expand All @@ -24,5 +26,7 @@ jobs:
uses: actions/checkout@v4
- name: Setup Go
uses: actions/setup-go@v5
with:
go-version-file: go.mod
- name: test
run: make test-go
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
module github.com/hatena/chi-middlewares

go 1.20
go 1.22

toolchain go1.22.1
Comment on lines +3 to +5
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

log/slog が使えるようになったのは1.22だけど、現在のバージョンまで上げておく


require (
github.com/go-chi/chi/v5 v5.0.12
github.com/google/go-cmp v0.6.0
golang.org/x/exp v0.0.0-20240318143956-a85f2c67cd81
)
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,3 @@ 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/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
golang.org/x/exp v0.0.0-20240318143956-a85f2c67cd81 h1:6R2FC06FonbXQ8pK11/PDFY6N6LWlf9KlzibaCapmqc=
golang.org/x/exp v0.0.0-20240318143956-a85f2c67cd81/go.mod h1:CQ1k9gNrJ50XIzaKCRR2hssIjF07kZFEiieALBM/ARQ=
2 changes: 1 addition & 1 deletion logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package logger

import (
"context"
"log/slog"
"net/http"

"github.com/go-chi/chi/v5/middleware"
"golang.org/x/exp/slog"
)

type LogKey struct{}
Expand Down
2 changes: 1 addition & 1 deletion logger/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"fmt"
"io"
"log/slog"
"math/rand"
"net/http"
"net/http/httptest"
Expand All @@ -13,7 +14,6 @@ import (

"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"golang.org/x/exp/slog"
)

func Test_Logger(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions logger/request_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package logger
import (
"context"
"fmt"
"log/slog"
"net/http"
"time"

"github.com/go-chi/chi/v5/middleware"
"golang.org/x/exp/slog"
)

// RequestLogger は、HTTP request が完了した時にその要約を log に吐く。Logger は RequestLogger の前に置かねばならない。middleware.RealIP は RequestLogger の前に置かねばならない。middleware.Recoverer は RequestLogger の後に置かねばならない
Expand Down Expand Up @@ -64,7 +64,7 @@ func (e *requestLogEntry) Write(

// middleware.Recoverer が呼ぶ
func (e *requestLogEntry) Panic(v interface{}, stack []byte) {
e.log.ErrorCtx(
e.log.ErrorContext(
e.ctx,
"panic",
slog.String("panic", fmt.Sprintf("%+v", v)),
Expand Down
2 changes: 1 addition & 1 deletion logger/request_logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"crypto/tls"
"encoding/json"
"fmt"
"log/slog"
"math/rand"
"net/http"
"net/http/httptest"
Expand All @@ -13,7 +14,6 @@ import (
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/google/go-cmp/cmp"
"golang.org/x/exp/slog"
)

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