简体   繁体   English

Rails管理调试代码

[英]Rails managing debugging code

I have a rails project that is sprinkled with various debugging code and customized logging to the console. 我有一个Rails项目,上面散布着各种调试代码和自定义的日志记录到控制台。

For example a lot of it take the form like this: 例如,很多格式如下:

CustomLogger.debug("TYPE", "My Message = " + some_variable.inspect)

Which in the backend I can either write to the log and watch a console by tailing a log files or else even package it up and display in special UI. 在后端中,我可以写日志并通过拖尾日志文件来查看控制台,也可以打包并将其显示在特殊的UI中。 But sprinkling this seems messy and I only really need in development context. 但是,撒些东西似乎很麻烦,我只在开发环境中才需要。 Is there a best practice for dynamically stripping out code like this when say releasing to production environment. 在发布到生产环境中时,是否有最佳实践来动态剥离此类代码。 I am sort of looking for something in the way of a preprocessor macro like in C. Something where this common boilerplate is in the source. 我正在某种形式类似于C的预处理器宏中寻找某种东西。 But is removed under certain environments. 但是在某些环境下会被删除。

Perhaps there are some useful meta programming techniques that would help? 也许有一些有用的元编程技术会有所帮助?

I really want to globably turn on and off but also not have to comment it out repeatedly. 我真的很想在全球范围内打开和关闭,但也不必重复评论。

Maybe I just doing it wrong but caveman debugging seems to make sense to me. 也许我只是做错了,但穴居人调试对我来说似乎很有意义。 The downside is that it seems like I spend a lot of time writing debugging code. 缺点是好像我花了很多时间编写调试代码。

In your config/environment/production.rb file, add something like 在您的config/environment/production.rb文件中,添加类似

config.log_level = :info

This will ignore :debug level log entries when in production. 在生产时,它将忽略:debug级日志条目。

Suggested Reading: http://guides.rubyonrails.org/debugging_rails_applications.html#log-levels 建议阅读: http : //guides.rubyonrails.org/debugging_rails_applications.html#log-levels

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM