-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7e90894
commit 483246d
Showing
23 changed files
with
269 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package restful | ||
|
||
import ( | ||
"io" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
func (s *Server) Logger() gin.HandlerFunc { | ||
return func(c *gin.Context) { | ||
// process request | ||
startTime := time.Now() | ||
c.Next() | ||
duration := time.Since(startTime) | ||
|
||
// log | ||
logger := s.logger.Info() | ||
if c.Writer.Status() >= 500 { | ||
logger = s.logger.Error() | ||
if c.Request != nil && c.Request.Body != nil { | ||
if body, err := io.ReadAll(c.Request.Body); err == nil { | ||
logger.Field("body", body) | ||
} | ||
} | ||
} | ||
logger. | ||
Field("protocol", "http"). | ||
Field("method", c.Request.Method). | ||
Field("path", c.Request.URL.Path). | ||
Field("status_code", c.Writer.Status()). | ||
Field("status", http.StatusText(c.Writer.Status())). | ||
Field("duration", duration). | ||
Msg("received http request") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package logger | ||
|
||
import ( | ||
"github.com/labasubagia/realworld-backend/internal/core/port" | ||
"github.com/labasubagia/realworld-backend/internal/core/util" | ||
) | ||
|
||
func NewLogger(config util.Config) port.Logger { | ||
return NewZeroLogLogger(config) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package logger | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"time" | ||
|
||
"github.com/labasubagia/realworld-backend/internal/core/port" | ||
"github.com/labasubagia/realworld-backend/internal/core/util" | ||
"github.com/rs/zerolog" | ||
) | ||
|
||
type zeroLogLogger struct { | ||
log zerolog.Logger | ||
event *zerolog.Event | ||
} | ||
|
||
func NewZeroLogLogger(config util.Config) port.Logger { | ||
logger := zerolog.New(os.Stderr) | ||
if !config.IsProduction() { | ||
output := zerolog.ConsoleWriter{Out: os.Stderr, TimeFormat: time.RFC3339} | ||
logger = zerolog.New(output).With().Timestamp().Logger() | ||
} | ||
return &zeroLogLogger{ | ||
log: logger, | ||
event: logger.Info(), | ||
} | ||
} | ||
|
||
// Log Level | ||
|
||
func (l *zeroLogLogger) Info() port.Logger { | ||
l.event = l.log.Info() | ||
return l | ||
} | ||
|
||
func (l *zeroLogLogger) Error() port.Logger { | ||
l.event = l.log.Error() | ||
return l | ||
} | ||
|
||
func (l *zeroLogLogger) Fatal() port.Logger { | ||
l.event = l.log.Fatal() | ||
return l | ||
} | ||
|
||
// Set Attributes | ||
|
||
func (l *zeroLogLogger) Err(err error) port.Logger { | ||
l.event = l.event.Err(err) | ||
return l | ||
} | ||
|
||
func (l *zeroLogLogger) Field(key string, value any) port.Logger { | ||
l.event = l.event.Any(key, value) | ||
return l | ||
} | ||
|
||
// Send | ||
|
||
func (l *zeroLogLogger) Msg(v ...any) { | ||
l.event.Msg(fmt.Sprint(v...)) | ||
} | ||
|
||
func (l *zeroLogLogger) Msgf(format string, v ...any) { | ||
l.event.Msgf(format, v...) | ||
} |
Oops, something went wrong.