简体   繁体   中英

Filter terminal results to just get the “puts”

I have some debuge print line in my ruby code. Consider it as v=10 puts v .

I just run my code using this command 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 .

So how can i filter the results?

Since this is a rails application, you should consider using logger instead of puts . This will output the logs to the console as well as store them in a file/db depending on how rails is configured. By default the logs will be stored in a file in the log/ directory so you can search for them later. Logs from your local development environment will be stored in log/development.log

Logs can have different levels :debug , :info , :warn , :error , :fatal , and :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

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.

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 .

Another option would be to use a debugger rather than logging.

You can filter the output with grep.

Please try this.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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