Skip to content
This repository was archived by the owner on Dec 23, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 1 addition & 14 deletions src/infrastructure/LogrusHttpLogDbHook.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"rol/app/interfaces"
"rol/app/utils"
"rol/domain"
"strings"
"sync"
"time"

Expand All @@ -20,9 +19,6 @@ import (
//HTTPBufSize buffer size for async hooks
var HTTPBufSize uint = 8192

const httpAPIName = "httplog"
const appAPIName = "applog"

//HTTPHook log hook struct
type HTTPHook struct {
repo interfaces.IGenericRepository[uuid.UUID, domain.HTTPLog]
Expand All @@ -41,7 +37,7 @@ type HTTPAsyncHook struct {
}

var httpInsertFunc = func(entry *logrus.Entry, repository interfaces.IGenericRepository[uuid.UUID, domain.HTTPLog]) error {
if entry.Data["method"] != nil && !fromLogController(entry) {
if entry.Data["method"] != nil {
ent := newEntityFromHTTP(entry)
_, err := repository.Insert(nil, *ent)
if err != nil {
Expand Down Expand Up @@ -210,12 +206,3 @@ func (h *HTTPHook) Levels() []logrus.Level {
logrus.DebugLevel,
}
}

func fromLogController(entry *logrus.Entry) bool {
if entry.Data["path"] == nil {
return false
} else if strings.Contains(entry.Data["path"].(string), httpAPIName) || strings.Contains(entry.Data["path"].(string), appAPIName) {
return true
}
return false
}
21 changes: 20 additions & 1 deletion src/infrastructure/LogrusLogDbHook.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"rol/app/errors"
"rol/app/interfaces"
"rol/domain"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -35,7 +36,7 @@ type AppAsyncHook struct {
}

var insertAppFunc = func(entry *logrus.Entry, repository interfaces.IGenericRepository[uuid.UUID, domain.AppLog]) error {
if entry.Data["method"] == nil && !fromLogController(entry) {
if entry.Data["method"] == nil {
ent := newEntityFromApp(entry)
_, err := repository.Insert(nil, *ent)
if err != nil {
Expand All @@ -59,12 +60,30 @@ var asyncAppInsertFunc = func(entry *logrus.Entry, repository interfaces.IGeneri
func newEntityFromApp(entry *logrus.Entry) *domain.AppLog {
var actionID uuid.UUID
var source string
var messagePrefix string
if entry.Data["actionID"] != nil {
actionID = entry.Data["actionID"].(uuid.UUID)
}
if entry.Data["source"] != nil {
source = entry.Data["source"].(string)
}

var sb strings.Builder
if _, ok := entry.Data["file"]; ok {
sb.WriteString(entry.Data["file"].(string) + ":")
}
if _, ok := entry.Data["line"]; ok {
line := entry.Data["line"].(int)
sb.WriteString(fmt.Sprintf("%d", line))
}
if sb.String() != "" {
messagePrefix = sb.String() + "\n"
}
if source == "" {
source = sb.String()
} else {
entry.Message = messagePrefix + entry.Message
}
return &domain.AppLog{
EntityUUID: domain.EntityUUID{
ID: uuid.New(),
Expand Down
2 changes: 1 addition & 1 deletion src/webapi/GinHttpServer.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type GinHTTPServer struct {
func NewGinHTTPServer(log *logrus.Logger, config *domain.AppConfig) *GinHTTPServer {
ginEngine := gin.New()
url := ginSwagger.URL("swagger/doc.json")
ginEngine.Use(middleware.Logger(log), middleware.Recovery(log))
ginEngine.Use(middleware.Logger(log, "/swagger/", "/log/"), middleware.Recovery(log))
ginEngine.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler, url))
return &GinHTTPServer{
Engine: ginEngine,
Expand Down
4 changes: 2 additions & 2 deletions src/webapi/controllers/GinHelpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func validationErrorToValidationErrorDto(err error) dtos.ValidationErrorDto {
//if error = nil, then abort with http.StatusInternalServerError
func abortWithStatusByErrorType(ctx *gin.Context, err error) {
if errors.As(err, errors.Internal) || errors.As(err, errors.NoType) {
ctx.AbortWithStatus(http.StatusInternalServerError)
_ = ctx.AbortWithError(http.StatusInternalServerError, err)
return
} else if errors.As(err, errors.Validation) {
ctx.JSON(http.StatusBadRequest, validationErrorToValidationErrorDto(err))
Expand All @@ -72,7 +72,7 @@ func abortWithStatusByErrorType(ctx *gin.Context, err error) {
ctx.AbortWithStatus(http.StatusNotFound)
return
}
ctx.AbortWithStatus(http.StatusInternalServerError)
_ = ctx.AbortWithError(http.StatusInternalServerError, err)
}

//handle current request by error type, if error = nil, set http status to 204 (No Content)
Expand Down
Loading