11package log
22
33import (
4+ "encoding/json"
45 "fmt"
56 "os"
67 "strings"
78 "time"
89)
910
1011// assert interface compliance.
11- var _ Interface = (* Entry )(nil )
12+ var _ Interface = (* EntryFields )(nil )
1213
1314// Now returns the current time.
1415var Now = time .Now
1516
16- // Entry represents a single log entry.
17+ // EntryFields represents a single log entry.
18+ type EntryFields struct {
19+ Logger * Logger `json:"-"`
20+ Fields Fields `json:"-"`
21+ start time.Time
22+ }
23+
24+ // Entry holds a Entry with a Message, Timestamp and a Level.
25+ // This is what is actually logged.
1726type Entry struct {
18- Logger * Logger `json:"-"`
19- Fields Fields `json:"fields"`
20- Level Level `json:"level"`
21- Timestamp time.Time `json:"timestamp"`
22- Message string `json:"message"`
23- start time.Time
27+ * EntryFields
28+ FieldsUnique Fields `json:"-"`
29+ Level Level `json:"level"`
30+ Timestamp time.Time `json:"timestamp"`
31+ Message string `json:"message"`
32+ }
33+
34+ func (e Entry ) MarshalJSON () ([]byte , error ) {
35+ fields := make (map [string ]any )
36+ for _ , f := range e .FieldsUnique {
37+ fields [f .Name ] = f .Value
38+ }
39+
40+ type EntryAlias Entry
41+ return json .Marshal (& struct {
42+ Fields map [string ]any `json:"fields"`
43+ EntryAlias
44+ }{
45+ Fields : fields ,
46+ EntryAlias : (EntryAlias )(e ),
47+ })
2448}
2549
2650// NewEntry returns a new entry for `log`.
27- func NewEntry (log * Logger ) * Entry {
28- return & Entry {
51+ func NewEntry (log * Logger ) * EntryFields {
52+ return & EntryFields {
2953 Logger : log ,
3054 }
3155}
3256
3357// WithFields returns a new entry with `fields` set.
34- func (e Entry ) WithFields (fielder Fielder ) * Entry {
58+ func (e EntryFields ) WithFields (fielder Fielder ) * EntryFields {
3559 e .Fields = append (e .Fields , fielder .Fields ()... )
3660 return & e
3761}
3862
3963// WithField returns a new entry with the `key` and `value` set.
40- func (e * Entry ) WithField (key string , value any ) * Entry {
64+ func (e * EntryFields ) WithField (key string , value any ) * EntryFields {
4165 return e .WithFields (Fields {{key , value }})
4266}
4367
4468// WithDuration returns a new entry with the "duration" field set
4569// to the given duration in milliseconds.
46- func (e * Entry ) WithDuration (d time.Duration ) * Entry {
70+ func (e * EntryFields ) WithDuration (d time.Duration ) * EntryFields {
4771 return e .WithField ("duration" , d .Milliseconds ())
4872}
4973
5074// WithError returns a new entry with the "error" set to `err`.
5175//
5276// The given error may implement .Fielder, if it does the method
5377// will add all its `.Fields()` into the returned entry.
54- func (e * Entry ) WithError (err error ) * Entry {
78+ func (e * EntryFields ) WithError (err error ) * EntryFields {
5579 if err == nil {
5680 return e
5781 }
@@ -81,78 +105,58 @@ func (e *Entry) WithError(err error) *Entry {
81105}
82106
83107// Debug level message.
84- func (e * Entry ) Debug (msg string ) {
108+ func (e * EntryFields ) Debug (msg string ) {
85109 e .Logger .log (DebugLevel , e , msg )
86110}
87111
88112// Info level message.
89- func (e * Entry ) Info (msg string ) {
113+ func (e * EntryFields ) Info (msg string ) {
90114 e .Logger .log (InfoLevel , e , msg )
91115}
92116
93117// Warn level message.
94- func (e * Entry ) Warn (msg string ) {
118+ func (e * EntryFields ) Warn (msg string ) {
95119 e .Logger .log (WarnLevel , e , msg )
96120}
97121
98122// Error level message.
99- func (e * Entry ) Error (msg string ) {
123+ func (e * EntryFields ) Error (msg string ) {
100124 e .Logger .log (ErrorLevel , e , msg )
101125}
102126
103127// Fatal level message, followed by an exit.
104- func (e * Entry ) Fatal (msg string ) {
128+ func (e * EntryFields ) Fatal (msg string ) {
105129 e .Logger .log (FatalLevel , e , msg )
106130 os .Exit (1 )
107131}
108132
109133// Debugf level formatted message.
110- func (e * Entry ) Debugf (msg string , v ... any ) {
134+ func (e * EntryFields ) Debugf (msg string , v ... any ) {
111135 e .Debug (fmt .Sprintf (msg , v ... ))
112136}
113137
114138// Infof level formatted message.
115- func (e * Entry ) Infof (msg string , v ... any ) {
139+ func (e * EntryFields ) Infof (msg string , v ... any ) {
116140 e .Info (fmt .Sprintf (msg , v ... ))
117141}
118142
119143// Warnf level formatted message.
120- func (e * Entry ) Warnf (msg string , v ... any ) {
144+ func (e * EntryFields ) Warnf (msg string , v ... any ) {
121145 e .Warn (fmt .Sprintf (msg , v ... ))
122146}
123147
124148// Errorf level formatted message.
125- func (e * Entry ) Errorf (msg string , v ... any ) {
149+ func (e * EntryFields ) Errorf (msg string , v ... any ) {
126150 e .Error (fmt .Sprintf (msg , v ... ))
127151}
128152
129153// Fatalf level formatted message, followed by an exit.
130- func (e * Entry ) Fatalf (msg string , v ... any ) {
154+ func (e * EntryFields ) Fatalf (msg string , v ... any ) {
131155 e .Fatal (fmt .Sprintf (msg , v ... ))
132156}
133157
134- // Trace returns a new entry with a Stop method to fire off
135- // a corresponding completion log, useful with defer.
136- func (e * Entry ) Trace (msg string ) * Entry {
137- e .Info (msg )
138- v := e .WithFields (e .Fields )
139- v .Message = msg
140- v .start = time .Now ()
141- return v
142- }
143-
144- // Stop should be used with Trace, to fire off the completion message. When
145- // an `err` is passed the "error" field is set, and the log level is error.
146- func (e * Entry ) Stop (err * error ) {
147- if err == nil || * err == nil {
148- e .WithDuration (time .Since (e .start )).Info (e .Message )
149- } else {
150- e .WithDuration (time .Since (e .start )).WithError (* err ).Error (e .Message )
151- }
152- }
153-
154158// mergedFields returns the fields list collapsed into a single slice.
155- func (e * Entry ) mergedFields () Fields {
159+ func (e * EntryFields ) mergedFields () Fields {
156160 fields := make (Fields , 0 , len (e .Fields ))
157161 for i := len (e .Fields ) - 1 ; i >= 0 ; i -- {
158162 f := e .Fields [i ]
@@ -176,12 +180,12 @@ func (e *Entry) mergedFields() Fields {
176180}
177181
178182// finalize returns a copy of the Entry with Fields merged.
179- func (e * Entry ) finalize (level Level , msg string ) * Entry {
183+ func (e * EntryFields ) finalize (level Level , msg string ) * Entry {
180184 return & Entry {
181- Logger : e . Logger ,
182- Fields : e .mergedFields (),
183- Level : level ,
184- Message : msg ,
185- Timestamp : Now (),
185+ EntryFields : e ,
186+ FieldsUnique : e .mergedFields (),
187+ Level : level ,
188+ Message : msg ,
189+ Timestamp : Now (),
186190 }
187191}
0 commit comments