Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1 - Broadcast - Slog wrapper #198

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 110 additions & 0 deletions logging/logging.go
Original file line number Diff line number Diff line change
@@ -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)
}
Loading