feat: add log keys

This commit is contained in:
Kevin Franklin Kim 2022-01-03 13:29:45 +01:00
parent 1e7dc782c6
commit eebf8d3b6e
2 changed files with 24 additions and 6 deletions

18
log/fields_stream.go Normal file
View File

@ -0,0 +1,18 @@
package log
import (
"go.uber.org/zap"
)
const (
StreamQueueKey = "queue"
StreamSubjectKey = "subject"
)
func FStreamQueue(queue string) zap.Field {
return zap.String(StreamQueueKey, queue)
}
func FStreamSubject(name string) zap.Field {
return zap.String(StreamSubjectKey, name)
}

View File

@ -98,23 +98,23 @@ func New(l *zap.Logger, name, addr string, opts ...Option) (*Stream, error) {
// default options
natsOpts := append([]nats.Option{
nats.ErrorHandler(func(conn *nats.Conn, subscription *nats.Subscription, err error) {
l.Error("Nats server error", log.FError(err))
l.Error("nats error", log.FError(err), log.FStreamQueue(subscription.Queue), log.FStreamSubject(subscription.Subject))
}),
nats.ClosedHandler(func(conn *nats.Conn) {
if err := conn.LastError(); err != nil {
l.Error("Closed connection to nats server", log.FError(err))
l.Error("nats closed", log.FError(err))
} else {
l.Info("Closed connection to nats server")
l.Debug("nats closed")
}
}),
nats.ReconnectHandler(func(conn *nats.Conn) {
l.Info("Reconnected to nats server")
l.Debug("nats reconnected")
}),
nats.DisconnectErrHandler(func(conn *nats.Conn, err error) {
if err != nil {
l.Error("Disconnected from nats server", log.FError(err))
l.Error("nats disconnected", log.FError(err))
} else {
l.Info("Disconnected from nats server")
l.Debug("nats disconnected")
}
}),
nats.Timeout(time.Millisecond * 500),