Is there any way to disable Erubis from printing "** Erubis 2.6.5" when starting the Rails environment?
I have several frequent Cron jobs that are run via Rake and the output of those jobs are e-mailed (via a MAILTO). Due to the fact that these tasks load the Rails environment (which includes Erubis) they always prints out "** Erubis 2.6.5" on startup. This means that an e-mail is always generated since Cron receives output. Is there any way to configure Erubis to cease printing this startup message to the console?开发者_如何学运维
Using the answers here plus the one linked to by @michael-andrews, I made the following change to our Rails 2.3.14 project that requires no change in our gems' source. Open up config/boot.rb
and find the Rails::Boot
class. You're augmenting the load_gems
method:
class Rails::Boot
def run
load_initializer
Rails::Initializer.class_eval do
def load_gems
buffer = ""
previous_stdout, $stdout = $stdout, StringIO.new(buffer)
@bundler_loaded ||= Bundler.require :default, Rails.env
ensure
$stdout = previous_stdout
output = buffer.gsub(/^\*\* Erubis (\d+\.?)+\s*/, '')
puts output unless output.strip.empty?
end
end
Rails::Initializer.run(:set_load_path)
end
end
The way this works is that we redirect $stdout while loading gems, pulling the stream into a local buffer. We then inspect the buffer after everything is complete, strip out Erubis's callout, and display anything else that might have happened (don't want to miss anything we're not expecting!).
Update: The below solution is from several years ago and applies when Rail 2 was new and plugins were still common. Now that using the gem is better and standard solution, the below answer is no longer applicable to newer apps; instead, the solution that @TALlama posted works. I'm leaving this answer here though, because it's a working solution in case your app is old and still uses the plugin.
You can modify the rails_xss plugin to remove this message. The offending part of the plugin is at "/plugins/rails_xss/lib/rails_xss/erubis.rb". At the very top of the file is a require:
require 'erubis/helpers/rails_helper'
Modify this require to simply redirect standard output to a dummy IO before the require, and restore standard output when you are done, like this:
stdout_original, $stdout = $stdout, StringIO.new
require 'erubis/helpers/rails_helper'
$stdout = stdout_original
It's ugly, but it solves the problem in a relatively non-intrusive way. I had a similar issue as the OP, needing to pipe the output of a "script/run" process to another process, and erubis was rudely breaking the convention about rails components/plugins being silent on the stdout front (exactly for this reason). The above solution is what I came up with and it works for me.
You need to redefine rails_helper.rb in erubis - these are offending lines:
## finish
ActionController::Base.new.logger.info "** Erubis #{::Erubis::VERSION}"
$stdout.puts "** Erubis #{::Erubis::VERSION}" if rails22
I suggest copying content of that file into new one, remove logging lines and requre that better_rails_helper instead of erubis providef one. . `
Support these pull requests and you get it without any monkey-patches
https://github.com/rails/rails_xss/pull/14 (rails_xss official plugin)
https://github.com/joloudov/rails_xss/pull/1 (for rails_xss gem)
精彩评论