Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

logger.debug vs puts in Rails debugging?

I used to do puts 'text' in order to have a look at the flow of my code. I decided to, instead of doing that, use the logger.debug 'text'.

However, I don't see any text outputed in the terminal where I have run rails s. What am I missing here?

like image 588
Hommer Smith Avatar asked Mar 13 '14 17:03

Hommer Smith


People also ask

What is rails logger debugging?

logger. debug , or in short logger. debug writes the parameter text to a log file. This log file is by default environment specific. If you are currently running rails in development environment then the log file in use is log/development.

What is the difference between logger info and logger debug?

INFO is used to log the information your program is working as expected. DEBUG is used to find the reason in case your program is not working as expected or an exception has occurred. it's in the interest of the developer.

What is logging debugging?

A debug log can record database operations, system processes, and errors that occur when executing a transaction or running unit tests. Debug logs can contain information about: Database changes. HTTP callouts.

Where does rails logger info go?

In a Rails app, logs are stored under the /log folder. In development mode, the development. log file is used & you see log output on the terminal you're running rails server on.


3 Answers

You're definitely headed the right direction in using logger.debug instead of puts as puts is more for command line applications.

Rails.logger.debug, or in short logger.debug writes the parameter text to a log file. This log file is by default environment specific. If you are currently running rails in development environment then the log file in use is log/development.log.

If you open the file log/development.log, you will find the debug text text in there. To see the log output as they are written to the file, you can use the tail command (if you are in unix system) as follows in a new terminal with present working directory as your rails application root:

$ tail -f log/development.log 
like image 157
vee Avatar answered Oct 20 '22 10:10

vee


The quick answer: The right way to debug your application is to print it to your logs (Try to avoid from scripts doing puts 'your message' ,instead, use Rails.logger.info "your message" as best practice.

Explanation: You have 4 types of loggers according to your environment config (production/staging/development/test), that are located in /logs. For example if you're debugging RAILS_ENV=test rails s -p 5000 you would find it in <yourApplicationPath>/log/test.log

The available log levels are: :debug, :info, :warn, :error, :fatal, and :unknown, corresponding to the log level numbers from 0 up to 5, respectively.

To write in the current log use the logger.(debug|info|warn|error|fatal|unknown) method from within a controller, model, or mailer:

Rails.logger.debug "Person attributes hash: #{@person.attributes.inspect}"
Rails.logger.info "Processing the request..."
Rails.logger.fatal "Terminating application, raised unrecoverable error!!!"

In this way you can grep and filter the right levels and insert some logic to your debugging process. Read rails documentation for more details:

like image 28
avivamg Avatar answered Oct 20 '22 09:10

avivamg


logger.debug 'text' would log the text in the environment logger file. If you are in development environment then you can see the logs in log/development.log.

To see the logs on console, For example: In development environment you can update the config/environments/development.rb file as below:

config.logger = Logger.new(STDOUT)
config.logger.level = Logger::DEBUG  ## set the appropriate log level like DEBUG, INFO, WARN, ERROR, FATAL

Or

You can directly read from the environment log file like Vee suggested

 tail -f log/development.log 
like image 45
Kirti Thorat Avatar answered Oct 20 '22 10:10

Kirti Thorat