-
Notifications
You must be signed in to change notification settings - Fork 2
Zap lager #22
base: master
Are you sure you want to change the base?
Zap lager #22
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| package logging | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| "code.cloudfoundry.org/lager" | ||
| "go.uber.org/zap" | ||
| ) | ||
|
|
||
| // LagerAdapter satisfies the lager.Logger interface with zap as the | ||
| // implementation. | ||
| type LagerAdapter struct { | ||
| originalLogger *zap.Logger | ||
|
Collaborator
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. this is all pointers now. |
||
| } | ||
|
|
||
| // NewLagerAdapter returns a new lager.Logger that uses zap underneath. | ||
| func NewLagerAdapter(zapLogger *zap.Logger) *LagerAdapter { | ||
| return &LagerAdapter{ | ||
| originalLogger: zapLogger, | ||
| } | ||
| } | ||
|
|
||
| // RegisterSink is never used after initialization, so it does nothing. | ||
| func (l *LagerAdapter) RegisterSink(_ lager.Sink) {} | ||
|
|
||
| // Session returns a new logger with a nested session. | ||
| func (l *LagerAdapter) Session(task string, data ...lager.Data) lager.Logger { | ||
| tmpLogger := l.originalLogger.Named(task) | ||
|
|
||
| if data != nil { | ||
| tmpLogger = l.originalLogger.With(dataToFields(data)...) | ||
| } | ||
|
|
||
| return &LagerAdapter{ | ||
| originalLogger: tmpLogger, | ||
| } | ||
| } | ||
|
Collaborator
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. not sure if this is implemented correctly, and I mostly don't care.
Collaborator
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. Session is more useful in tracing and knowing where the logs are coming from in bigger projects where a lot of component logs come through or when you have many requests and you correlate all the logs for one request.
Collaborator
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. Saying that, lager does a special thing with session so you can track it all the way down to where the original logger was created. With the adapter we won't have that so can't use Session in that way anymore. |
||
|
|
||
| // SessionName returns the name of the logger session | ||
| func (l *LagerAdapter) SessionName() string { | ||
| return "why would you want the name of the logger?" | ||
|
Collaborator
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 honestly don't understand why this is a thing that exists. |
||
| } | ||
|
|
||
|
Collaborator
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. Everything below is a direct copy. |
||
| // Debug logs a message at the debug log level. | ||
| func (l *LagerAdapter) Debug(action string, data ...lager.Data) { | ||
| l.originalLogger.Debug(action, dataToFields(data)...) | ||
| } | ||
|
|
||
| // Info logs a message at the info log level. | ||
| func (l *LagerAdapter) Info(action string, data ...lager.Data) { | ||
| l.originalLogger.Info(action, dataToFields(data)...) | ||
| } | ||
|
|
||
| // Error logs a message at the error log level. | ||
| func (l *LagerAdapter) Error(action string, err error, data ...lager.Data) { | ||
| l.originalLogger.Error(action, appendError(err, dataToFields(data))...) | ||
| } | ||
|
|
||
| // Fatal logs a message and exits with status 1. | ||
| func (l *LagerAdapter) Fatal(action string, err error, data ...lager.Data) { | ||
| l.originalLogger.Fatal(action, appendError(err, dataToFields(data))...) | ||
| } | ||
|
|
||
| // WithData returns a logger with newly added data. | ||
| func (l *LagerAdapter) WithData(data lager.Data) lager.Logger { | ||
| return &LagerAdapter{ | ||
| originalLogger: l.originalLogger.With(dataToFields([]lager.Data{data})...), | ||
| } | ||
| } | ||
|
|
||
| func dataToFields(data []lager.Data) []zap.Field { | ||
| fields := []zap.Field{} | ||
| for _, datum := range data { | ||
| for key, value := range datum { | ||
| fields = append(fields, zap.String(key, fmt.Sprintf("%v", value))) | ||
| } | ||
| } | ||
| return fields | ||
| } | ||
|
|
||
| func appendError(err error, fields []zap.Field) []zap.Field { | ||
| return append(fields, zap.Error(err)) | ||
| } | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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.
import changed.