@@ -27,10 +27,9 @@ import (
2727 "sync"
2828 "time"
2929
30+ "go.uber.org/multierr"
3031 "go.uber.org/zap/internal/bufferpool"
3132 "go.uber.org/zap/internal/exit"
32-
33- "go.uber.org/multierr"
3433)
3534
3635var (
@@ -152,6 +151,13 @@ type Entry struct {
152151 Stack string
153152}
154153
154+ // CheckWriteHook allows to customize the action to take after a Fatal log entry
155+ // is processed.
156+ type CheckWriteHook interface {
157+ // OnWrite gets invoked when an entry is written
158+ OnWrite (* CheckedEntry , []Field )
159+ }
160+
155161// CheckWriteAction indicates what action to take after a log entry is
156162// processed. Actions are ordered in increasing severity.
157163type CheckWriteAction uint8
@@ -164,10 +170,25 @@ const (
164170 WriteThenGoexit
165171 // WriteThenPanic causes a panic after Write.
166172 WriteThenPanic
167- // WriteThenFatal causes a fatal os.Exit after Write.
173+ // WriteThenFatal causes an os.Exit(1) after Write.
168174 WriteThenFatal
169175)
170176
177+ // OnWrite implements the OnWrite method to keep CheckWriteAction compatible
178+ // with the new CheckWriteHook interface which deprecates CheckWriteAction.
179+ func (a CheckWriteAction ) OnWrite (ce * CheckedEntry , _ []Field ) {
180+ switch a {
181+ case WriteThenGoexit :
182+ runtime .Goexit ()
183+ case WriteThenPanic :
184+ panic (ce .Message )
185+ case WriteThenFatal :
186+ exit .Exit ()
187+ }
188+ }
189+
190+ var _ CheckWriteHook = CheckWriteAction (0 )
191+
171192// CheckedEntry is an Entry together with a collection of Cores that have
172193// already agreed to log it.
173194//
@@ -178,15 +199,15 @@ type CheckedEntry struct {
178199 Entry
179200 ErrorOutput WriteSyncer
180201 dirty bool // best-effort detection of pool misuse
181- should CheckWriteAction
202+ after CheckWriteHook
182203 cores []Core
183204}
184205
185206func (ce * CheckedEntry ) reset () {
186207 ce .Entry = Entry {}
187208 ce .ErrorOutput = nil
188209 ce .dirty = false
189- ce .should = WriteThenNoop
210+ ce .after = nil
190211 for i := range ce .cores {
191212 // don't keep references to cores
192213 ce .cores [i ] = nil
@@ -224,17 +245,11 @@ func (ce *CheckedEntry) Write(fields ...Field) {
224245 ce .ErrorOutput .Sync ()
225246 }
226247
227- should , msg := ce .should , ce .Message
228- putCheckedEntry (ce )
229-
230- switch should {
231- case WriteThenPanic :
232- panic (msg )
233- case WriteThenFatal :
234- exit .Exit ()
235- case WriteThenGoexit :
236- runtime .Goexit ()
248+ hook := ce .after
249+ if hook != nil {
250+ hook .OnWrite (ce , fields )
237251 }
252+ putCheckedEntry (ce )
238253}
239254
240255// AddCore adds a Core that has agreed to log this CheckedEntry. It's intended to be
@@ -252,11 +267,19 @@ func (ce *CheckedEntry) AddCore(ent Entry, core Core) *CheckedEntry {
252267// Should sets this CheckedEntry's CheckWriteAction, which controls whether a
253268// Core will panic or fatal after writing this log entry. Like AddCore, it's
254269// safe to call on nil CheckedEntry references.
270+ // Deprecated: Use After(ent Entry, after CheckWriteHook) instead.
255271func (ce * CheckedEntry ) Should (ent Entry , should CheckWriteAction ) * CheckedEntry {
272+ return ce .After (ent , should )
273+ }
274+
275+ // After sets this CheckEntry's CheckWriteHook, which will be called after this
276+ // log entry has been written. It's safe to call this on nil CheckedEntry
277+ // references.
278+ func (ce * CheckedEntry ) After (ent Entry , hook CheckWriteHook ) * CheckedEntry {
256279 if ce == nil {
257280 ce = getCheckedEntry ()
258281 ce .Entry = ent
259282 }
260- ce .should = should
283+ ce .after = hook
261284 return ce
262285}
0 commit comments