Module: PgLogger
- Defined in:
- lib/pg_logger.rb
Constant Summary collapse
- POSTGRES_ERROR_MAPPING =
Maps PG notice levels to Ruby Logger levels
{ 'DEBUG' => :debug, 'LOG' => :info, 'INFO' => :info, 'NOTICE' => :warn, 'WARNING' => :warn, 'EXCEPTION' => :error, }
- DEFAULT_FORMAT =
'[pg] %s'.freeze
Class Method Summary collapse
-
.notice_proc_for_logger(logger, format_str = DEFAULT_FORMAT) ⇒ Proc
Creates a Proc from the given Logger object.
Class Method Details
.notice_proc_for_logger(logger, format_str = DEFAULT_FORMAT) ⇒ Proc
Creates a Proc from the given Logger object. The proc will accept PG::Result objects from notices and forward their contents into the given logger
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/pg_logger.rb', line 21 def self.notice_proc_for_logger(logger, format_str = DEFAULT_FORMAT) ->(*pg_results) { pg_results.each do |pg_result| # message is a PG::Result, we need to extract the errors from it # Only use the PG constants _within_ the proc so that the gem can still be loaded # before the "pg" gem. severity = pg_result.error_field(PG::Result::PG_DIAG_SEVERITY) = [ PG::Result::PG_DIAG_MESSAGE_PRIMARY, PG::Result::PG_DIAG_MESSAGE_DETAIL, PG::Result::PG_DIAG_MESSAGE_HINT, ] = .map do |err_const| pg_result.error_field(err_const) end.join(' ') logger_method_name = POSTGRES_ERROR_MAPPING.fetch(severity) logger.public_send(logger_method_name) { format_str % } end } end |