Skip to content

Commit

Permalink
fix(logging.go): configureDefaultLoggingSetup: added param stackPathS…
Browse files Browse the repository at this point in the history
…plitter

this is to make stack path splitting generic regardles of project name
  • Loading branch information
Adam Bezecny committed Nov 6, 2023
1 parent a7fce16 commit 6fb55ac
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 19 deletions.
4 changes: 2 additions & 2 deletions aws/dynamodb/dist_lock/dist_lock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const (

func TstDistributedLockWithDynamoDbHappyPath(t *testing.T) {
t.Helper()
logging.ConfigureDefaultLoggingSetup()
logging.ConfigureDefaultLoggingSetup("")
ctx := context.Background()
c := test.SetupLocalstack(ctx)
ctx = aws.SetCustomAwsEndpoint(ctx, c.URI)
Expand Down Expand Up @@ -89,7 +89,7 @@ func TstDistributedLockWithDynamoDbHappyPath(t *testing.T) {

func TstDistributedLockWithDynamoDbUnhappyPathTimeout(t *testing.T) {
t.Helper()
logging.ConfigureDefaultLoggingSetup()
logging.ConfigureDefaultLoggingSetup("")
ctx := context.Background()
c := test.SetupLocalstack(ctx)
ctx = aws.SetCustomAwsEndpoint(ctx, c.URI)
Expand Down
23 changes: 23 additions & 0 deletions errors/rest_api_error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
errorHelper "errors"
"fmt"
"github.com/gin-gonic/gin"
"github.com/hrsupersport/hrnogomet-backend-kit/logging"
"github.com/rotisserie/eris"
"github.com/rs/zerolog/log"
"github.com/stretchr/testify/assert"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -117,3 +119,24 @@ func TestTranslateToHttpErrorNormalError(t *testing.T) {
TranslateServiceErrorToAPIError(c, serviceError, includeErrorDetails)
assert.Equal(t, http.StatusInternalServerError, w.Code)
}

func TestErrorStackLogging(t *testing.T) {
//gin.SetMode(gin.TestMode)
logging.ConfigureDefaultLoggingSetup("hrnogomet-backend-kit")

serviceError := NewServiceErrorNotFound(errorHelper.New("fromBackend404"), "custom404")

/* contains absolute paths
custom404
errors.TestErrorStackLogging:/Users/adambezecny/dev/hrnogomet-backend-kit/errors/rest_api_error_test.go:127
errors.NewServiceErrorNotFound:/Users/adambezecny/dev/hrnogomet-backend-kit/errors/service_error.go:142
errors.newServiceError:/Users/adambezecny/dev/hrnogomet-backend-kit/errors/service_error.go:133
fromBackend404
*/
fmt.Println(eris.ToString(serviceError, true))

/* contains relative paths thanks to setting stackPathSplitter in logging.ConfigureDefaultLoggingSetup call!
{"L":"error","stack":[{"func":"errors.newServiceError","source":"/errors/service_error.go:133"},{"func":"errors.NewServiceErrorNotFound","source":"/errors/service_error.go:142"},{"func":"errors.TestErrorStackLogging","source":"/errors/rest_api_error_test.go:127"}],"error":"custom404: fromBackend404","T":"2023-11-06T09:35:45.36983+01:00","caller":"rest_api_error_test.go:129"}
*/
log.Error().Stack().Err(serviceError).Msg("")
}
36 changes: 19 additions & 17 deletions logging/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,30 +42,32 @@ func ConfigureCommonFieldsInLogMessages() {
}

// ConfigureDefaultLoggingSetup should be used in main file to configure common logging setup
func ConfigureDefaultLoggingSetup() {
func ConfigureDefaultLoggingSetup(stackPathSplitter string) {
ConfigureShortFileNameInLogMessages()
ConfigureCommonFieldsInLogMessages()
// ConfigureLoggingMetrics()
zerolog.SetGlobalLevel(zerolog.InfoLevel)
zerolog.ErrorStackMarshaler = MarshalStack
zerolog.ErrorStackMarshaler = MarshalStackFnCreator(stackPathSplitter)
log.Logger = log.Output(os.Stdout)
}

func MarshalStack(err error) interface{} {
ue := eris.Unpack(err)
out := make([]map[string]string, 0, len(ue.ErrRoot.Stack))
for _, frame := range ue.ErrRoot.Stack {
// stop processing for stack not from hrnogomet
// TODO: do we need this here?
parsedPath := strings.Split(frame.File, "hrnogomet-api")
if len(parsedPath) < 2 {
break
func MarshalStackFnCreator(stackPathSplitter string) func(error) interface{} {
return func(err error) interface{} {
ue := eris.Unpack(err)
out := make([]map[string]string, 0, len(ue.ErrRoot.Stack))
for _, frame := range ue.ErrRoot.Stack {
// split the path so that it begins with project folder only, e.g. internal/...
// instead of including absolute path like /Users/xx/project/yy/internal/...
parsedPath := strings.Split(frame.File, stackPathSplitter)
if len(parsedPath) < 2 {
break
}
file := fmt.Sprintf("%s:%d", parsedPath[len(parsedPath)-1], frame.Line)
out = append(out, map[string]string{
"source": file,
"func": frame.Name,
})
}
file := fmt.Sprintf("%s:%d", parsedPath[len(parsedPath)-1], frame.Line)
out = append(out, map[string]string{
"source": file,
"func": frame.Name,
})
return out
}
return out
}

0 comments on commit 6fb55ac

Please sign in to comment.