Skip to content
This repository has been archived by the owner on May 21, 2024. It is now read-only.

Commit

Permalink
move set logger to infra package for more intuitive
Browse files Browse the repository at this point in the history
  • Loading branch information
imantung committed Jan 25, 2021
1 parent af00f2f commit f253fe7
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 27 deletions.
7 changes: 3 additions & 4 deletions internal/app/infra/server.go → internal/app/infra/echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ package infra

import (
"github.com/labstack/echo/v4"
"github.com/typical-go/typical-rest-server/internal/app/infra/log"
"github.com/typical-go/typical-rest-server/pkg/logruskit"
)

// NewServer return new instance of server
// NewEcho return new instance of server
// @ctor
func NewServer(cfg *AppCfg) *echo.Echo {
func NewEcho(cfg *AppCfg) *echo.Echo {
e := echo.New()
logger := log.SetDebug(cfg.Debug)
logger := SetLogger(cfg.Debug)

e.HideBanner = true
e.Debug = cfg.Debug
Expand Down
18 changes: 0 additions & 18 deletions internal/app/infra/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,11 @@ package log

import (
"context"
"log"

"github.com/sirupsen/logrus"
"github.com/typical-go/typical-rest-server/pkg/logruskit"
)

var _debug bool

// SetDebug set logger
func SetDebug(debug bool) *logrus.Logger {
_debug = debug
logger := logrus.StandardLogger()
if debug {
logger.SetLevel(logrus.DebugLevel)
logger.SetFormatter(&logrus.TextFormatter{})
} else {
logger.SetLevel(logrus.WarnLevel)
logger.SetFormatter(&logrus.JSONFormatter{})
}
log.SetOutput(logger.Writer())
return logger
}

// Info ..
func Info(ctx context.Context, args ...interface{}) {
logrus.WithFields(logruskit.GetFields(ctx)).Info(args...)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package log
package infra

import (
"log"
"strconv"
"time"

Expand All @@ -10,8 +11,25 @@ import (
"github.com/typical-go/typical-rest-server/pkg/logruskit"
)

// Middleware log every request
func Middleware(next echo.HandlerFunc) echo.HandlerFunc {
var _debug bool

// SetLogger set logger
func SetLogger(debug bool) *logrus.Logger {
_debug = debug
logger := logrus.StandardLogger()
if debug {
logger.SetLevel(logrus.DebugLevel)
logger.SetFormatter(&logrus.TextFormatter{})
} else {
logger.SetLevel(logrus.WarnLevel)
logger.SetFormatter(&logrus.JSONFormatter{})
}
log.SetOutput(logger.Writer())
return logger
}

// LogMiddleware log every request
func LogMiddleware(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
req := c.Request()
res := c.Response()
Expand Down
4 changes: 2 additions & 2 deletions internal/app/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/typical-go/typical-rest-server/internal/app/controller"
"github.com/typical-go/typical-rest-server/internal/app/infra/log"
"github.com/typical-go/typical-rest-server/internal/app/infra"
"github.com/typical-go/typical-rest-server/pkg/echokit"
)

Expand All @@ -16,7 +16,7 @@ func SetServer(
) {

// set middleware
e.Use(log.Middleware)
e.Use(infra.LogMiddleware)
e.Use(middleware.Recover())

// set route
Expand Down

0 comments on commit f253fe7

Please sign in to comment.