Sunday, November 28, 2010

Debugging Rails

You can always debug your application and get it back on the rails if ever it something goes wrong. You have to check the application log files. See to it that "tail –f" commands are running on the server.log and development.log. Rails will then show debugging and runtime information to these files and debugging information will also be displayed in the browser on requests from 127.0.0.1.

Using the Ruby logger class from inside your controllers, you can also log your own messages directly into the log file from your code like:
class ReportController < ActionController::Base

def destroy

@report = Report.find(params[:id])

@report.destroy

#Debug Log file

logger.info("#{Time.now} Destroyed Report ID ##{@report.id}!")

end

end

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

Description:


logger.debug

The DEBUG level designates fine-grained informational events that are most useful to debug an application.

logger.info

The INFO level designates informational messages that highlight the progress of the application at coarse-grained level.

logger.warn

The WARN level designates potentially harmful situations.

logger.error

The ERROR level designates error events that might still allow the application to continue running.

logger.fatal

The FATAL level designates very severe error events that would presumably lead the application to abort.

To write in the current log use the logger.(debug|info|warn|error|fatal) method from within a controller, model or mailer.
The default Rails log level is info in production mode and debug in development and test mode.

0 comments:

Post a Comment