Add new logging features for databases
Add new logging features for databases

Previously, Sequel::Database had only one logging function, log_info,
which logged the SQL to be executed at level info before it was sent
to the database.

However, users have been asking for more logging flexibility, in
particular:

* The ability to only log queries that raise errors.

* The ability to log how long queries take.

This requires changing the logging API, as instead of a simple
method call before execution, the logging method needs to take
a block.  This unfortunately means changes to every single adapter.
The changes are minor in some cases, but signficant in others.

This commit adds the underlying support to Sequel::Database.  Future
patches will be committed to add support for each adapter.

The underlying support takes the form of the new log_yield method,
which takes the same arguments as log_info, but requires a block
be passed, which is yielded to.  It wraps the yield in a
begin/rescue/block.  The rescue part makes sure that if the query
raises an error, it is logged at error level.  This means that if
you only want to log errors, you just need to have your logger ignore
things lower than error level.

The ensure block handles logging in the non-error case, and has one
feature that hasn't been requested but that I think is worthwhile.
It does log the duration of the query, but it also gives the user
the ability to specify a duration (in seconds) above which queries
are logged at warn level:

  # The default - Log all successful queries at info level
  DB.log_warn_duration = nil
  # Log all successful queries at warn level
  DB.log_warn_duration = 0
  # Log successful queries that take the database more than half a
  # second at warn level, other successful queries at info level
  DB.log_warn_duration = 0.5

Usage of log_yield will change the order of events to logging after
query execution.  Which means if you issue a query that crashes the
interpreter (bad extension coding), or deadlocks, it will not be
logged.  This is unfortunate, but if you do need such capability,
it's fairly easy to override log_yield to log the query at debug
level before yielding.

gferguson-gd/sequel,pfarrell/sequel,davidbegin/sequel,nju520/sequel,seeARMS/sequel,dennis-d/custom_sequel,gferguson-gd/sequel,offshore-safety/sequel,uhoh-itsmaciek/sequel,knut2/sequel,offshore-safety/sequel,reubano/sequel,chef/sequel,knut2/sequel,bigfix/sequel,t-morgan/sequel,nicohvi/sequel,offshore-safety/sequel,Marketcircle/sequel,dennis-d/custom_sequel,mpalmer/sequel,nju520/sequel,tmm1/sequel,t-morgan/sequel,beni55/sequel,davidbegin/sequel,uhoh-itsmaciek/sequel,yazshel/sequel,freshpay/sequel,seeARMS/sequel,beni55/sequel,freshpay/sequel,nicohvi/sequel,reubano/sequel,yazshel/sequel,pfarrell/sequel,mpalmer/sequel