diff --git a/lib/message/message.go b/lib/message/message.go index 324e5c7..0df964e 100644 --- a/lib/message/message.go +++ b/lib/message/message.go @@ -92,12 +92,23 @@ type payload struct { data interface{} } -func (pl payload) String() string { +func (pl *payload) String() string { b, err := json.Marshal(pl.data) - log.Err(err) + if err != nil { + log.Error().Err(err).Msg("payload.String()") + } return string(b) } func Payload(d interface{}) lib.Payload { - return payload{d} + return &payload{d} +} + +func FromJson[T any](s string) lib.Payload { + pl := &payload{new(T)} + err := json.Unmarshal([]byte(s), &pl.data) + if err != nil { + log.Error().Err(err).Msg("payload: FromJson()") + } + return pl } diff --git a/logging/log/log.go b/logging/log/log.go index a2367bd..9fe203d 100644 --- a/logging/log/log.go +++ b/logging/log/log.go @@ -16,5 +16,4 @@ var ( Info = logging.GetLogger(nil).Info Warn = logging.GetLogger(nil).Warn Error = logging.GetLogger(nil).Error - Err = logging.GetLogger(nil).Err ) diff --git a/logging/logging.go b/logging/logging.go index 8649138..4c3238a 100644 --- a/logging/logging.go +++ b/logging/logging.go @@ -28,6 +28,7 @@ var Levels = map[string]zerolog.Level{ type Logger = zerolog.Logger +var defaultLogger = log.Logger var globalLogger *Logger = &log.Logger type Cfg struct { @@ -36,6 +37,10 @@ type Cfg struct { Level string } +func SetDefault() { + globalLogger = &defaultLogger +} + func Setup(ctx lib.Context, cfg *Cfg, home string) { if cfg == nil { return // use unchanged predefined logger diff --git a/testing/testing.go b/testing/testing.go index 0ffd76d..777beb9 100644 --- a/testing/testing.go +++ b/testing/testing.go @@ -38,6 +38,7 @@ func MakeT(tbase *testing.T) *T { func SetUp(tbase *testing.T) *T { t := MakeT(tbase) + logging.SetDefault() return t } diff --git a/tests/unit_test.go b/tests/unit_test.go index 61d8be2..3b1e303 100644 --- a/tests/unit_test.go +++ b/tests/unit_test.go @@ -54,4 +54,6 @@ func PayloadTest(t *testing.T) { pl2 := message.Payload(map[string]string{"activity": "development"}) t.AssertEqual(fmt.Sprint(pl2), `{"activity":"development"}`) msg.WithPayload(pl2) + pl3 := message.FromJson[map[string]any](`{"activity": "development"}`) + t.AssertEqual(fmt.Sprint(pl3), `{"activity":"development"}`) }