-
-
Notifications
You must be signed in to change notification settings - Fork 55
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 logging for executing queries #134
Conversation
Arguments are translated to Log::Metadata::Value via DB::Statement#arg_to_log method. DB::Statement#before_query_or_exec & after_query_or_exec protected methods can be used to hook and run around the statement execution
This is great! But extending the way logging works by adding overloads to undocumented, private methods might be a bit confusing or obscure. How about introducing an "interface" (a module) that provides a way to turn an argument into a DB log argument, make the existing objects implement it, and let the implementation use Or some other way to make this a bit less hidden. |
I didn't want to pollute the Object with this. And is something only drivers will need. Having a public module to encapsulate that interface is good enough for me. Does that sounds good to you? |
So this is something where crystal-pg, in this case, would implement any overrides with |
@jwoertink overriding the method will do. |
With the last commit I applied the same pattern as in crystal-lang/crystal#9756 . class DB::Statement
def_around_query_or_exec do |args|
start = Time.monotonic
res = yield
elapsed = Time.monotonic - start
Log.debug &.emit("Finished", elapsed: "#{elapsed.total_seconds.humanize(precision: 2, significant: false)}s")
res
end
end |
Co-authored-by: Ary Borenszweig <asterite@gmail.com>
The class DB::MetadataValueConverter
def arg_to_log(arg : PG::Geo::Point)
Log::Metadata::Value.new("(#{arg.x}, #{arg.y})::point")
end
end And the |
See #134 (comment) for the final state of the PR
--
This PR adds a debug "Executing query" message to the
db
log source. The query and the arguments are passed as local metadataquery
and `args.The arguments are translated to
Log::Metadata::Value
viaDB::Statement#arg_to_log
method.For example in crystal-pg the following code would allow customizing how the
Geo::Point
argument is translated.By default,
arg.to_s
is used for all the values that are not directly supported byLog::Metadata::Value
.DB::Statement#before_query_or_exec
&after_query_or_exec
protected methods can be used to hook around the statement execution.The
DB::Statement
has now acommand : String
property that is specified during initialization. This the breaking-change part of this PR.Fixes #62
Closes #64