Skip to content

Commit

Permalink
Merge pull request #104 from bmf-san/feature/replace-logger-to-slog
Browse files Browse the repository at this point in the history
[update] replace logger
  • Loading branch information
bmf-san authored Aug 26, 2023
2 parents a46c345 + 4165b31 commit 51051f5
Show file tree
Hide file tree
Showing 17 changed files with 66 additions and 86 deletions.
4 changes: 2 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ workflows:
- lint:
matrix:
parameters:
version: ["1.21", "1.20"]
version: ["1.21"]
- test:
matrix:
parameters:
version: ["1.21", "1.20"]
version: ["1.21"]
3 changes: 1 addition & 2 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 23 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
2 changes: 1 addition & 1 deletion app/Dockerfile
Original file line number Diff line number Diff line change
@@ -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

Expand Down
2 changes: 1 addition & 1 deletion app/Dockerfile.local
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.20.0-alpine
FROM golang:1.21.0-alpine

WORKDIR /go/gobel-api/app

Expand Down
9 changes: 0 additions & 9 deletions app/domain/logger.go

This file was deleted.

1 change: 0 additions & 1 deletion app/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand Down
2 changes: 0 additions & 2 deletions app/go.sum
Original file line number Diff line number Diff line change
@@ -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=
Expand Down
41 changes: 9 additions & 32 deletions app/infrastructure/logger.go
Original file line number Diff line number Diff line change
@@ -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
}
5 changes: 3 additions & 2 deletions app/infrastructure/middleware.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package infrastructure

import (
"log/slog"
"net/http"
"os"
"runtime"
Expand All @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions app/infrastructure/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@ 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"
"github.com/go-redis/redis/v9"
)

// 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,
Expand Down
7 changes: 4 additions & 3 deletions app/interfaces/controller/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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{
Expand Down
7 changes: 4 additions & 3 deletions app/interfaces/controller/category.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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{
Expand Down
7 changes: 4 additions & 3 deletions app/interfaces/controller/comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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{
Expand Down
7 changes: 4 additions & 3 deletions app/interfaces/controller/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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{
Expand Down
7 changes: 4 additions & 3 deletions app/interfaces/controller/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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{
Expand Down
9 changes: 4 additions & 5 deletions app/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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())
Expand Down

0 comments on commit 51051f5

Please sign in to comment.