forked from zeta-chain/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog_handler.go
More file actions
53 lines (44 loc) · 1.25 KB
/
log_handler.go
File metadata and controls
53 lines (44 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package server
import (
"context"
"log/slog"
"cosmossdk.io/log"
)
// CustomSlogHandler bridges Geth's slog logs to the existing Cosmos SDK logger.
type CustomSlogHandler struct {
logger log.Logger
}
// Handle processes slog records and forwards them to your Cosmos SDK logger.
func (h *CustomSlogHandler) Handle(_ context.Context, r slog.Record) error {
attrs := []interface{}{}
r.Attrs(func(attr slog.Attr) bool {
attrs = append(attrs, attr.Key, attr.Value.Any())
return true
})
// Map slog levels to Cosmos SDK logger
switch r.Level {
case slog.LevelDebug:
h.logger.Debug(r.Message, attrs...)
case slog.LevelInfo:
h.logger.Info(r.Message, attrs...)
case slog.LevelWarn:
h.logger.Warn(r.Message, attrs...)
case slog.LevelError:
h.logger.Error(r.Message, attrs...)
default:
h.logger.Info(r.Message, attrs...)
}
return nil
}
// Enabled determines if the handler should log a given level.
func (h *CustomSlogHandler) Enabled(_ context.Context, _ slog.Level) bool {
return true
}
// WithAttrs allows adding additional attributes.
func (h *CustomSlogHandler) WithAttrs(_ []slog.Attr) slog.Handler {
return h
}
// WithGroup is required to implement slog.Handler (not used).
func (h *CustomSlogHandler) WithGroup(_ string) slog.Handler {
return h
}