Setting log levels with Ruby Logger - ruby

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.

Related

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

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.

Where should a gem store log files?

I am building a ruby gem that should output a logfile. Where is it a good practice to store log files?
I am extracting this functionality from a Rails website I am building, and there I could simply log in the log/ directory.
Ideally, make the path configurable (.rc file, switch, rails/rack config, whatever).
If it's a Rack middleware, add the possibility to specify it in the constructor's arguments.
If no log path is provided, fallback to detecting a log directory. (I vaguely remember it being config.paths['log'] in Rails, but be sure that config actually points to something before using that in your gem if it can be used outside of Rails.)
And if all else fails, log to nowhere...
Also, allow to disable logging if you enable it by default. Not everyone wants logs.

Set Ruby runtime configuration parameters?

I'm looking to pass runtime parameters to my Ruby code. I have a Java background and, in the majority of cases, if I want to override a configuration I'd use a system property with a sane default value.
For example, if I wrote a test against a REST API to localhost, then want to run it against an integration environment and want to adjust the base URL:
$ rake -Cbaseurl=https://i-env.company.com/tbse/ test
Is there an equivalent to system properties in Ruby?
Is there a standard pattern people are using?
I know how to use/load YAML configurations, as well as reference environment variables.
Ruby's from the UNIX world, where this is command line arguments and environment variables.
Command line arguments should be used for things that are commonly changed and are run dependent. These are things like a verbose flag, an output file, or a target URL. Access these with the Ruby stdlib OptionParser.
Environment variables are for things that change less frequently, and are usually across applications. Things like system executable path ($PATH). Use the built-in Ruby env object to access these.
For other things, like configuration, use a config file. In Ruby, these are usually in the YAML format. Use the Ruby stdlib yaml library for these.

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

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.

Resources