-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Add zapgrpc package #402
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add zapgrpc package #402
Changes from 3 commits
e96ae7c
c8d7cb5
23bd17a
379bb77
a51186d
ca2270b
fa1c169
35d347b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| // Copyright (c) 2016 Uber Technologies, Inc. | ||
| // | ||
| // Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| // of this software and associated documentation files (the "Software"), to deal | ||
| // in the Software without restriction, including without limitation the rights | ||
| // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| // copies of the Software, and to permit persons to whom the Software is | ||
| // furnished to do so, subject to the following conditions: | ||
| // | ||
| // The above copyright notice and this permission notice shall be included in | ||
| // all copies or substantial portions of the Software. | ||
| // | ||
| // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
| // THE SOFTWARE. | ||
|
|
||
| // Package zapgrpc provides a logger that is compatible with grpclog. | ||
| package zapgrpc // import "go.uber.org/zap/zapgrpc" | ||
|
|
||
| import "go.uber.org/zap" | ||
|
|
||
| // LoggerOption is an option for a new Logger. | ||
| type LoggerOption func(*loggerOptions) | ||
|
|
||
| // WithDebug says to print the debug level instead of the default info level. | ||
| func WithDebug() LoggerOption { | ||
| return func(loggerOptions *loggerOptions) { | ||
| loggerOptions.printFunc = (*zap.SugaredLogger).Debug | ||
| loggerOptions.printfFunc = (*zap.SugaredLogger).Debugf | ||
| } | ||
| } | ||
|
|
||
| // NewLogger returns a new Logger. | ||
| func NewLogger(l *zap.Logger, options ...LoggerOption) *Logger { | ||
| loggerOptions := newLoggerOptions(options...) | ||
| return &Logger{ | ||
| l.Sugar(), | ||
| (*zap.SugaredLogger).Fatal, | ||
| (*zap.SugaredLogger).Fatalf, | ||
| loggerOptions.printFunc, | ||
| loggerOptions.printfFunc, | ||
| } | ||
| } | ||
|
|
||
| // Logger is a logger that is compatible with the grpclog Logger interface. | ||
| type Logger struct { | ||
| sugaredLogger *zap.SugaredLogger | ||
| fatalFunc func(*zap.SugaredLogger, ...interface{}) | ||
| fatalfFunc func(*zap.SugaredLogger, string, ...interface{}) | ||
| printFunc func(*zap.SugaredLogger, ...interface{}) | ||
| printfFunc func(*zap.SugaredLogger, string, ...interface{}) | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's the benefit of keeping these four function pointers instead of and checking This also lets us get rid of the the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Talked a little offline, I still prefer this, but I can change to something else
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I could also do
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Mind explaining what's attractive about this approach? I'm not necessarily opposed, I'm just not seeing the upside. There's no reason I can see that changing the internal structure of
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe there's a misunderstanding - note this is controlling what happens when you call a grpclog.Print("hello") // if WithDebug was not set, this will call zap.Info
grpclog.Print("hello") // if WithDebug was set, this will call zap.DebugBy setting Doing this by setting
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fair enough. |
||
|
|
||
| // Fatal implements grpclog.Logger#Fatal. | ||
| func (l *Logger) Fatal(args ...interface{}) { | ||
| l.fatalFunc(l.sugaredLogger, args...) | ||
| } | ||
|
|
||
| // Fatalf implements grpclog.Logger#Fatalf. | ||
| func (l *Logger) Fatalf(format string, args ...interface{}) { | ||
| l.fatalfFunc(l.sugaredLogger, format, args...) | ||
| } | ||
|
|
||
| // Fatalln implements grpclog.Logger#Fatalln. | ||
| func (l *Logger) Fatalln(args ...interface{}) { | ||
| l.fatalFunc(l.sugaredLogger, args...) | ||
| } | ||
|
|
||
| // Print implements grpclog.Logger#Print. | ||
| func (l *Logger) Print(args ...interface{}) { | ||
| l.printFunc(l.sugaredLogger, args...) | ||
| } | ||
|
|
||
| // Printf implements grpclog.Logger#Printf. | ||
| func (l *Logger) Printf(format string, args ...interface{}) { | ||
| l.printfFunc(l.sugaredLogger, format, args...) | ||
| } | ||
|
|
||
| // Println implements grpclog.Logger#Println. | ||
| func (l *Logger) Println(args ...interface{}) { | ||
| l.printFunc(l.sugaredLogger, args...) | ||
| } | ||
|
|
||
| type loggerOptions struct { | ||
| printFunc func(*zap.SugaredLogger, ...interface{}) | ||
| printfFunc func(*zap.SugaredLogger, string, ...interface{}) | ||
| } | ||
|
|
||
| func newLoggerOptions(options ...LoggerOption) *loggerOptions { | ||
| loggerOptions := &loggerOptions{} | ||
| for _, option := range options { | ||
| option(loggerOptions) | ||
| } | ||
| if loggerOptions.printFunc == nil { | ||
| loggerOptions.printFunc = (*zap.SugaredLogger).Info | ||
| } | ||
| if loggerOptions.printfFunc == nil { | ||
| loggerOptions.printfFunc = (*zap.SugaredLogger).Infof | ||
| } | ||
| return loggerOptions | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See comment on
Logger- if possible, it'd be nice to avoid having exported types reference unexported types.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated to take a
*Loggerinstead