📖 Scenario: You are building a simple Rails application that tracks user actions. To keep your app healthy and debug issues quickly, you need to set up logging and monitoring.
🎯 Goal: Learn how to create a basic log entry, configure log level, and display logs in the Rails console.
📋 What You'll Learn
Create a log entry with a specific message
Set the log level to :info
Write a method that logs user actions
Print the log output to the console
💡 Why This Matters
🌍 Real World
Logging helps developers track what happens in their applications. Monitoring logs is like checking a car's dashboard to see if everything is working well.
💼 Career
Understanding logging and monitoring is essential for DevOps roles to maintain application health and troubleshoot issues quickly.
Progress0 / 4 steps
1
Create a logger instance
Create a variable called logger and assign it a new Logger instance that writes to STDOUT.
Ruby on Rails
Hint
Use Logger.new(STDOUT) to create a logger that outputs to the console.
2
Set the log level to info
Set the logger variable's log level to Logger::INFO.
Ruby on Rails
Hint
Use logger.level = Logger::INFO to set the log level.
3
Write a method to log user actions
Define a method called log_user_action that takes a user and an action string, and uses logger.info to log the message: "User: #{user} performed: #{action}".
Ruby on Rails
Hint
Define a method with two parameters and call logger.info inside it.
4
Log a sample user action and display output
Call the log_user_action method with "Alice" as the user and "login" as the action. This should print the log message to the console.
Ruby on Rails
Hint
Call the method with the exact arguments to see the log output.
Practice
(1/5)
1. What is the main purpose of logging in a Rails application?
easy
A. To record events and errors for later review
B. To monitor real-time server performance
C. To deploy the application automatically
D. To manage database migrations
Solution
Step 1: Understand logging purpose
Logging captures events and errors that happen in the app so developers can check them later.
Step 2: Differentiate from monitoring
Monitoring is about watching app health live, not storing past events.
Final Answer:
To record events and errors for later review -> Option A
Quick Check:
Logging = record events/errors [OK]
Hint: Logging saves past events; monitoring watches live [OK]
Common Mistakes:
Confusing logging with monitoring
Thinking logging deploys apps
Mixing logging with database tasks
2. Which of the following is the correct way to write a log message in Rails?
easy
A. logger.write("User signed in")
B. Rails.log('info', 'User signed in')
C. log.info('User signed in')
D. Rails.logger.info "User signed in"
Solution
Step 1: Recall Rails logging syntax
Rails uses Rails.logger with methods like info, error, debug to write logs.
Step 2: Check each option
Only Rails.logger.info "User signed in" uses correct Rails.logger.info syntax.
Final Answer:
Rails.logger.info "User signed in" -> Option D
Quick Check:
Rails.logger.info is correct syntax [OK]
Hint: Use Rails.logger.method("message") for logging [OK]
Common Mistakes:
Using undefined logger object
Wrong method names like write or log
Incorrect method call syntax
3. Given this Rails code snippet, what will be the log level of the message?
Rails.logger.warn "Disk space low"
medium
A. Info
B. Warn
C. Error
D. Debug
Solution
Step 1: Identify the logging method used
The method called is warn, which sets the log level to warning.
Step 2: Match method to log level
Rails.logger.warn writes a warning level message.
Final Answer:
Warn -> Option B
Quick Check:
Rails.logger.warn = Warn level [OK]
Hint: Method name matches log level (warn = Warn) [OK]
Common Mistakes:
Confusing warn with info or error
Assuming default log level is info
Mixing debug and warn levels
4. You added this line in a Rails controller but no log appears:
logger.error "Payment failed"
What is the most likely reason?
medium
A. The log level is set higher than error
B. Rails.logger must be used instead of logger
C. The logger object is not defined in controllers
D. Logging is disabled by default in Rails
Solution
Step 1: Understand logger availability in controllers
In Rails controllers, logger is available as a shortcut to Rails.logger.
Step 2: Check log level settings
If the log level is set higher than error (like fatal), error messages won't show.
Final Answer:
The log level is set higher than error -> Option A
Quick Check:
Log level filters out lower severity logs [OK]
Hint: Check log level filters if logs don't appear [OK]
Common Mistakes:
Assuming logger is undefined in controllers
Thinking Rails.logger must be used always
Believing logging is off by default
5. You want to monitor your Rails app's performance in production. Which approach is best?
hard
A. Use Rails built-in logger to track all performance metrics
B. Add custom puts statements in code to watch performance
C. Integrate an external monitoring tool like New Relic or Datadog
D. Rely on Rails console logs only
Solution
Step 1: Understand Rails logging limits
Rails logging records events but does not provide detailed real-time performance monitoring.
Step 2: Identify best monitoring practice
External tools like New Relic or Datadog specialize in monitoring app health and performance live.
Final Answer:
Integrate an external monitoring tool like New Relic or Datadog -> Option C
Quick Check:
Use external tools for monitoring, not just logs [OK]
Hint: Use specialized tools for monitoring, not just logs [OK]
Common Mistakes:
Expecting Rails logger to monitor performance live
Using puts for monitoring which is not scalable
Ignoring external monitoring solutions
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.