-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change Logger to resemble go-kit/log.Logger
- Loading branch information
1 parent
57095c4
commit c6ea527
Showing
5 changed files
with
42 additions
and
32 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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
linters-settings: | ||
errcheck: | ||
exclude-functions: | ||
- (io.Writer).Write | ||
- (github.com/stephenafamo/orchestra.Logger).Log | ||
|
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
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 |
---|---|---|
@@ -1,28 +1,51 @@ | ||
package orchestra | ||
|
||
import ( | ||
"log" | ||
"os" | ||
"errors" | ||
"fmt" | ||
) | ||
|
||
// Logger is accepted by some Players ([Conductor], [ServerPlayer]) | ||
type Logger interface { | ||
Printf(format string, v ...interface{}) | ||
Log(keyvals ...interface{}) error | ||
} | ||
|
||
// DefaultLogger is used when a conductor's logger is nil | ||
var DefaultLogger Logger = log.New(os.Stderr, "", log.LstdFlags) | ||
var DefaultLogger Logger = defaultLogger{} | ||
|
||
type defaultLogger struct{} | ||
|
||
func (d defaultLogger) Log(keyvals ...interface{}) error { | ||
pairLen := len(keyvals) | ||
|
||
if pairLen < 1 || pairLen%2 != 0 { | ||
return errors.New("non-even number of values to log") | ||
} | ||
|
||
for i := range keyvals { | ||
if i%2 != 0 { | ||
continue | ||
} | ||
|
||
fmt.Printf("%v=%q ", keyvals[i], keyvals[i+1]) | ||
} | ||
|
||
// Move to next line | ||
fmt.Println() | ||
|
||
return nil | ||
} | ||
|
||
type subConductorLogger struct { | ||
name string | ||
l Logger | ||
} | ||
|
||
func (s subConductorLogger) Printf(format string, v ...interface{}) { | ||
func (s subConductorLogger) Log(keyvals ...interface{}) error { | ||
l := s.l | ||
if s.l == nil { | ||
l = DefaultLogger | ||
} | ||
|
||
l.Printf("%s: "+format, append([]interface{}{s.name}, v...)...) | ||
return l.Log(append([]interface{}{"conductor", s.name}, keyvals...)...) | ||
} |