-
Notifications
You must be signed in to change notification settings - Fork 55
Use the context logger, if available. #180
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
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 |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| package logger | ||
|
|
||
| import ( | ||
| "context" | ||
| "io" | ||
| "os" | ||
| "runtime" | ||
|
|
@@ -123,9 +124,24 @@ func Err(err error) *zerolog.Event { | |
| return Logger.Err(err) | ||
| } | ||
|
|
||
| // Ctx returns a DBSQLLogger from the provided context. If no logger is found, | ||
| // the default logger is returned. | ||
| func Ctx(ctx context.Context) *DBSQLLogger { | ||
|
||
| l := zerolog.Ctx(ctx) | ||
| if l == zerolog.DefaultContextLogger { | ||
| return Logger | ||
| } | ||
| return &DBSQLLogger{*l} | ||
| } | ||
|
|
||
| // AddContext sets connectionId, correlationId, and queryId as fields on the provided logger. | ||
| func AddContext(l *DBSQLLogger, connectionId string, correlationId string, queryId string) *DBSQLLogger { | ||
| return &DBSQLLogger{l.With().Str("connId", connectionId).Str("corrId", correlationId).Str("queryId", queryId).Logger()} | ||
| } | ||
|
|
||
| // WithContext sets connectionId, correlationId, and queryId to be used as fields. | ||
| func WithContext(connectionId string, correlationId string, queryId string) *DBSQLLogger { | ||
| return &DBSQLLogger{Logger.With().Str("connId", connectionId).Str("corrId", correlationId).Str("queryId", queryId).Logger()} | ||
| return AddContext(Logger, connectionId, correlationId, queryId) | ||
| } | ||
|
|
||
| // Track is a convenience function to track time spent | ||
|
|
||
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.
If the only thing we are using ctx for is to extract a logger. I would rather pass in a logger as the last argument to NewRows and ensure that dbsqllog.AddContext handles a nil first argument.