10

Rails Tip Snippet: Logging informational messages to your log

 3 years ago
source link: https://www.devroom.io/2007/03/22/rails-tip-snippet-logging-informational-messages-to-your-log/?utm_campaign=Feed%3A+ariejan+%28ariejan%7Cdevroom.io%29
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Rails Tip Snippet: Logging informational messages to your log

Posted: 2007-03-22 - Last updated: 2019-06-05

Tagged general rubyonrails features tipsnippets

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 [email protected].

My site is free of ads and trackers. I record privacy-respecting usage statistics with Fathom.

Was this post helpful to you? Why not ☕ Buy me a coffee

Copyright © 1999-2020 Ariejan de Vroom <[email protected]>

Live now; make now always the most precious time. Now will never come again.
    – Jean-Luc Picard


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK