resolved reqpreprocessing review comments
1. added logs for contextKey parsing and unmarshaling yml file. 2. resolved router test case issue. 2. resolved logger test case issues.
This commit is contained in:
@@ -12,6 +12,7 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/beckn/beckn-onix/pkg/model"
|
||||
"github.com/rs/zerolog"
|
||||
"gopkg.in/natefinch/lumberjack.v2"
|
||||
)
|
||||
@@ -52,9 +53,9 @@ var logLevels = map[level]zerolog.Level{
|
||||
|
||||
// Config represents the configuration for logging.
|
||||
type Config struct {
|
||||
Level level `yaml:"level"`
|
||||
Destinations []destination `yaml:"destinations"`
|
||||
ContextKeys []string `yaml:"contextKeys"`
|
||||
Level level `yaml:"level"`
|
||||
Destinations []destination `yaml:"destinations"`
|
||||
ContextKeys []model.ContextKey `yaml:"contextKeys"`
|
||||
}
|
||||
|
||||
var (
|
||||
@@ -277,7 +278,7 @@ func addCtx(ctx context.Context, event *zerolog.Event) {
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
keyStr := key
|
||||
keyStr := string(key)
|
||||
event.Any(keyStr, val)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,6 +13,8 @@ import (
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/beckn/beckn-onix/pkg/model"
|
||||
)
|
||||
|
||||
type ctxKey any
|
||||
@@ -63,7 +65,12 @@ func setupLogger(t *testing.T, l level) string {
|
||||
},
|
||||
},
|
||||
},
|
||||
ContextKeys: []string{"userID", "requestID"},
|
||||
ContextKeys: []model.ContextKey{
|
||||
model.ContextKeyTxnID,
|
||||
model.ContextKeyMsgID,
|
||||
model.ContextKeySubscriberID,
|
||||
model.ContextKeyModelID,
|
||||
},
|
||||
}
|
||||
|
||||
// Initialize logger with the given config
|
||||
@@ -97,16 +104,16 @@ func parseLogLine(t *testing.T, line string) map[string]interface{} {
|
||||
func TestDebug(t *testing.T) {
|
||||
t.Helper()
|
||||
logPath := setupLogger(t, DebugLevel)
|
||||
ctx := context.WithValue(context.Background(), userID, "12345")
|
||||
ctx := context.WithValue(context.Background(), model.ContextKeySubscriberID, "12345")
|
||||
Debug(ctx, "Debug message")
|
||||
lines := readLogFile(t, logPath)
|
||||
if len(lines) == 0 {
|
||||
t.Fatal("No logs were written.")
|
||||
}
|
||||
expected := map[string]interface{}{
|
||||
"level": "debug",
|
||||
"userID": "12345",
|
||||
"message": "Debug message",
|
||||
"level": "debug",
|
||||
"subscriber_id": "12345",
|
||||
"message": "Debug message",
|
||||
}
|
||||
|
||||
var found bool
|
||||
@@ -129,16 +136,16 @@ func TestDebug(t *testing.T) {
|
||||
|
||||
func TestInfo(t *testing.T) {
|
||||
logPath := setupLogger(t, InfoLevel)
|
||||
ctx := context.WithValue(context.Background(), userID, "12345")
|
||||
ctx := context.WithValue(context.Background(), model.ContextKeySubscriberID, "12345")
|
||||
Info(ctx, "Info message")
|
||||
lines := readLogFile(t, logPath)
|
||||
if len(lines) == 0 {
|
||||
t.Fatal("No logs were written.")
|
||||
}
|
||||
expected := map[string]interface{}{
|
||||
"level": "info",
|
||||
"userID": "12345",
|
||||
"message": "Info message",
|
||||
"level": "info",
|
||||
"subscriber_id": "12345",
|
||||
"message": "Info message",
|
||||
}
|
||||
|
||||
var found bool
|
||||
@@ -161,16 +168,16 @@ func TestInfo(t *testing.T) {
|
||||
|
||||
func TestWarn(t *testing.T) {
|
||||
logPath := setupLogger(t, WarnLevel)
|
||||
ctx := context.WithValue(context.Background(), userID, "12345")
|
||||
ctx := context.WithValue(context.Background(), model.ContextKeySubscriberID, "12345")
|
||||
Warn(ctx, "Warning message")
|
||||
lines := readLogFile(t, logPath)
|
||||
if len(lines) == 0 {
|
||||
t.Fatal("No logs were written.")
|
||||
}
|
||||
expected := map[string]interface{}{
|
||||
"level": "warn",
|
||||
"userID": "12345",
|
||||
"message": "Warning message",
|
||||
"level": "warn",
|
||||
"subscriber_id": "12345",
|
||||
"message": "Warning message",
|
||||
}
|
||||
|
||||
var found bool
|
||||
@@ -189,17 +196,17 @@ func TestWarn(t *testing.T) {
|
||||
|
||||
func TestError(t *testing.T) {
|
||||
logPath := setupLogger(t, ErrorLevel)
|
||||
ctx := context.WithValue(context.Background(), userID, "12345")
|
||||
ctx := context.WithValue(context.Background(), model.ContextKeySubscriberID, "12345")
|
||||
Error(ctx, fmt.Errorf("test error"), "Error message")
|
||||
lines := readLogFile(t, logPath)
|
||||
if len(lines) == 0 {
|
||||
t.Fatal("No logs were written.")
|
||||
}
|
||||
expected := map[string]interface{}{
|
||||
"level": "error",
|
||||
"userID": "12345",
|
||||
"message": "Error message",
|
||||
"error": "test error",
|
||||
"level": "error",
|
||||
"subscriber_id": "12345",
|
||||
"message": "Error message",
|
||||
"error": "test error",
|
||||
}
|
||||
|
||||
var found bool
|
||||
@@ -277,17 +284,17 @@ func TestResponse(t *testing.T) {
|
||||
|
||||
func TestFatal(t *testing.T) {
|
||||
logPath := setupLogger(t, FatalLevel)
|
||||
ctx := context.WithValue(context.Background(), userID, "12345")
|
||||
ctx := context.WithValue(context.Background(), model.ContextKeySubscriberID, "12345")
|
||||
Fatal(ctx, fmt.Errorf("fatal error"), "Fatal message")
|
||||
lines := readLogFile(t, logPath)
|
||||
if len(lines) == 0 {
|
||||
t.Fatal("No logs were written.")
|
||||
}
|
||||
expected := map[string]interface{}{
|
||||
"level": "fatal",
|
||||
"userID": "12345",
|
||||
"message": "Fatal message",
|
||||
"error": "fatal error",
|
||||
"level": "fatal",
|
||||
"subscriber_id": "12345",
|
||||
"message": "Fatal message",
|
||||
"error": "fatal error",
|
||||
}
|
||||
|
||||
var found bool
|
||||
@@ -308,17 +315,17 @@ func TestFatal(t *testing.T) {
|
||||
|
||||
func TestPanic(t *testing.T) {
|
||||
logPath := setupLogger(t, PanicLevel)
|
||||
ctx := context.WithValue(context.Background(), userID, "12345")
|
||||
ctx := context.WithValue(context.Background(), model.ContextKeySubscriberID, "12345")
|
||||
Panic(ctx, fmt.Errorf("panic error"), "Panic message")
|
||||
lines := readLogFile(t, logPath)
|
||||
if len(lines) == 0 {
|
||||
t.Fatal("No logs were written.")
|
||||
}
|
||||
expected := map[string]interface{}{
|
||||
"level": "panic",
|
||||
"userID": "12345",
|
||||
"message": "Panic message",
|
||||
"error": "panic error",
|
||||
"level": "panic",
|
||||
"subscriber_id": "12345",
|
||||
"message": "Panic message",
|
||||
"error": "panic error",
|
||||
}
|
||||
|
||||
var found bool
|
||||
@@ -339,16 +346,16 @@ func TestPanic(t *testing.T) {
|
||||
|
||||
func TestDebugf(t *testing.T) {
|
||||
logPath := setupLogger(t, DebugLevel)
|
||||
ctx := context.WithValue(context.Background(), userID, "12345")
|
||||
ctx := context.WithValue(context.Background(), model.ContextKeySubscriberID, "12345")
|
||||
Debugf(ctx, "Debugf message: %s", "test")
|
||||
lines := readLogFile(t, logPath)
|
||||
if len(lines) == 0 {
|
||||
t.Fatal("No logs were written.")
|
||||
}
|
||||
expected := map[string]interface{}{
|
||||
"level": "debug",
|
||||
"userID": "12345",
|
||||
"message": "Debugf message: test",
|
||||
"level": "debug",
|
||||
"subscriber_id": "12345",
|
||||
"message": "Debugf message: test",
|
||||
}
|
||||
|
||||
var found bool
|
||||
@@ -370,16 +377,16 @@ func TestDebugf(t *testing.T) {
|
||||
|
||||
func TestInfof(t *testing.T) {
|
||||
logPath := setupLogger(t, InfoLevel)
|
||||
ctx := context.WithValue(context.Background(), userID, "12345")
|
||||
ctx := context.WithValue(context.Background(), model.ContextKeySubscriberID, "12345")
|
||||
Infof(ctx, "Infof message: %s", "test")
|
||||
lines := readLogFile(t, logPath)
|
||||
if len(lines) == 0 {
|
||||
t.Fatal("No logs were written.")
|
||||
}
|
||||
expected := map[string]interface{}{
|
||||
"level": "info",
|
||||
"userID": "12345",
|
||||
"message": "Infof message: test",
|
||||
"level": "info",
|
||||
"subscriber_id": "12345",
|
||||
"message": "Infof message: test",
|
||||
}
|
||||
|
||||
var found bool
|
||||
@@ -400,16 +407,16 @@ func TestInfof(t *testing.T) {
|
||||
|
||||
func TestWarnf(t *testing.T) {
|
||||
logPath := setupLogger(t, WarnLevel)
|
||||
ctx := context.WithValue(context.Background(), userID, "12345")
|
||||
ctx := context.WithValue(context.Background(), model.ContextKeySubscriberID, "12345")
|
||||
Warnf(ctx, "Warnf message: %s", "test")
|
||||
lines := readLogFile(t, logPath)
|
||||
if len(lines) == 0 {
|
||||
t.Fatal("No logs were written.")
|
||||
}
|
||||
expected := map[string]interface{}{
|
||||
"level": "warn",
|
||||
"userID": "12345",
|
||||
"message": "Warnf message: test",
|
||||
"level": "warn",
|
||||
"subscriber_id": "12345",
|
||||
"message": "Warnf message: test",
|
||||
}
|
||||
|
||||
var found bool
|
||||
@@ -430,7 +437,7 @@ func TestWarnf(t *testing.T) {
|
||||
|
||||
func TestErrorf(t *testing.T) {
|
||||
logPath := setupLogger(t, ErrorLevel)
|
||||
ctx := context.WithValue(context.Background(), userID, "12345")
|
||||
ctx := context.WithValue(context.Background(), model.ContextKeySubscriberID, "12345")
|
||||
err := fmt.Errorf("error message")
|
||||
Errorf(ctx, err, "Errorf message: %s", "test")
|
||||
lines := readLogFile(t, logPath)
|
||||
@@ -438,10 +445,10 @@ func TestErrorf(t *testing.T) {
|
||||
t.Fatal("No logs were written.")
|
||||
}
|
||||
expected := map[string]interface{}{
|
||||
"level": "error",
|
||||
"userID": "12345",
|
||||
"message": "Errorf message: test",
|
||||
"error": "error message",
|
||||
"level": "error",
|
||||
"subscriber_id": "12345",
|
||||
"message": "Errorf message: test",
|
||||
"error": "error message",
|
||||
}
|
||||
|
||||
var found bool
|
||||
@@ -462,7 +469,7 @@ func TestErrorf(t *testing.T) {
|
||||
|
||||
func TestFatalf(t *testing.T) {
|
||||
logPath := setupLogger(t, FatalLevel)
|
||||
ctx := context.WithValue(context.Background(), userID, "12345")
|
||||
ctx := context.WithValue(context.Background(), model.ContextKeySubscriberID, "12345")
|
||||
err := fmt.Errorf("fatal error")
|
||||
Fatalf(ctx, err, "Fatalf message: %s", "test")
|
||||
lines := readLogFile(t, logPath)
|
||||
@@ -470,10 +477,10 @@ func TestFatalf(t *testing.T) {
|
||||
t.Fatal("No logs were written.")
|
||||
}
|
||||
expected := map[string]interface{}{
|
||||
"level": "fatal",
|
||||
"userID": "12345",
|
||||
"message": "Fatalf message: test",
|
||||
"error": "fatal error",
|
||||
"level": "fatal",
|
||||
"subscriber_id": "12345",
|
||||
"message": "Fatalf message: test",
|
||||
"error": "fatal error",
|
||||
}
|
||||
|
||||
var found bool
|
||||
@@ -494,7 +501,7 @@ func TestFatalf(t *testing.T) {
|
||||
|
||||
func TestPanicf(t *testing.T) {
|
||||
logPath := setupLogger(t, PanicLevel)
|
||||
ctx := context.WithValue(context.Background(), userID, "12345")
|
||||
ctx := context.WithValue(context.Background(), model.ContextKeySubscriberID, "12345")
|
||||
err := fmt.Errorf("panic error")
|
||||
Panicf(ctx, err, "Panicf message: %s", "test")
|
||||
lines := readLogFile(t, logPath)
|
||||
@@ -503,10 +510,10 @@ func TestPanicf(t *testing.T) {
|
||||
t.Fatal("No logs were written.")
|
||||
}
|
||||
expected := map[string]interface{}{
|
||||
"level": "panic",
|
||||
"userID": "12345",
|
||||
"message": "Panicf message: test",
|
||||
"error": "panic error",
|
||||
"level": "panic",
|
||||
"subscriber_id": "12345",
|
||||
"message": "Panicf message: test",
|
||||
"error": "panic error",
|
||||
}
|
||||
|
||||
var found bool
|
||||
|
||||
Reference in New Issue
Block a user