Rails Tip Snippet: Logging informational messages to your log
22 Mar, 2007
1 minute read

This “Rails Tip Snippet” is one in a series of small blocks of code that will make your life developing Rails applications a bit easier.

This first snippet shows you how you can log informational message to your log file so you can track any important actions that happened. As an example you may want to log all user accounts that get deleted:

def destroy
   @user = User.find(params[:id])
   @user.destroy!
   logger.info(“User account ‘#{@user.login}’ deleted by user #{current_user.login})
   …
end
Line 4 will put a string like “User account ‘johnlocke” delete by user ariejan” in your log file. Use that information any way you want.

You may also use the logger to inspect variables:

logger.info(“article inspection: #{@article.inspect}“)
Until next time! Any tips are welcome at tips@ariejan.net.

If you're seeing this message you've probably enabled some form of adblocker. I respect that.

I use this space to show recommended books on Amazon.com in relation to the post you just read. This generates a small income that allows me to buy a book every now and then.