forked from nanomsg/mangos-v1
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixes #252 Add logging to ease debugging connection reattempts
- Loading branch information
Ben Krieger
committed
Nov 13, 2016
1 parent
68cfb1b
commit f26a484
Showing
4 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,3 +11,4 @@ wenfang <[email protected]> | |
Anner van Hardenbroek <[email protected]> | ||
Gerrit Renker <[email protected]> | ||
Rene Kaufmann <[email protected]> | ||
Ben Krieger <[email protected]> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package mangos | ||
|
||
import "strings" | ||
|
||
// Logger is an interface that describes a very basic logger. | ||
// It only needs INFO and DEBUG levels, because errors are | ||
// returned by the library code and warnings are just ignored | ||
// errors (which is a bad idea). See: | ||
// http://dave.cheney.net/2015/11/05/lets-talk-about-logging | ||
type Logger interface { | ||
Infof(string, ...interface{}) | ||
Debugf(string, ...interface{}) | ||
} | ||
|
||
type loggers struct { | ||
logs []Logger | ||
level int | ||
} | ||
|
||
var log loggers | ||
|
||
// RegisterLogger appends a new logger to the list of loggers. | ||
// The same logger can be registered multiple times, but will | ||
// then be used multiple times. | ||
func RegisterLogger(l Logger) { | ||
log.logs = append(log.logs, l) | ||
} | ||
|
||
// SetLogLevel understands the following levels: | ||
// 0 - No output | ||
// 1 - Only INFO level | ||
// 2 - Both INFO and DEBUG level | ||
func SetLogLevel(i int) { | ||
log.level = i | ||
} | ||
|
||
// Infof calls Infof on all registered loggers if the log level | ||
// is set to 1 or greater | ||
func (l *loggers) Infof(fmt string, v ...interface{}) { | ||
if l.level < 1 { | ||
return | ||
} | ||
for _, log := range l.logs { | ||
log.Infof(fmt, v...) | ||
} | ||
} | ||
|
||
// Info logs the values space-separated on all registered | ||
// loggers if the log level is set to 1 or greater | ||
func (l *loggers) Info(v ...interface{}) { | ||
fmt := strings.Repeat(" %v", len(v)) | ||
l.Infof(fmt[1:], v...) | ||
} | ||
|
||
// Debugf calls Debugf on all registered loggers if the log level | ||
// is set to 2 or greater | ||
func (l *loggers) Debugf(fmt string, v ...interface{}) { | ||
if l.level < 2 { | ||
return | ||
} | ||
for _, log := range l.logs { | ||
log.Debugf(fmt, v...) | ||
} | ||
} | ||
|
||
// Debug logs the values space-separated on all registered | ||
// loggers if the log level is set to 2 or greater | ||
func (l *loggers) Debug(v ...interface{}) { | ||
fmt := strings.Repeat(" %v", len(v)) | ||
l.Debugf(fmt[1:], v...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package log | ||
|
||
import ( | ||
"log" | ||
|
||
"github.com/blitzrk/mangos" | ||
) | ||
|
||
func init() { | ||
mangos.RegisterLogger(&logger{}) | ||
} | ||
|
||
type logger struct{} | ||
|
||
func (l *logger) Infof(fmt string, v ...interface{}) { | ||
log.Printf("[INFO] "+fmt, v...) | ||
} | ||
|
||
func (l *logger) Debugf(fmt string, v ...interface{}) { | ||
log.Printf("[DEBUG] "+fmt, v...) | ||
} |