简体   繁体   English

过滤终端结果以获取“ puts”

[英]Filter terminal results to just get the “puts”

I have some debuge print line in my ruby code. 我的ruby代码中有一些debuge打印行。 Consider it as v=10 puts v . 将其视为v=10 puts v

I just run my code using this command rails server -p $PORT -b $IP 我只是使用此命令rails server -p $PORT -b $IP运行我的代码

In the terminal there are lots of unused information like 在终端中,有很多未使用的信息,例如

=> Booting WEBrick
=> Rails 4.2.1 application starting in development on http://0.0.0.0:8080
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
[2015-10-18 08:40:17] INFO  WEBrick 1.3.1
[2015-10-18 08:40:17] INFO  ruby 2.2.2 (2015-04-13) [x86_64-linux]
[2015-10-18 08:40:17] INFO  WEBrick::HTTPServer#start: pid=8290 port=8080

.
.
.
10
.
.
.

But I just want to get the 10 . 但是我只想拿10

So how can i filter the results? 那么如何过滤结果?

Since this is a rails application, you should consider using logger instead of puts . 由于这是一个Rails应用程序,因此您应该考虑使用logger而不是puts This will output the logs to the console as well as store them in a file/db depending on how rails is configured. 这会将日志输出到控制台,并将它们存储在文件/ db中,具体取决于配置rails的方式。 By default the logs will be stored in a file in the log/ directory so you can search for them later. 默认情况下,日志将存储在log/目录中的文件中,因此您以后可以搜索它们。 Logs from your local development environment will be stored in log/development.log 来自本地开发环境的log/development.log将存储在log/development.log

Logs can have different levels :debug , :info , :warn , :error , :fatal , and :unknown . 日志可以具有不同的级别:debug:info:warn:error:fatal:unknown In your case, it sounds like you're debugging so you can log with the line 就您而言,这听起来像是在调试,因此您可以使用以下行进行记录

logger.debug "Value is: #{value}"

If you want to tag the logs, though, so maybe all logs from the same controller are easy to find, you can use log tags. 但是,如果要标记日志,那么也许很容易找到来自同一控制器的所有日志,则可以使用日志标记。 In this case you might use the line 在这种情况下,您可以使用

logger.tagged("NameOfController") { logger.debug value }

This will output [NameOfController] value 这将输出[NameOfController] value

Then, as itmammoth said, you can grep the output on the tag that you used to find all the logs for a given controller or search through the log file. 然后,就像itmammoth所说的那样,您可以在标记上复制输出,该标记用于查找给定控制器的所有日志或搜索日志文件。

rails server -p $PORT -b $IP | grep "[NameOfController]"

You can find more information on using logger and other rails debugging in the official rails guides here and information about tagging here . 您可以在此处的官方Rails指南中找到有关使用logger和其他Rails调试的更多信息,并在此处找到有关标记的信息

Another option would be to use a debugger rather than logging. 另一种选择是使用调试器而不是日志记录。

You can filter the output with grep. 您可以使用grep过滤输出。

Please try this. 请尝试这个。

rails server -p $PORT -b $IP | grep "Hello World"

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

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