Is there a way to disconnect Erubis from printing "** Erubis 2.6.5" when starting the Rails environment?

I have several frequent Cron jobs that run through Rake, and the output from these jobs is emailed (via MAILTO). Because these tasks load the Rails environment (including Erubis), they always print "** Erubis 2.6.5" at startup. This means that the email is always created as Cron receives the output. Is there a way to configure Erubis to stop printing this startup message to the console?

+4
source share
4 answers

Update. The solution is lower from a few years ago and applies when Rail 2 was new and plugins were still common. Now that the use of the gemstone is better and the standard solution, the answer below is no longer applicable to newer applications; instead, the solution that @TALlama posted is working. I leave this answer here because it is a working solution if your application is outdated and still uses the plugin.

You can modify the rails_xss plugin to remove this message. The offensive part of the plugin is in "/plugins/rails_xss/lib/rails_xss/erubis.rb". At the very top of the file you need:

require 'erubis/helpers/rails_helper' 

Modify this to simply redirect standard output to dummy IO before requesting and restore standard output when you are done:

 stdout_original, $stdout = $stdout, StringIO.new require 'erubis/helpers/rails_helper' $stdout = stdout_original 

This is ugly, but it solves the problem in a relatively unobtrusive way. I had a similar problem with the OP, which was supposed to handle the output of the "script / run" process to another process, and erubis flagrantly violated the agreement that rails components / plugins are silent on the stdout front (this is the reason for this). The above solution is what I came across and it works for me.

+2
source

Using the answers here, plus one related to @ michael-andrews, I made the following change to our Rails 2.3.14 project, which does not require changes to the source of our gems. Open config/boot.rb and find the Rails::Boot class. You increase 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 

How it works, we redirect $ stdout when loading gems, pulling the stream to a local buffer. Then we check the buffer after everything is ready, cross out the Herubis callouts and show everything that could happen (don't want to miss something we don't expect!).

+2
source

You need to override rails_helper.rb in erubis - these are offensive lines:

 ## finish ActionController::Base.new.logger.info "** Erubis #{::Erubis::VERSION}" $stdout.puts "** Erubis #{::Erubis::VERSION}" if rails22 

I suggest copying the contents of this file to a new one, deleting the log lines and requesting that better_rails_helper provides one instead of erubis. , `

+1
source

Support these stretch requests and you get them without any monkey patches

https://github.com/rails/rails_xss/pull/14 (official rails_xss plugin)

https://github.com/joloudov/rails_xss/pull/1 (for rails_xss gem)

+1
source

Source: https://habr.com/ru/post/1308785/


All Articles