@@ -4,8 +4,6 @@ package local
44
55import (
66 "errors"
7- "fmt"
8- "log"
97 "log/slog"
108 "sync"
119 "time"
@@ -82,10 +80,10 @@ func (l *Listener) Listen() (chan listener.Notification, chan error, error) {
8280
8381func (l * Listener ) listen (msgs chan listener.Notification , errs chan error ) {
8482 defer func () {
85- slog .Debug (fmt . Sprintf ( "Stopping listener..." ) )
83+ slog .Debug ("Stopping listener..." )
8684 l .items = nil
8785 close (l .stop )
88- slog .Debug (fmt . Sprintf ( "Stopped." ) )
86+ slog .Debug ("Stopped." )
8987 }()
9088
9189 l .wait (msgs , errs , l .stop )
@@ -97,27 +95,27 @@ func (l *Listener) wait(msgs chan listener.Notification, errs chan error, stop c
9795 // channel is signaled or closed.
9896 //
9997 // This path is currently run only in the unit tests, so I included some
100- // `log.Printf ` usages are for the benefit of verbose testing output.
98+ // `slog.Debug ` usages are for the benefit of verbose testing output.
10199 if l .deferredStart != nil {
102100 // First, wait for the test to notify that it's time to start listening. This
103101 // gives us a chance to set up a deadlock condition in the test.
104- log . Printf ("Waiting for test notification to start" )
102+ slog . Debug ("Waiting for test notification to start" )
105103 <- l .deferredStart
106104 // Next, simulate an unexpected stop by waiting for a stop signal after
107105 // which we return without ever receiving from the `l.items` channel.
108- log . Printf ("Proceeding with wait by waiting for stop signal" )
106+ slog . Debug ("Proceeding with wait by waiting for stop signal" )
109107 <- stop
110- log . Printf ("Stopped. Returning without receiving from l.items." )
108+ slog . Debug ("Stopped. Returning without receiving from l.items." )
111109 return
112110 }
113111 for {
114112 select {
115113 case <- stop :
116- slog .Debug (fmt . Sprintf ( "Stopping wait" ) )
114+ slog .Debug ("Stopping wait" )
117115 return
118116 case i := <- l .items :
119117 if msg , ok := i .(listener.Notification ); ok {
120- slog .Debug (fmt . Sprintf ( "Received message: %s . Sending to buffered channel with current size %d" , msg .Guid (), len (msgs ) ))
118+ slog .Debug ("Received message. Sending to buffered channel" , "guid" , msg .Guid (), "channelSize" , len (msgs ))
121119 msgs <- msg
122120 } else {
123121 errs <- ErrNotNotificationType
@@ -129,13 +127,13 @@ func (l *Listener) wait(msgs chan listener.Notification, errs chan error, stop c
129127func (l * Listener ) Stop () {
130128 if l .stop != nil {
131129 // Signal to stop
132- slog .Debug (fmt . Sprintf ( "Signaling stop channel to stop..." ) )
130+ slog .Debug ("Signaling stop channel to stop..." )
133131 l .stop <- true
134132
135133 // Wait for stop
136- slog .Debug (fmt . Sprintf ( "Waiting for stop channel to close..." ) )
134+ slog .Debug ("Waiting for stop channel to close..." )
137135 <- l .stop
138- slog .Debug (fmt . Sprintf ( "Stop channel for %s closed." , l .guid ) )
136+ slog .Debug ("Stop channel closed." , "guid" , l .guid )
139137 l .stop = nil
140138
141139 // Remove from provider
@@ -169,7 +167,7 @@ func (l *ListenerProvider) notify(channel string, n interface{}, prevMissedItems
169167 notifyTxt = "renotify"
170168 }
171169
172- slog .Debug (fmt . Sprintf ( "Notify called with type=%s on %d listeners " , notifyTxt , len (l .listeners ) ))
170+ slog .Debug ("Notify called" , " type" , notifyTxt , "listenerCount" , len (l .listeners ))
173171 for _ , ll := range l .listeners {
174172 var needNotify bool
175173 if prevMissedItems == nil {
@@ -184,7 +182,7 @@ func (l *ListenerProvider) notify(channel string, n interface{}, prevMissedItems
184182 // There's a chance that `ll.items` could be non-nil, but not receiving. Timeout
185183 // to prevent deadlock, but keep trying until we're sure that the listener is
186184 // closed.
187- slog .Debug (fmt . Sprintf ( "Ready to %s internal items with guid %s for channel %s %s: %+v" , notifyTxt , notifyGuid , ll .guid , channel , n ) )
185+ slog .Debug ("Ready to notify internal items" , "type" , notifyTxt , " notifyGuid" , notifyGuid , "listenerGuid" , ll .guid , " channel" , channel , "notification" , n )
188186 func () {
189187 // It's important to create a ticker and stop it so it doesn't leak. This has the potential to be called
190188 // thousands of times in a relatively short period on a busy server, so timer leaks can result in
@@ -193,9 +191,9 @@ func (l *ListenerProvider) notify(channel string, n interface{}, prevMissedItems
193191 defer timeout .Stop ()
194192 select {
195193 case ll .items <- n :
196- slog .Debug (fmt . Sprintf ( "Done with %s in local listener with guid %s for channel %s: %+v" , notifyTxt , notifyGuid , channel , n ) )
194+ slog .Debug ("Done with notify in local listener" , "type" , notifyTxt , " notifyGuid" , notifyGuid , " channel" , channel , "notification" , n )
197195 case <- timeout .C :
198- slog .Debug (fmt . Sprintf ( "Timeout during %s for listener %s/%s with guid %s for channel %s: %+v" , notifyTxt , ll .guid , ll .name , notifyGuid , channel , n ) )
196+ slog .Debug ("Timeout during notify" , "type" , notifyTxt , "listenerGuid" , ll .guid , "listenerName" , ll .name , " notifyGuid" , notifyGuid , " channel" , channel , "notification" , n )
199197 // Record the timed-out notification in the `missed` map so we can retry it
200198 missed [ll .guid ] = notifyGuid
201199 }
@@ -212,7 +210,7 @@ func (l *ListenerProvider) notify(channel string, n interface{}, prevMissedItems
212210 // provider after the mutex is again locked by the recursive call to `notify` will be
213211 // attempted again as needed.
214212 if len (missed ) > 0 {
215- slog .Debug (fmt . Sprintf ( "calling l.notify for %+v with guid %s for %d missed items on channel %s" , n , notifyGuid , len (missed ), channel ) )
213+ slog .Debug ("calling l.notify for missed items" , "notification" , n , " notifyGuid" , notifyGuid , "missedCount" , len (missed ), " channel" , channel )
216214 stopCh := make (chan struct {})
217215 defer close (stopCh )
218216 // If logging is enabled, periodically record notifications that are still waiting
@@ -222,13 +220,13 @@ func (l *ListenerProvider) notify(channel string, n interface{}, prevMissedItems
222220 for {
223221 select {
224222 case <- tick .C :
225- slog .Debug (fmt . Sprintf ( "still waiting on l.notify for +%v with guid %s on channel %s" , n , notifyGuid , channel ) )
223+ slog .Debug ("still waiting on l.notify" , "notification" , n , " notifyGuid" , notifyGuid , " channel" , channel )
226224 case <- stop :
227225 return
228226 }
229227 }
230228 }(stopCh )
231229 l .notify (channel , n , missed )
232- slog .Debug (fmt . Sprintf ( "completed calling l.notify for %d missed items with guid %s on channel %s" , len (missed ), notifyGuid , channel ) )
230+ slog .Debug ("completed calling l.notify for missed items" , "missedCount" , len (missed ), " notifyGuid" , notifyGuid , " channel" , channel )
233231 }
234232}
0 commit comments