Skip to content

Commit 7199d6e

Browse files
committed
log prefix moved to logger
1 parent 878a942 commit 7199d6e

File tree

4 files changed

+52
-41
lines changed

4 files changed

+52
-41
lines changed

app.go

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ func (a *App) Shutdown() {
103103
// Wait for all servers to start serving to avoid race conditions
104104
// connected with shutdown. 'Shutdown' must be called only if server
105105
// has already started or it does nothing.
106-
logger.Printf("ZeroDT: shutdown servers...")
106+
logger.Printf("shutdown servers...")
107107
a.served.Wait()
108108

109109
var wg sync.WaitGroup
@@ -115,10 +115,10 @@ func (a *App) Shutdown() {
115115
defer wg.Done()
116116
err := s.Shutdown(context.Background())
117117
if err != nil {
118-
logger.Printf("ZeroDT: server %s has been shutdown with: %v", s.Addr, err)
118+
logger.Printf("server %s has been shutdown with: %v", s.Addr, err)
119119
return
120120
}
121-
logger.Printf("ZeroDT: server %s has been shutdown", s.Addr)
121+
logger.Printf("server %s has been shutdown", s.Addr)
122122
}(s)
123123
}
124124

@@ -132,11 +132,11 @@ func (a *App) Shutdown() {
132132
func (a *App) ListenAndServe() error {
133133
inherited, messenger, err := inherit()
134134
if err != nil {
135-
logger.Printf("ZeroDT: failed to inherit listeners with: '%v'", err)
135+
logger.Printf("failed to inherit listeners with: '%v'", err)
136136
return err
137137
}
138138
e := newExchange(inherited)
139-
logger.Printf("ZeroDT: serving with pid='%d', inherited='%s'", os.Getpid(), formatInherited(e))
139+
logger.Printf("serving with pid='%d', inherited='%s'", os.Getpid(), formatInherited(e))
140140

141141
// Signals wait group.
142142
var sigWG sync.WaitGroup
@@ -170,20 +170,20 @@ func (a *App) ListenAndServe() error {
170170

171171
l, err := e.acquireOrCreateListener("tcp", s.Addr)
172172
if err != nil {
173-
logger.Printf("ZeroDT: failed to listen on %v with: %v", s.Addr, err)
173+
logger.Printf("failed to listen on %v with: %v", s.Addr, err)
174174
return
175175
}
176176
// A server is about to Serve and already listen.
177177
startOnce.Done()
178178
// Wait for parent to start if set.
179179
parentWG.Wait()
180180
if startErr != nil {
181-
logger.Printf("ZeroDT: server %v exited with: %v", s.Addr, startErr)
181+
logger.Printf("server %v exited with: %v", s.Addr, startErr)
182182
return
183183
}
184184
// TODO: shutdown all servers in case of error
185185
err = s.Serve(&notifyListener{Listener: tcpKeepAliveListener{l}, doneOnce: servedOnce})
186-
logger.Printf("ZeroDT: server %v has finished serving with: %v", s.Addr, err)
186+
logger.Printf("server %v has finished serving with: %v", s.Addr, err)
187187
}(s)
188188
}
189189

@@ -219,7 +219,7 @@ func (a *App) ListenAndServe() error {
219219
}
220220

221221
func (a *App) handleSignals(ctx context.Context, wg *sync.WaitGroup, e *exchange) {
222-
defer logger.Printf("ZeroDT: stop handling signals")
222+
defer logger.Printf("stop handling signals")
223223
defer wg.Done()
224224

225225
signals := make(chan os.Signal, 10)
@@ -241,7 +241,7 @@ CatchSignals:
241241
return
242242
// OS signal.
243243
case s := <-signals:
244-
logger.Printf("ZeroDT: %v signal...", s)
244+
logger.Printf("%v signal...", s)
245245
switch s {
246246
// Shutdown servers. No exit here.
247247
case syscall.SIGINT, syscall.SIGTERM:
@@ -251,12 +251,12 @@ CatchSignals:
251251
case syscall.SIGUSR2:
252252
_, f, err := forkExec(e.activeFiles())
253253
if err != nil {
254-
logger.Printf("ZeroDT: failed to forkExec: '%v'", err)
254+
logger.Printf("failed to forkExec: '%v'", err)
255255
continue CatchSignals
256256
}
257257
m, err := ListenSocket(f)
258258
if err != nil {
259-
logger.Printf("ZeroDT: failed to listen communication socket: '%v'", err)
259+
logger.Printf("failed to listen communication socket: '%v'", err)
260260
continue CatchSignals
261261
}
262262
// Nothing to do with errors.
@@ -353,21 +353,21 @@ func protocolActAsParent(m *StreamMessenger, waitChildTimeout time.Duration, wai
353353
m.SetDeadline(time.Now().Add(waitChildTimeout))
354354

355355
// Child->Parent, ready message.
356-
logger.Printf("ZeroDT: waiting for readyMsg...")
356+
logger.Printf("waiting for readyMsg...")
357357
r := readyMsg{}
358358
err := m.Recv(&r)
359359
if err != nil {
360-
logger.Printf("ZeroDT: Parent<=>Child communication failed with: '%v'", err)
360+
logger.Printf("Parent<=>Child communication failed with: '%v'", err)
361361
// The child will die by timout.
362362
return err
363363
}
364364

365365
// Parent->Child, ready confirmation message.
366-
logger.Printf("ZeroDT: sending readyConfirmationMsg to the child...")
366+
logger.Printf("sending readyConfirmationMsg to the child...")
367367
tipTimeout := maxTimeout(r.WaitParentShutdownTimeout, waitParentShutdownTimeout)
368368
err = m.Send(readyConfirmationMsg{FixedWaitParentShutdownTimeout: tipTimeout})
369369
if err != nil {
370-
logger.Printf("ZeroDT: Parent<=>Child communication failed with: '%v'", err)
370+
logger.Printf("Parent<=>Child communication failed with: '%v'", err)
371371
// The child will die by timout.
372372
return err
373373
}
@@ -377,11 +377,11 @@ func protocolActAsParent(m *StreamMessenger, waitChildTimeout time.Duration, wai
377377
//
378378

379379
// Child->Parent, accepted message.
380-
logger.Printf("ZeroDT: waiting for acceptedMsg...")
380+
logger.Printf("waiting for acceptedMsg...")
381381
a := acceptedMsg{}
382382
err = m.Recv(&a)
383383
if err != nil {
384-
logger.Printf("ZeroDT: Parent<=>Child communication failed with: '%v'", err)
384+
logger.Printf("Parent<=>Child communication failed with: '%v'", err)
385385
}
386386

387387
// Shutdown callback.
@@ -391,11 +391,11 @@ func protocolActAsParent(m *StreamMessenger, waitChildTimeout time.Duration, wai
391391
if tipTimeout == 0 {
392392
return nil
393393
}
394-
logger.Printf("ZeroDT: sending shutdownConfirmationMsg to the child...")
394+
logger.Printf("sending shutdownConfirmationMsg to the child...")
395395
m.SetDeadline(time.Now().Add(sendTimeout))
396396
err = m.Send(shutdownConfirmationMsg{})
397397
if err != nil {
398-
logger.Printf("ZeroDT: Parent<=>Child communication failed with: '%v'", err)
398+
logger.Printf("Parent<=>Child communication failed with: '%v'", err)
399399
}
400400
return nil
401401
}
@@ -404,21 +404,21 @@ func protocolActAsChild(m *StreamMessenger, waitChildTimeout time.Duration, wait
404404
defer m.Close()
405405

406406
// Child->Parent, ready message.
407-
logger.Printf("ZeroDT: sending readyMsg to the parent...")
407+
logger.Printf("sending readyMsg to the parent...")
408408
m.SetDeadline(time.Now().Add(sendTimeout))
409409
err := m.Send(readyMsg{WaitParentShutdownTimeout: waitParentShutdownTimeout})
410410
if err != nil {
411-
logger.Printf("ZeroDT: Parent<=>Child communication failed with: '%v'", err)
411+
logger.Printf("Parent<=>Child communication failed with: '%v'", err)
412412
return err
413413
}
414414

415415
// Parent->Child, ready confirmation message.
416-
logger.Printf("ZeroDT: waiting for readyConfirmationMsg...")
416+
logger.Printf("waiting for readyConfirmationMsg...")
417417
rcr := readyConfirmationMsg{}
418418
m.SetDeadline(time.Now().Add(maxTimeout(waitChildTimeout, waitParentShutdownTimeout)))
419419
err = m.Recv(&rcr)
420420
if err != nil {
421-
logger.Printf("ZeroDT: Parent<=>Child communication failed with: '%v'", err)
421+
logger.Printf("Parent<=>Child communication failed with: '%v'", err)
422422
return err
423423
}
424424

@@ -429,29 +429,29 @@ func protocolActAsChild(m *StreamMessenger, waitChildTimeout time.Duration, wait
429429
notifyFn()
430430

431431
// Child->Parent, accepted message.
432-
logger.Printf("ZeroDT: sending acceptedMsg...")
432+
logger.Printf("sending acceptedMsg...")
433433
m.SetDeadline(time.Now().Add(sendTimeout))
434434
err = m.Send(acceptedMsg{})
435435
if err != nil {
436-
logger.Printf("ZeroDT: Parent<=>Child communication failed with: '%v'", err)
436+
logger.Printf("Parent<=>Child communication failed with: '%v'", err)
437437
}
438438

439439
if rcr.FixedWaitParentShutdownTimeout == 0 {
440440
return nil
441441
}
442442

443443
// Parent->Child, shutdown confirmation message.
444-
logger.Printf("ZeroDT: waiting for shutdownConfirmationMsg...")
444+
logger.Printf("waiting for shutdownConfirmationMsg...")
445445
scr := shutdownConfirmationMsg{}
446446
m.SetDeadline(time.Now().Add(rcr.FixedWaitParentShutdownTimeout))
447447
err = m.Recv(&scr)
448448
if err != nil {
449-
logger.Printf("ZeroDT: Parent<=>Child communication failed with: '%v'", err)
449+
logger.Printf("Parent<=>Child communication failed with: '%v'", err)
450450
if opErr, ok := err.(*net.OpError); ok {
451451
if opErr.Timeout() {
452452
// There are issues on parent's side probably. Need to kill parent.
453453
parentPID, err := killParent()
454-
logger.Printf("ZeroDT: parent %d was killed with: '%v'", parentPID, err)
454+
logger.Printf("parent %d was killed with: '%v'", parentPID, err)
455455
return nil
456456
}
457457
}

examples/test/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import (
99
"strconv"
1010
"time"
1111

12-
"github.com/Sirupsen/logrus"
1312
"github.com/gorilla/mux"
13+
"github.com/sirupsen/logrus"
1414
"github.com/ssgreg/zerodt"
1515
)
1616

exchange.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ func (e *exchange) acquireOrCreateListener(netStr, addrStr string) (*net.TCPList
9797
// Try to acquire one of inherited listeners.
9898
l := e.acquireListener(addr)
9999
if l != nil {
100-
logger.Printf("ZeroDT: listener '%v' acquired", addr)
100+
logger.Printf("listener '%v' acquired", addr)
101101
return l, nil
102102
}
103103

@@ -111,7 +111,7 @@ func (e *exchange) acquireOrCreateListener(netStr, addrStr string) (*net.TCPList
111111
l.Close()
112112
return nil, err
113113
}
114-
logger.Printf("ZeroDT: listener '%v' created", addr)
114+
logger.Printf("listener '%v' created", addr)
115115

116116
return l, nil
117117
}

logger.go

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,32 @@ type StdLogger interface {
1515
Print(...interface{})
1616
Printf(string, ...interface{})
1717
Println(...interface{})
18-
19-
Fatal(...interface{})
20-
Fatalf(string, ...interface{})
21-
Fatalln(...interface{})
22-
23-
Panic(...interface{})
24-
Panicf(string, ...interface{})
25-
Panicln(...interface{})
2618
}
2719

2820
var (
2921
// logger discards all log messages by default
3022
logger StdLogger = log.New(ioutil.Discard, "", 0)
3123
)
3224

25+
type prefixLogger struct {
26+
StdLogger
27+
prefix string
28+
}
29+
30+
func (l *prefixLogger) Print(args ...interface{}) {
31+
args = append([]interface{}{l.prefix}, args...)
32+
l.StdLogger.Print(args...)
33+
}
34+
35+
func (l *prefixLogger) Printf(format string, args ...interface{}) {
36+
l.StdLogger.Printf(l.prefix+" "+format, args...)
37+
}
38+
39+
func (l *prefixLogger) Println(args ...interface{}) {
40+
args = append([]interface{}{l.prefix}, args...)
41+
l.StdLogger.Println(args...)
42+
}
43+
3344
// SetLogger allows to set a different logger that is compatible with StdLogger
3445
// interface. Tested with stdlib logger:
3546
//
@@ -40,5 +51,5 @@ var (
4051
// logrus.StandardLogger()
4152
//
4253
func SetLogger(l StdLogger) {
43-
logger = l
54+
logger = &prefixLogger{StdLogger: l, prefix: "zerodt:"}
4455
}

0 commit comments

Comments
 (0)