-
Notifications
You must be signed in to change notification settings - Fork 3
/
logger.go
84 lines (68 loc) · 2.56 KB
/
logger.go
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package logger
import (
"context"
"go.uber.org/zap"
"github.com/CoreumFoundation/coreum-tools/pkg/parallel"
)
//go:generate mockgen -destination=mock.go -package=logger . Logger,ParallelLogger
// ParallelLogger is parallel logger interface used mostly for mocks.
type ParallelLogger interface {
parallel.Logger
}
// A Field is a marshaling operation used to add a key-value pair to a logger's context. Most fields are lazily
// marshaled, so it's inexpensive to add fields to disabled debug-level log statements.
type Field struct {
Key string
Type FieldType
Integer int64
String string
Interface any
}
// A FieldType indicates which member of the Field union struct should be used and how it should be serialized.
type FieldType uint8
// Logger is a logger interface.
type Logger interface {
Debug(ctx context.Context, msg string, fields ...Field)
Info(ctx context.Context, msg string, fields ...Field)
Warn(ctx context.Context, msg string, fields ...Field)
Error(ctx context.Context, msg string, fields ...Field)
ParallelLogger(ctx context.Context) ParallelLogger
}
// AnyField takes a key and an arbitrary value and chooses the best way to represent them as a field, falling back to a
// reflection-based approach only if necessary.
func AnyField(key string, value any) Field {
return convertZapFieldToField(zap.Any(key, value))
}
// StringField constructs a field with the given key and value.
func StringField(key, value string) Field {
return convertZapFieldToField(zap.String(key, value))
}
// Uint32Field constructs a field with the given key and value.
func Uint32Field(key string, value uint32) Field {
return convertZapFieldToField(zap.Uint32(key, value))
}
// Int64Field constructs a field with the given key and value.
func Int64Field(key string, value int64) Field {
return convertZapFieldToField(zap.Int64(key, value))
}
// Uint64Field constructs a field with the given key and value.
func Uint64Field(key string, value uint64) Field {
return convertZapFieldToField(zap.Uint64(key, value))
}
// ByteStringField constructs a field with the given key and value.
func ByteStringField(key string, value []byte) Field {
return convertZapFieldToField(zap.ByteString(key, value))
}
// Error is shorthand for the common idiom NamedError("error", err).
func Error(err error) Field {
return convertZapFieldToField(zap.Error(err))
}
func convertZapFieldToField(zapField zap.Field) Field {
return Field{
Key: zapField.Key,
Type: FieldType(zapField.Type),
Integer: zapField.Integer,
String: zapField.String,
Interface: zapField.Interface,
}
}