How do I set Ruby Sequel logging to the DEBUG level? - ruby

The default Ruby Sequel behaviour is to log all DB queries at the INFO level (unlike ActiveRecord which logs at the DEBUG level). How do I change this?

Previously, it was fairly simple to do with a proxy logger object, but enough people have asked for this that I implemented it. With the git master branch of Sequel, you can now do:
DB.sql_log_level = :debug
Which will use the debug method instead of the info method when logging queries.

Related

How to enable query logger in rom-sql (rom-rb)?

In rom-sql I want to enable logging, so that I can see all the sql queries that are produced.
How can I achieve that?
Because it uses sequel underneath, I guest it may be somehow possible through sequel logger.
Got it: ROM::SQL::Gateway#use_logger

How to reduce logs from database cleaner in ruby application

Here is my ruby spec_helper for rspec:
As you see I'm using database cleaner because I'm writing tests using the DB.
However, I get all this nonsense in my console output:
Is there a way to supress some of this output? Again remember, I'm not in RAils so I can't simply do:
config.logger.level = Logger::ERROR

How do I configure Sinatra to use structure.sql instead of schema.rb?

I've got a Sinatra app that I'm setting up with a database using ActiveRecord.
Due to one of the quirks of this database (namely a string primary key), I want to use a SQL schema (structure.sql) instead of a Ruby one (schema.rb). I don't mind that this restricts me to using a specific database flavour, we use Postgres for everything anyway.
To achieve this in Rails, I would put config.active_record.schema_format = :sql in config/application.rb. How do I do the same thing in Sinatra?
It's easy to configure your database by hand with Sinatra. We like to build our tables in MySQL instead of using ActiveRecord Migrations.
You'll have to create your database models by hand instead of using generators and you'll add this line to manage your connection:
ActiveRecord::Base.establish_connection(database_settings)
This is super easy. We typically read in the settings from a YAML file. It gets complicated when you want to write automated tests. Here's a blog I wrote about how to set up automated tests with Sinatra, MiniTest, and ActiveRecord.
Since you are still using active record, you can just add next line to your config (I put it under config/initializers/active_record.rb).
ActiveRecord::Base.schema_format = :sql

Setting log levels with Ruby Logger

I'm using a Ruby gem that utilizes the Logger class.
Is there a way to set log levels globally like in log4j with an .xml or .properties file?
I don't want the output to be written at all. My concern is that printing all those logging statements drains performance.
P. S.: It's just a simple script, I'm not using Ruby on Rails.
Because the gem you're using is kind enough to expose the logger as a attr_accessor, you can simply do this:
some_server = Server.new
some_server.logger.level = Logger::ERROR
Obviously feel free to chose the log level that is appropriate for your environment and application (http://www.ruby-doc.org/stdlib-1.9.3/libdoc/logger/rdoc/Logger.html).
Hope this helps.

Output SQL from an ActiveRecord migration without executing it (not rails!)

There's a bunch of questions out there similar to this one that talk about rails plugins as a solution - but I'm not using rails, read on for more
I have a Rakefile in a sinatra project which allows me to rake db:migrate. It'll do my migration perfectly, but I'd like to pass that a flag (or write a new rake task) which does the same thing, but outputs the SQL to STDOUT and doesn't commit the changes to the database. Does anyone know how to do this?
My first thought was to try ActiveRecord logging and see if I could get the SQL out at all, but that doesn't work! Any ideas?
namespace :db do
task :migrate_sql do
require 'logger'
ActiveRecord::Base.logger = Logger.new(STDOUT)
Rake::Task['db:migrate'].invoke
# This does the migration and doesn't output SQL - so no good!
end
end
I think there isn't any easy way to do it, for the following reasons:
up, down, and change are methods which execute other methods; there isn't a global migration query string that gets built and executed
neither the statements methods (add_column, etc) expose their statements as strings; as I understand, they are implemented as connection adapter methods, and for example the mysql adapter has a add_column_sql method, while the postgresql adapter does not, and its sql is a variable inside its add_column method
So, if you really need this functionality, I think your best option is to copy the sql from the log.

Resources