Bootstrap copied BFF and local accounts with public Sendico contracts (#34)
This commit is contained in:
1 parent
67dc5b5b1e
commit
7062672a87
556 files changed
+42291
-777
No files matched your search
@@ -0,0 +1,10 @@
|
||||
package mlogger
|
||||
|
||||
import (
|
||||
lt "git.sendico.io/tech/sxchnge/bff/pkg/mlogger"
|
||||
li "git.sendico.io/tech/sxchnge/bff/pkg/mlogger/internal/mlogger"
|
||||
)
|
||||
|
||||
func NewLogger(debug bool) lt.Logger {
|
||||
return li.NewLoggerImp(debug)
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package mlogger
|
||||
|
||||
import (
|
||||
"git.sendico.io/tech/sxchnge/bff/pkg/mlogger"
|
||||
"github.com/mattn/go-colorable"
|
||||
"go.uber.org/zap"
|
||||
"go.uber.org/zap/zapcore"
|
||||
)
|
||||
|
||||
func NewLoggerImp(debug bool) mlogger.Logger {
|
||||
if debug {
|
||||
dc := zap.NewDevelopmentEncoderConfig()
|
||||
dc.EncodeLevel = zapcore.CapitalColorLevelEncoder
|
||||
logger := zap.New(zapcore.NewCore(
|
||||
zapcore.NewConsoleEncoder(dc),
|
||||
zapcore.AddSync(colorable.NewColorableStdout()),
|
||||
zapcore.DebugLevel,
|
||||
))
|
||||
return logger
|
||||
}
|
||||
|
||||
return zap.Must(zap.NewProduction())
|
||||
}
|
||||
@@ -0,0 +1,119 @@
|
||||
package mlogger
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
type Logger = *zap.Logger
|
||||
|
||||
// Named appends logger name components while avoiding adjacent or already-present
|
||||
// name stutter such as "notification.notification" or "vault.vault.kv".
|
||||
func Named(logger Logger, names ...any) Logger {
|
||||
if logger == nil {
|
||||
logger = zap.NewNop()
|
||||
}
|
||||
|
||||
current := splitName(logger.Name())
|
||||
for _, name := range names {
|
||||
if name == nil {
|
||||
continue
|
||||
}
|
||||
parts := splitName(fmt.Sprint(name))
|
||||
if len(parts) == 0 {
|
||||
continue
|
||||
}
|
||||
if containsParts(current, parts) {
|
||||
continue
|
||||
}
|
||||
|
||||
parts = trimOverlappingPrefix(current, parts)
|
||||
parts = trimStutteringPrefix(current, parts)
|
||||
if len(parts) == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
child := strings.Join(parts, ".")
|
||||
logger = logger.Named(child)
|
||||
current = append(current, parts...)
|
||||
}
|
||||
|
||||
return logger
|
||||
}
|
||||
|
||||
func splitName(name string) []string {
|
||||
raw := strings.Split(name, ".")
|
||||
parts := make([]string, 0, len(raw))
|
||||
for _, part := range raw {
|
||||
part = strings.TrimSpace(part)
|
||||
if part == "" {
|
||||
continue
|
||||
}
|
||||
parts = append(parts, part)
|
||||
}
|
||||
return parts
|
||||
}
|
||||
|
||||
func containsParts(parent, child []string) bool {
|
||||
if len(parent) == 0 || len(child) == 0 || len(child) > len(parent) {
|
||||
return false
|
||||
}
|
||||
for i := 0; i <= len(parent)-len(child); i++ {
|
||||
matches := true
|
||||
for j := range child {
|
||||
if parent[i+j] != child[j] {
|
||||
matches = false
|
||||
break
|
||||
}
|
||||
}
|
||||
if matches {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func trimOverlappingPrefix(parent, child []string) []string {
|
||||
max := len(parent)
|
||||
if len(child) < max {
|
||||
max = len(child)
|
||||
}
|
||||
for n := max; n > 0; n-- {
|
||||
matches := true
|
||||
for i := 0; i < n; i++ {
|
||||
if parent[len(parent)-n+i] != child[i] {
|
||||
matches = false
|
||||
break
|
||||
}
|
||||
}
|
||||
if matches {
|
||||
return child[n:]
|
||||
}
|
||||
}
|
||||
return child
|
||||
}
|
||||
|
||||
func trimStutteringPrefix(parent, child []string) []string {
|
||||
if len(parent) == 0 || len(child) == 0 {
|
||||
return child
|
||||
}
|
||||
|
||||
last := parent[len(parent)-1]
|
||||
first := child[0]
|
||||
for _, separator := range []string{"_", "-"} {
|
||||
prefix := last + separator
|
||||
if !strings.HasPrefix(first, prefix) {
|
||||
continue
|
||||
}
|
||||
trimmed := strings.TrimSpace(strings.TrimPrefix(first, prefix))
|
||||
if trimmed == "" {
|
||||
return child[1:]
|
||||
}
|
||||
out := append([]string{trimmed}, child[1:]...)
|
||||
return out
|
||||
}
|
||||
|
||||
return child
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
package mlogger
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
"go.uber.org/zap"
|
||||
"go.uber.org/zap/zaptest/observer"
|
||||
)
|
||||
|
||||
func TestNamedAvoidsLoggerNameStutter(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
base string
|
||||
add []any
|
||||
want string
|
||||
}{
|
||||
{
|
||||
name: "skips duplicate service name",
|
||||
base: "notification",
|
||||
add: []any{"notification"},
|
||||
want: "notification",
|
||||
},
|
||||
{
|
||||
name: "overlaps dotted component",
|
||||
base: "vault",
|
||||
add: []any{"vault.kv"},
|
||||
want: "vault.kv",
|
||||
},
|
||||
{
|
||||
name: "drops child prefix stutter",
|
||||
base: "server.client",
|
||||
add: []any{"client_v2"},
|
||||
want: "server.client.v2",
|
||||
},
|
||||
{
|
||||
name: "keeps already present module name",
|
||||
base: "payments.quotation.server",
|
||||
add: []any{"payments.quotation"},
|
||||
want: "payments.quotation.server",
|
||||
},
|
||||
{
|
||||
name: "extends partial overlap",
|
||||
base: "payments",
|
||||
add: []any{"payments.quotation"},
|
||||
want: "payments.quotation",
|
||||
},
|
||||
{
|
||||
name: "keeps unrelated names",
|
||||
base: "gateway.server",
|
||||
add: []any{"chain", "storage.mongo"},
|
||||
want: "gateway.server.chain.storage.mongo",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
observed, logs := observer.New(zap.InfoLevel)
|
||||
logger := Named(zap.New(observed).Named(tt.base), tt.add...)
|
||||
logger.Info("test")
|
||||
|
||||
entries := logs.All()
|
||||
require.Len(t, entries, 1)
|
||||
require.Equal(t, tt.want, entries[0].LoggerName)
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user