From 4165b31d7f471588312967823b1c7a4e59622055 Mon Sep 17 00:00:00 2001 From: bmf-san Date: Sat, 26 Aug 2023 17:08:47 +0900 Subject: [PATCH] [update] replace logger --- .circleci/config.yml | 4 +-- .env.example | 3 +- README.md | 35 +++++++++++++++-------- app/Dockerfile | 2 +- app/Dockerfile.local | 2 +- app/domain/logger.go | 9 ------ app/go.mod | 1 - app/go.sum | 2 -- app/infrastructure/logger.go | 41 ++++++--------------------- app/infrastructure/middleware.go | 5 ++-- app/infrastructure/router.go | 4 +-- app/interfaces/controller/auth.go | 7 +++-- app/interfaces/controller/category.go | 7 +++-- app/interfaces/controller/comment.go | 7 +++-- app/interfaces/controller/post.go | 7 +++-- app/interfaces/controller/tag.go | 7 +++-- app/main.go | 9 +++--- 17 files changed, 66 insertions(+), 86 deletions(-) delete mode 100644 app/domain/logger.go diff --git a/.circleci/config.yml b/.circleci/config.yml index 537034c..26949b3 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -81,8 +81,8 @@ workflows: - lint: matrix: parameters: - version: ["1.21", "1.20"] + version: ["1.21"] - test: matrix: parameters: - version: ["1.21", "1.20"] \ No newline at end of file + version: ["1.21"] \ No newline at end of file diff --git a/.env.example b/.env.example index fc6a128..4ea5434 100644 --- a/.env.example +++ b/.env.example @@ -6,8 +6,7 @@ TIME_ZONE=Asia/Tokyo ALLOW_ORIGIN=* JWT_ACCESS_TOKEN_SECRET=gobel-api-jwt-access-token-secret JWT_REFRESH_TOKEN_SECRET=gobel-api-jwt-refresh-token-secret -LOG_THRESHOLD=0 # 0:info 1:warn 2:error 3:fatal -LOG_TIME_ZONE_OFFSET=32400 # 9*60*60 +LOG_LEVEL=0 # -4: Debug 0: Info 4: Warn 8: Error # mysql DB_DRIVER=mysql diff --git a/README.md b/README.md index 29c600e..188bf40 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ The Gobel is a headless cms built with golang. [bmfsan/gobel-api](https://hub.docker.com/r/bmfsan/gobel-api) # Features -- Support Go 1.20 >= 1.18 +- Support Go 1.21 # Documentation - [Coding Rule](https://github.com/bmf-san/gobel-api/blob/master/doc/CodingRule.md) @@ -65,21 +65,32 @@ gobel-api is based on Clean Architecture. | -------------------- | -------------- | | Frameworks & Drivers | infrastructure | | Interface | interfaces | -| usecase | usecase | +| Usecase | usecase | | Entities | domain | -cf. [blog.cleancoder.com - The Clean Code Blog by Robert C. Martin (Uncle Bob)](https://blog.cleancoder.com/uncle-bob/2012/08/13/the-clean-architecture.html) +cf. [bmf-tech.com - Golangでクリーンアーキテクチャに入門する](https://bmf-tech.com/posts/Golang%E3%81%A7%E3%82%AF%E3%83%AA%E3%83%BC%E3%83%B3%E3%82%A2%E3%83%BC%E3%82%AD%E3%83%86%E3%82%AF%E3%83%81%E3%83%A3%E3%81%AB%E5%85%A5%E9%96%80%E3%81%99%E3%82%8B) -# Contributing -We welcome your issue or pull request from everyone. -Please make sure to read the [CONTRIBUTING.md](https://github.com/bmf-san/gobel-api/.github/CONTRIBUTING.md). +# Contribution +Issues and Pull Requests are always welcome. + +We would be happy to receive your contributions. + +Please review the following documents before making a contribution. + +[CODE_OF_CONDUCT](https://github.com/bmf-san/gobel-api/blob/master/.github/CODE_OF_CONDUCT.md) +[CONTRIBUTING](https://github.com/bmf-san/gobel-api/blob/master/.github/CONTRIBUTING.md) # License -This project is licensed under the terms of the MIT license. +Based on the MIT License. + +[LICENSE](https://github.com/bmf-san/gobel-api/blob/master/LICENSE) -# Author -bmf - Software engineer. +## Author +[bmf-san](https://github.com/bmf-san) -- [github - bmf-san/bmf-san](https://github.com/bmf-san/bmf-san) -- [twitter - @bmf-san](https://twitter.com/bmf_san) -- [blog - bmf-tech](http://bmf-tech.com/) +- Email + - bmf.infomation@gmail.com +- Blog + - [bmf-tech.com](http://bmf-tech.com) +- Twitter + - [bmf-san](https://twitter.com/bmf-san) \ No newline at end of file diff --git a/app/Dockerfile b/app/Dockerfile index 96b537c..1c49c61 100644 --- a/app/Dockerfile +++ b/app/Dockerfile @@ -1,4 +1,4 @@ -FROM golang:1.20.0-alpine as builder +FROM golang:1.21.0-alpine as builder WORKDIR /go/gobel-api/app diff --git a/app/Dockerfile.local b/app/Dockerfile.local index 09364cc..39be1e1 100644 --- a/app/Dockerfile.local +++ b/app/Dockerfile.local @@ -1,4 +1,4 @@ -FROM golang:1.20.0-alpine +FROM golang:1.21.0-alpine WORKDIR /go/gobel-api/app diff --git a/app/domain/logger.go b/app/domain/logger.go deleted file mode 100644 index dcee6dc..0000000 --- a/app/domain/logger.go +++ /dev/null @@ -1,9 +0,0 @@ -package domain - -// A Logger represents a logger. -type Logger interface { - Fatal(msg string) - Error(msg string) - Warn(msg string) - Info(msg string) -} diff --git a/app/go.mod b/app/go.mod index 7b8ec1e..82e75c7 100644 --- a/app/go.mod +++ b/app/go.mod @@ -4,7 +4,6 @@ go 1.21 require ( github.com/bmf-san/goblin v0.0.0-20230202025516-4793d4844d3f - github.com/bmf-san/golem v0.0.0-20210221133857-7ecb33f0ca76 golang.org/x/crypto v0.7.0 ) diff --git a/app/go.sum b/app/go.sum index fcb732c..224d886 100644 --- a/app/go.sum +++ b/app/go.sum @@ -1,7 +1,5 @@ github.com/bmf-san/goblin v0.0.0-20230202025516-4793d4844d3f h1:9zv+ydZDJuioOV9taKWq/LgdGziBYilo4asfjbjW5oE= github.com/bmf-san/goblin v0.0.0-20230202025516-4793d4844d3f/go.mod h1:RovWvwcoNNx7Idk/FanQtQjRSWIjZobNusVK8P3Semo= -github.com/bmf-san/golem v0.0.0-20210221133857-7ecb33f0ca76 h1:eZRVFcQ82z1uPDYQzI71M5zXwpjvrD3tLy+uc0txC6E= -github.com/bmf-san/golem v0.0.0-20210221133857-7ecb33f0ca76/go.mod h1:v2YJqaEietOXsYUQVOJeBNIxAGrn4GCeWbfpVzAndRE= github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44= github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= diff --git a/app/infrastructure/logger.go b/app/infrastructure/logger.go index 4b1ac63..05a0bb5 100644 --- a/app/infrastructure/logger.go +++ b/app/infrastructure/logger.go @@ -1,39 +1,16 @@ package infrastructure import ( - "time" + "os" - "github.com/bmf-san/golem" + "log/slog" ) -// A Logger represents a logger. -type Logger struct { - logger *golem.Logger -} - -// NewLogger creates a Logger. -func NewLogger(threshold int, location *time.Location) *Logger { - return &Logger{ - logger: golem.NewLogger(threshold, location), - } -} - -// Fatal outputs a fatal level log. -func (l *Logger) Fatal(msg string) { - l.logger.Fatal(msg) -} - -// Error outputs a fatal level log. -func (l *Logger) Error(msg string) { - l.logger.Error(msg) -} - -// Warn outputs a fatal level log. -func (l *Logger) Warn(msg string) { - l.logger.Warn(msg) -} - -// Info outputs a fatal level log. -func (l *Logger) Info(msg string) { - l.logger.Info(msg) +// NewLogger creates a logger. +func NewLogger(level int) *slog.Logger { + handler := slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{ + Level: slog.Level(level), + }) + logger := slog.New(handler) + return logger } diff --git a/app/infrastructure/middleware.go b/app/infrastructure/middleware.go index d927640..a89bec8 100644 --- a/app/infrastructure/middleware.go +++ b/app/infrastructure/middleware.go @@ -1,6 +1,7 @@ package infrastructure import ( + "log/slog" "net/http" "os" "runtime" @@ -12,13 +13,13 @@ import ( // Middleware represents the plural of middelware. type Middleware struct { - logger domain.Logger + logger *slog.Logger adminRepository repository.AdminRepository JWT repository.JWT } // NewLogger creates a Middleware. -func NewMiddleware(l domain.Logger, ar repository.AdminRepository, jr repository.JWT) *Middleware { +func NewMiddleware(l *slog.Logger, ar repository.AdminRepository, jr repository.JWT) *Middleware { return &Middleware{ logger: l, adminRepository: ar, diff --git a/app/infrastructure/router.go b/app/infrastructure/router.go index 2d85954..1ad86de 100644 --- a/app/infrastructure/router.go +++ b/app/infrastructure/router.go @@ -2,11 +2,11 @@ package infrastructure import ( "database/sql" + "log/slog" "net/http" "net/http/pprof" "os" - "github.com/bmf-san/gobel-api/app/domain" "github.com/bmf-san/gobel-api/app/interfaces/controller" "github.com/bmf-san/gobel-api/app/interfaces/repository" "github.com/bmf-san/goblin" @@ -14,7 +14,7 @@ import ( ) // Route sets the routing. -func Route(connm *sql.DB, connr *redis.Client, l domain.Logger) *goblin.Router { +func Route(connm *sql.DB, connr *redis.Client, l *slog.Logger) *goblin.Router { ar := repository.AdminRepository{ ConnMySQL: connm, ConnRedis: connr, diff --git a/app/interfaces/controller/auth.go b/app/interfaces/controller/auth.go index b09ff5a..ace6394 100644 --- a/app/interfaces/controller/auth.go +++ b/app/interfaces/controller/auth.go @@ -6,7 +6,8 @@ import ( "io" "net/http" - "github.com/bmf-san/gobel-api/app/domain" + "log/slog" + "github.com/bmf-san/gobel-api/app/interfaces/repository" "github.com/bmf-san/gobel-api/app/usecase" "github.com/bmf-san/gobel-api/app/usecase/dto/request" @@ -18,11 +19,11 @@ import ( // An AuthController is a controller for an authentication. type AuthController struct { AuthInteractor usecase.Auth - Logger domain.Logger + Logger *slog.Logger } // NewAuthController creates an AuthController. -func NewAuthController(connMySQL *sql.DB, connRedis *redis.Client, logger domain.Logger) *AuthController { +func NewAuthController(connMySQL *sql.DB, connRedis *redis.Client, logger *slog.Logger) *AuthController { return &AuthController{ AuthInteractor: &interactor.AuthInteractor{ AdminRepository: &repository.AdminRepository{ diff --git a/app/interfaces/controller/category.go b/app/interfaces/controller/category.go index 62b911c..42b25c3 100644 --- a/app/interfaces/controller/category.go +++ b/app/interfaces/controller/category.go @@ -7,7 +7,8 @@ import ( "net/http" "strconv" - "github.com/bmf-san/gobel-api/app/domain" + "log/slog" + "github.com/bmf-san/gobel-api/app/interfaces/repository" "github.com/bmf-san/gobel-api/app/usecase" "github.com/bmf-san/gobel-api/app/usecase/dto/request" @@ -19,11 +20,11 @@ import ( // A CategoryController is a controller for a comment. type CategoryController struct { CategoryInteractor usecase.Category - Logger domain.Logger + Logger *slog.Logger } // NewCategoryController creates a CategoryController. -func NewCategoryController(connMySQL *sql.DB, logger domain.Logger) *CategoryController { +func NewCategoryController(connMySQL *sql.DB, logger *slog.Logger) *CategoryController { return &CategoryController{ CategoryInteractor: &interactor.CategoryInteractor{ Category: &repository.Category{ diff --git a/app/interfaces/controller/comment.go b/app/interfaces/controller/comment.go index e8e06c6..d37649b 100644 --- a/app/interfaces/controller/comment.go +++ b/app/interfaces/controller/comment.go @@ -7,7 +7,8 @@ import ( "net/http" "strconv" - "github.com/bmf-san/gobel-api/app/domain" + "log/slog" + "github.com/bmf-san/gobel-api/app/interfaces/repository" "github.com/bmf-san/gobel-api/app/usecase" "github.com/bmf-san/gobel-api/app/usecase/dto/request" @@ -19,11 +20,11 @@ import ( // A CommentController is a controller for a comment. type CommentController struct { CommentInteractor usecase.Comment - Logger domain.Logger + Logger *slog.Logger } // NewCommentController creates a CommentController. -func NewCommentController(connMySQL *sql.DB, logger domain.Logger) *CommentController { +func NewCommentController(connMySQL *sql.DB, logger *slog.Logger) *CommentController { return &CommentController{ CommentInteractor: &interactor.CommentInteractor{ Comment: &repository.Comment{ diff --git a/app/interfaces/controller/post.go b/app/interfaces/controller/post.go index 380166b..3d55668 100644 --- a/app/interfaces/controller/post.go +++ b/app/interfaces/controller/post.go @@ -7,7 +7,8 @@ import ( "net/http" "strconv" - "github.com/bmf-san/gobel-api/app/domain" + "log/slog" + "github.com/bmf-san/gobel-api/app/interfaces/repository" "github.com/bmf-san/gobel-api/app/usecase" "github.com/bmf-san/gobel-api/app/usecase/dto/request" @@ -20,11 +21,11 @@ import ( // A PostController is a controller for a post. type PostController struct { PostInteractor usecase.Post - Logger domain.Logger + Logger *slog.Logger } // NewPostController creates a PostController. -func NewPostController(connMySQL *sql.DB, connRedis *redis.Client, logger domain.Logger) *PostController { +func NewPostController(connMySQL *sql.DB, connRedis *redis.Client, logger *slog.Logger) *PostController { return &PostController{ PostInteractor: &interactor.PostInteractor{ AdminRepository: &repository.AdminRepository{ diff --git a/app/interfaces/controller/tag.go b/app/interfaces/controller/tag.go index 4a8fdfb..9d60f0b 100644 --- a/app/interfaces/controller/tag.go +++ b/app/interfaces/controller/tag.go @@ -7,7 +7,8 @@ import ( "net/http" "strconv" - "github.com/bmf-san/gobel-api/app/domain" + "log/slog" + "github.com/bmf-san/gobel-api/app/interfaces/repository" "github.com/bmf-san/gobel-api/app/usecase" "github.com/bmf-san/gobel-api/app/usecase/dto/request" @@ -19,11 +20,11 @@ import ( // A TagController is a controller for a post. type TagController struct { TagInteractor usecase.Tag - Logger domain.Logger + Logger *slog.Logger } // NewTagController creates a TagController. -func NewTagController(connMySQL *sql.DB, logger domain.Logger) *TagController { +func NewTagController(connMySQL *sql.DB, logger *slog.Logger) *TagController { return &TagController{ TagInteractor: &interactor.TagInteractor{ Tag: &repository.Tag{ diff --git a/app/main.go b/app/main.go index 70e4cba..884d549 100644 --- a/app/main.go +++ b/app/main.go @@ -18,11 +18,8 @@ import ( const timeout time.Duration = 10 * time.Second func main() { - threshold, _ := strconv.Atoi(os.Getenv("LOG_THRESHOLD")) - offset, _ := strconv.Atoi(os.Getenv("LOG_TIME_ZONE_OFFSET")) - location := time.FixedZone(os.Getenv("TIME_ZONE"), offset) - - logger := infrastructure.NewLogger(threshold, location) + level, _ := strconv.Atoi(os.Getenv("LOG_LEVEL")) + logger := infrastructure.NewLogger(level) defer func() { if x := recover(); x != nil { @@ -50,6 +47,8 @@ func main() { Handler: r, } + logger.Info("Start server") + go func() { if err := s.ListenAndServe(); err != http.ErrServerClosed { logger.Error(err.Error())