Skip to content

Commit

Permalink
Merge pull request #80 from bmf-san/feature/impl-contextual-logger
Browse files Browse the repository at this point in the history
Contextual loggingの実装
  • Loading branch information
bmf-san authored Oct 8, 2023
2 parents 0f28cf0 + 5b0f82d commit fe31911
Show file tree
Hide file tree
Showing 15 changed files with 244 additions and 209 deletions.
31 changes: 15 additions & 16 deletions app/controller/category_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,21 @@ import (
"io"
"net/http"

"log/slog"

"github.com/bmf-san/bmf-tech-client/app/api"
"github.com/bmf-san/bmf-tech-client/app/logger"
"github.com/bmf-san/bmf-tech-client/app/model"
"github.com/bmf-san/bmf-tech-client/app/presenter"
)

// A CategoryController is a controller for category.
type CategoryController struct {
Logger *slog.Logger
Logger *logger.Logger
Client *api.Client
Presenter *presenter.Presenter
}

// NewCategoryController creates a CategoryController.
func NewCategoryController(logger *slog.Logger, client *api.Client, presenter *presenter.Presenter) *CategoryController {
func NewCategoryController(logger *logger.Logger, client *api.Client, presenter *presenter.Presenter) *CategoryController {
return &CategoryController{
Logger: logger,
Client: client,
Expand All @@ -36,23 +35,23 @@ func (cc *CategoryController) Index() http.Handler {
code := http.StatusOK
page, _, err := cc.Client.GetPageAndLimit(r)
if err != nil {
cc.Logger.Error(err.Error())
cc.Logger.ErrorContext(r.Context(), err.Error())
code = http.StatusInternalServerError
buf, err := cc.Presenter.ExecuteError(buf, code)
if err != nil {
cc.Logger.Error(err.Error())
cc.Logger.ErrorContext(r.Context(), err.Error())
}
bufWriteTo(buf, w, code)
return
}

resp, err := cc.Client.GetCategories(page, 100)
if err != nil {
cc.Logger.Error(err.Error())
cc.Logger.ErrorContext(r.Context(), err.Error())
code = http.StatusInternalServerError
buf, err := cc.Presenter.ExecuteError(buf, code)
if err != nil {
cc.Logger.Error(err.Error())
cc.Logger.ErrorContext(r.Context(), err.Error())
}
bufWriteTo(buf, w, code)
return
Expand All @@ -61,11 +60,11 @@ func (cc *CategoryController) Index() http.Handler {

body, err := io.ReadAll(resp.Body)
if err != nil {
cc.Logger.Error(err.Error())
cc.Logger.ErrorContext(r.Context(), err.Error())
code = http.StatusInternalServerError
buf, err := cc.Presenter.ExecuteError(buf, code)
if err != nil {
cc.Logger.Error(err.Error())
cc.Logger.ErrorContext(r.Context(), err.Error())
}
bufWriteTo(buf, w, code)
return
Expand All @@ -74,23 +73,23 @@ func (cc *CategoryController) Index() http.Handler {
var categories model.Categories

if err := json.Unmarshal(body, &categories); err != nil {
cc.Logger.Error(err.Error())
cc.Logger.ErrorContext(r.Context(), err.Error())
code = http.StatusInternalServerError
buf, err := cc.Presenter.ExecuteError(buf, code)
if err != nil {
cc.Logger.Error(err.Error())
cc.Logger.ErrorContext(r.Context(), err.Error())
}
bufWriteTo(buf, w, code)
return
}

var pagination model.Pagination
if err := pagination.Convert(resp.Header); err != nil {
cc.Logger.Error(err.Error())
cc.Logger.ErrorContext(r.Context(), err.Error())
code = http.StatusInternalServerError
buf, err := cc.Presenter.ExecuteError(buf, code)
if err != nil {
cc.Logger.Error(err.Error())
cc.Logger.ErrorContext(r.Context(), err.Error())
}
bufWriteTo(buf, w, code)
return
Expand All @@ -104,11 +103,11 @@ func (cc *CategoryController) Index() http.Handler {
},
})
if err != nil {
cc.Logger.Error(err.Error())
cc.Logger.ErrorContext(r.Context(), err.Error())
code = http.StatusInternalServerError
buf, err = cc.Presenter.ExecuteError(buf, code)
if err != nil {
cc.Logger.Error(err.Error())
cc.Logger.ErrorContext(r.Context(), err.Error())
}
}

Expand Down
7 changes: 3 additions & 4 deletions app/controller/comment_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,20 @@ package controller
import (
"net/http"

"log/slog"

"github.com/bmf-san/bmf-tech-client/app/api"
"github.com/bmf-san/bmf-tech-client/app/logger"
"github.com/bmf-san/bmf-tech-client/app/presenter"
)

// A CommentControlller is a controller for a comment.
type CommentControlller struct {
Logger *slog.Logger
Logger *logger.Logger
Client *api.Client
Presenter *presenter.Presenter
}

// NewCommentController creates a NewCommentController.
func NewCommentController(logger *slog.Logger, client *api.Client, presenter *presenter.Presenter) *CommentControlller {
func NewCommentController(logger *logger.Logger, client *api.Client, presenter *presenter.Presenter) *CommentControlller {
return &CommentControlller{
Logger: logger,
Client: client,
Expand Down
19 changes: 9 additions & 10 deletions app/controller/feed_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,21 @@ import (
"net/http"
"os"

"log/slog"

"github.com/bmf-san/bmf-tech-client/app/api"
"github.com/bmf-san/bmf-tech-client/app/logger"
"github.com/bmf-san/bmf-tech-client/app/model"
"github.com/bmf-san/bmf-tech-client/app/presenter"
)

// A FeedController is a controller for feed.
type FeedController struct {
Logger *slog.Logger
Logger *logger.Logger
Client *api.Client
Presenter *presenter.Presenter
}

// NewFeedController creates a FeedController.
func NewFeedController(logger *slog.Logger, client *api.Client, presenter *presenter.Presenter) *FeedController {
func NewFeedController(logger *logger.Logger, client *api.Client, presenter *presenter.Presenter) *FeedController {
return &FeedController{
Logger: logger,
Client: client,
Expand All @@ -39,11 +38,11 @@ func (fc *FeedController) Index() http.Handler {
// NOTE: Since api does not support getting all items, so taking a rough method.
resp, err := fc.Client.GetPosts(1, 99999)
if err != nil {
fc.Logger.Error(err.Error())
fc.Logger.ErrorContext(r.Context(), err.Error())
code = http.StatusInternalServerError
buf, err := fc.Presenter.ExecuteError(buf, code)
if err != nil {
fc.Logger.Error(err.Error())
fc.Logger.ErrorContext(r.Context(), err.Error())
}
bufWriteTo(buf, w, code)
return
Expand All @@ -52,11 +51,11 @@ func (fc *FeedController) Index() http.Handler {

body, err := io.ReadAll(resp.Body)
if err != nil {
fc.Logger.Error(err.Error())
fc.Logger.ErrorContext(r.Context(), err.Error())
code = http.StatusInternalServerError
buf, err := fc.Presenter.ExecuteError(buf, code)
if err != nil {
fc.Logger.Error(err.Error())
fc.Logger.ErrorContext(r.Context(), err.Error())
}
bufWriteTo(buf, w, code)
return
Expand All @@ -65,11 +64,11 @@ func (fc *FeedController) Index() http.Handler {
var posts model.Posts

if err := json.Unmarshal(body, &posts); err != nil {
fc.Logger.Error(err.Error())
fc.Logger.ErrorContext(r.Context(), err.Error())
code = http.StatusInternalServerError
buf, err := fc.Presenter.ExecuteError(buf, code)
if err != nil {
fc.Logger.Error(err.Error())
fc.Logger.ErrorContext(r.Context(), err.Error())
}
bufWriteTo(buf, w, code)
return
Expand Down
27 changes: 13 additions & 14 deletions app/controller/home_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,21 @@ import (
"io"
"net/http"

"log/slog"

"github.com/bmf-san/bmf-tech-client/app/api"
"github.com/bmf-san/bmf-tech-client/app/logger"
"github.com/bmf-san/bmf-tech-client/app/model"
"github.com/bmf-san/bmf-tech-client/app/presenter"
)

// A HomeController is a controller for a home.
type HomeController struct {
Logger *slog.Logger
Logger *logger.Logger
Client *api.Client
Presenter *presenter.Presenter
}

// NewHomeController creates a HomeController.
func NewHomeController(logger *slog.Logger, client *api.Client, presenter *presenter.Presenter) *HomeController {
func NewHomeController(logger *logger.Logger, client *api.Client, presenter *presenter.Presenter) *HomeController {
return &HomeController{
Logger: logger,
Client: client,
Expand All @@ -36,23 +35,23 @@ func (hc *HomeController) Index() http.Handler {
code := http.StatusOK
page, limit, err := hc.Client.GetPageAndLimit(r)
if err != nil {
hc.Logger.Error(err.Error())
hc.Logger.ErrorContext(r.Context(), err.Error())
code = http.StatusInternalServerError
buf, err := hc.Presenter.ExecuteError(buf, code)
if err != nil {
hc.Logger.Error(err.Error())
hc.Logger.ErrorContext(r.Context(), err.Error())
}
bufWriteTo(buf, w, code)
return
}

resp, err := hc.Client.GetPosts(page, limit)
if err != nil {
hc.Logger.Error(err.Error())
hc.Logger.ErrorContext(r.Context(), err.Error())
code = http.StatusInternalServerError
buf, err := hc.Presenter.ExecuteError(buf, code)
if err != nil {
hc.Logger.Error(err.Error())
hc.Logger.ErrorContext(r.Context(), err.Error())
}
bufWriteTo(buf, w, code)
return
Expand All @@ -61,11 +60,11 @@ func (hc *HomeController) Index() http.Handler {

body, err := io.ReadAll(resp.Body)
if err != nil {
hc.Logger.Error(err.Error())
hc.Logger.ErrorContext(r.Context(), err.Error())
code = http.StatusInternalServerError
buf, err := hc.Presenter.ExecuteError(buf, code)
if err != nil {
hc.Logger.Error(err.Error())
hc.Logger.ErrorContext(r.Context(), err.Error())
}
bufWriteTo(buf, w, code)
return
Expand All @@ -74,11 +73,11 @@ func (hc *HomeController) Index() http.Handler {
var posts model.Posts

if err := json.Unmarshal(body, &posts); err != nil {
hc.Logger.Error(err.Error())
hc.Logger.ErrorContext(r.Context(), err.Error())
code = http.StatusInternalServerError
buf, err := hc.Presenter.ExecuteError(buf, code)
if err != nil {
hc.Logger.Error(err.Error())
hc.Logger.ErrorContext(r.Context(), err.Error())
}
bufWriteTo(buf, w, code)
return
Expand All @@ -88,11 +87,11 @@ func (hc *HomeController) Index() http.Handler {
Posts: &posts,
})
if err != nil {
hc.Logger.Error(err.Error())
hc.Logger.ErrorContext(r.Context(), err.Error())
code = http.StatusInternalServerError
buf, err = hc.Presenter.ExecuteError(buf, code)
if err != nil {
hc.Logger.Error(err.Error())
hc.Logger.ErrorContext(r.Context(), err.Error())
}
}

Expand Down
Loading

0 comments on commit fe31911

Please sign in to comment.