문제

I have a webserver that uses Sinatra and the Sequel gem. I would like to know if it is possible to print every query executed into the console.

I found in the Sequel documentation that I can setup a log file path.

You can also specify optional parameters, such as the connection pool size, or loggers for logging SQL queries:

DB = Sequel.connect("postgres://user:password@host:port/database_name",  
:max_connections => 10, :logger => Logger.new('log/db.log'))

However I was unable to find anything about printing the queries into the console rather than a log.

도움이 되었습니까?

해결책

You can, and you can log to multiple loggers too, see example below

db_location_test = "/db/reservation.accdb"
log_file_path = "#{__FILE__}_#{Time.now.strftime("%Y%m%d")}.txt"
log_file = File.open(log_file_path, "a")
$filelog = Logger.new log_file
$console = Logger.new STDOUT

$console.info "connecting to access database" #only logged to console
sConnectionStringAccess = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=#{db_location_test}"
#sql will be logged to both file and console
DBA = Sequel.ado(:conn_string=>sConnectionStringAccess, :loggers=>[$filelog,$console])

class Reservations < Sequel::Model(:TABLE_RESERVATIONS);end

Reservations.all.each do |record|
  $console.info Hash[record]
end
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top