devroom.io/content/posts/2007-03-22-rails-tip-snippet-logging-informational-messages-to-your-log.md
2015-03-26 12:28:08 +01:00

24 lines
1.1 KiB
Markdown

+++
date = "2007-03-22"
title = "Rails Tip Snippet: Logging informational messages to your log"
tags = ["General", "RubyOnRails", "Features", "TipSnippets"]
slug = "rails-tip-snippet-logging-informational-messages-to-your-log"
+++
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:
<!--more-->
<pre lang="ruby">def destroy
@user = User.find(params[:id])
@user.destroy!
logger.info("User account '#{@user.login}' deleted by user #{current_user.login})
...
end</pre>
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:
<pre lang="ruby">logger.info("article inspection: #{@article.inspect}")</pre>
Until next time! Any tips are welcome at <a href="mailto:tips@ariejan.net">tips@ariejan.net</a>.