有没有办法禁止Erubis打印“** Erubis 2.6.5”什么时候启动Rails环境?
我有几个通过 Rake 运行的频繁 Cron 作业,并且这些作业的输出通过电子邮件发送(通过 MAILTO)。由于这些任务加载 Rails 环境(包括 Erubis),因此它们在启动时总是打印出“** Erubis 2.6.5”。这意味着自 Cron 接收输出以来始终会生成电子邮件。有什么方法可以配置 Erubis 停止将此启动消息打印到控制台吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用此处的答案以及 @michael-andrews 链接的答案,我对 Rails 2.3.14 项目进行了以下更改,无需更改 gems 的源代码。打开 config/boot.rb 并找到 Rails::Boot 类。您正在增强
load_gems
方法:其工作方式是我们在加载 gem 时重定向 $stdout,将流拉入本地缓冲区。然后,我们在一切完成后检查缓冲区,去掉 Erubis 的标注,并显示可能发生的任何其他情况(不想错过任何我们没有预料到的事情!)。
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 theRails::Boot
class. You're augmenting theload_gems
method: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!).
更新:下面的解决方案来自几年前,适用于 Rail 2 还很新且插件仍然很常见的时候。既然使用 gem 是更好的标准解决方案,下面的答案不再适用于较新的应用程序;相反,@TALlama 发布的解决方案有效。不过,我将这个答案留在这里,因为如果您的应用程序很旧并且仍然使用该插件,这是一个可行的解决方案。
您可以修改rails_xss插件以删除此消息。该插件的违规部分位于“/plugins/rails_xss/lib/rails_xss/erubis.rb”。在文件的最顶部是一个require:
修改这个require以简单地将标准输出重定向到require之前的虚拟IO,并在完成后恢复标准输出,如下所示:
它很丑陋,但它相对解决了问题非侵入方式。我有一个与OP类似的问题,需要将“脚本/运行”进程的输出通过管道传输到另一个进程,而erubis粗鲁地打破了关于rails组件/插件在stdout前端保持沉默的约定(正是因为这个原因) 。上面的解决方案是我想出的,它对我有用。
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:
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:
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.
您需要在erubis中重新定义rails_helper.rb - 这些是有问题的行:
我建议将该文件的内容复制到新文件中,删除日志行并要求better_rails_helper而不是erubis提供的文件。
。
`
You need to redefine rails_helper.rb in erubis - these are offending lines:
I suggest copying content of that file into new one, remove logging lines and requre that better_rails_helper instead of erubis providef one.
.
`
支持这些拉取请求,您无需任何猴子补丁即可获得它
https://github.com/rails/ rails_xss/pull/14(rails_xss官方插件)
https://github.com/joloudov /rails_xss/pull/1 (对于rails_xss gem)
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)