From aefe0af56ab172d255a4647f1ff8d442ddf880ce Mon Sep 17 00:00:00 2001 From: Aleksander Vedvik Date: Sat, 11 Jan 2025 14:24:38 +0100 Subject: [PATCH 1/2] feat(Logging): implemented a slog wrapper - The broadcast implementation greatly relies on the logging feature - Type safety in logs is encouraged by the wrapper --- logging/logging.go | 110 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 logging/logging.go diff --git a/logging/logging.go b/logging/logging.go new file mode 100644 index 00000000..68f4289a --- /dev/null +++ b/logging/logging.go @@ -0,0 +1,110 @@ +package logging + +import ( + "log/slog" + "time" +) + +// LogEntry is used in slog with correct types and json mapping +type LogEntry struct { + Time time.Time `json:"time"` + Level string `json:"level"` + Msg string `json:"msg"` + MsgType string `json:"msgType"` + BroadcastID uint64 `json:"BroadcastID"` + Err error `json:"err"` + Method string `json:"method"` + From string `json:"from"` + Cancelled bool `json:"cancelled"` + MachineID uint64 `json:"MachineID"` + MsgID uint64 `json:"msgID"` + NodeID uint64 `json:"nodeID"` + NodeAddr string `json:"nodeAddr"` + Type string `json:"type"` + Reconnect bool `json:"reconnect"` + RetryNum float64 `json:"retryNum"` + MaxRetries int `json:"maxRetries"` + NumFailed int `json:"numFailed"` + Stopping bool `json:"stopping"` + IsBroadcastCall bool `json:"isBroadcastCall"` + Started time.Time `json:"started"` + Ended time.Time `json:"ended"` +} + +// the functions are used to get type safety on fields when logging: + +func MsgType(msgType string) slog.Attr { + return slog.String("msgType", msgType) +} + +func BroadcastID(broadcastID uint64) slog.Attr { + return slog.Uint64("BroadcastID", broadcastID) +} + +func Err(err error) slog.Attr { + return slog.Any("err", err) +} + +func Method(m string) slog.Attr { + return slog.String("method", m) +} + +func From(from string) slog.Attr { + return slog.String("from", from) +} + +func Cancelled(cancelled bool) slog.Attr { + return slog.Bool("cancelled", cancelled) +} + +func MachineID(machineID uint64) slog.Attr { + return slog.Uint64("MachineID", machineID) +} + +func MsgID(msgID uint64) slog.Attr { + return slog.Uint64("msgID", msgID) +} + +func NodeID(nodeID uint32) slog.Attr { + return slog.Uint64("nodeID", uint64(nodeID)) +} + +func NodeAddr(nodeAddr string) slog.Attr { + return slog.String("nodeAddr", nodeAddr) +} + +func Type(t string) slog.Attr { + return slog.String("type", t) +} + +func Reconnect(reconnect bool) slog.Attr { + return slog.Bool("reconnect", reconnect) +} + +func RetryNum(num float64) slog.Attr { + return slog.Float64("retryNum", num) +} + +func MaxRetries(maxRetries int) slog.Attr { + return slog.Int("maxRetries", maxRetries) +} + +func NumFailed(num int) slog.Attr { + return slog.Int("numFailed", num) +} + +func Stopping(stopping bool) slog.Attr { + return slog.Bool("stopping", stopping) +} + +func IsBroadcastCall(isBroadcastCall bool) slog.Attr { + return slog.Bool("isBroadcastCall", isBroadcastCall) +} + +func Started(started time.Time) slog.Attr { + return slog.Time("started", started) +} + +func Ended(ended time.Time) slog.Attr { + return slog.Time("ended", ended) +} From b99652fb7c17953404f94f903122728ebd019770 Mon Sep 17 00:00:00 2001 From: Aleksander Vedvik Date: Sat, 18 Jan 2025 12:58:42 +0100 Subject: [PATCH 2/2] refactor(Broadcast): correct casing --- logging/logging.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/logging/logging.go b/logging/logging.go index 68f4289a..f503ae4e 100644 --- a/logging/logging.go +++ b/logging/logging.go @@ -11,12 +11,12 @@ type LogEntry struct { Level string `json:"level"` Msg string `json:"msg"` MsgType string `json:"msgType"` - BroadcastID uint64 `json:"BroadcastID"` + BroadcastID uint64 `json:"broadcastID"` Err error `json:"err"` Method string `json:"method"` From string `json:"from"` Cancelled bool `json:"cancelled"` - MachineID uint64 `json:"MachineID"` + MachineID uint64 `json:"machineID"` MsgID uint64 `json:"msgID"` NodeID uint64 `json:"nodeID"` NodeAddr string `json:"nodeAddr"` @@ -38,7 +38,7 @@ func MsgType(msgType string) slog.Attr { } func BroadcastID(broadcastID uint64) slog.Attr { - return slog.Uint64("BroadcastID", broadcastID) + return slog.Uint64("broadcastID", broadcastID) } func Err(err error) slog.Attr { @@ -58,7 +58,7 @@ func Cancelled(cancelled bool) slog.Attr { } func MachineID(machineID uint64) slog.Attr { - return slog.Uint64("MachineID", machineID) + return slog.Uint64("machineID", machineID) } func MsgID(msgID uint64) slog.Attr {