-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentry.go
119 lines (100 loc) · 3.16 KB
/
entry.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package tower
import (
"context"
"time"
)
// EntryBuilder is the builder for Entry.
type EntryBuilder interface {
// Code Sets the code for this entry.
Code(i int) EntryBuilder
// Message Sets the message for this entry.
//
// In built in implementation, If args are supplied, fmt.Sprintf will be called with s as base string.
//
// Very unlikely you will need to set this, because tower already create the message field for you when you call tower.NewEntry.
Message(s string, args ...any) EntryBuilder
// Context Sets additional data that will enrich how the entry will look.
//
// `tower.Fields` is a type that is more integrated with built-in Messengers.
// Using this type as Context value will often have special treatments for it.
//
// In built-in implementation, additional call to .Context() will make additional index, not replacing what you already set.
//
// Example:
//
// tower.NewEntry(msg).Code(200).Context(tower.F{"foo": "bar"}).Freeze()
Context(ctx ...any) EntryBuilder
// Key Sets the key for this entry. This is how Messenger will use to identify if an entry is the same as previous or not.
//
// In tower's built-in implementation, by default, no key is set when creating new entry.
//
// Usually by not setting the key, The Messenger will generate their own key for this message.
//
// In built in implementation, If args are supplied, fmt.Sprintf will be called with key as base string.
Key(key string, args ...any) EntryBuilder
// Caller Sets the caller for this entry.
//
// In tower's built-in implementation, by default, the caller is the location where you call `tower.NewEntry`.
Caller(c Caller) EntryBuilder
// Time Sets the time for this entry. By default, it's already set when you call tower.NewEntry.
Time(time.Time) EntryBuilder
/*
Sets the level for this entry.
In tower's built-in implementation, this defaults to what method you call to generate this entry.
*/
Level(lvl Level) EntryBuilder
/*
Freeze this entry. Preventing further mutations.
*/
Freeze() Entry
/*
Log this entry. Implicitly calling .Freeze() method.
*/
Log(ctx context.Context) Entry
/*
Sends this Entry to Messengers. Implicitly calling .Freeze() method.
*/
Notify(ctx context.Context, opts ...MessageOption) Entry
}
type Entry interface {
CallerHint
CodeHint
ContextHint
HTTPCodeHint
KeyHint
LevelHint
MessageHint
ServiceHint
TimeHint
/*
Logs this entry.
*/
Log(ctx context.Context) Entry
/*
Notifies this entry to Messengers.
*/
Notify(ctx context.Context, opts ...MessageOption) Entry
}
type EntryConstructorContext struct {
Caller Caller
Message string
Tower *Tower
}
type EntryConstructor interface {
ConstructEntry(*EntryConstructorContext) EntryBuilder
}
var _ EntryConstructor = (EntryConstructorFunc)(nil)
type EntryConstructorFunc func(*EntryConstructorContext) EntryBuilder
func (f EntryConstructorFunc) ConstructEntry(ctx *EntryConstructorContext) EntryBuilder {
return f(ctx)
}
func defaultEntryConstructor(ctx *EntryConstructorContext) EntryBuilder {
return &entryBuilder{
message: ctx.Message,
caller: ctx.Caller,
context: []any{},
level: InfoLevel,
tower: ctx.Tower,
time: time.Now(),
}
}