0
0
Ruby on Railsframework~10 mins

Logging and monitoring in Ruby on Rails - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to log an info message in Rails.

Ruby on Rails
Rails.logger.[1]("User signed in successfully")
Drag options to blanks, or click blank then click option'
Adebug
Berror
Cwarn
Dinfo
Attempts:
3 left
💡 Hint
Common Mistakes
Using debug level for normal info messages
Using warn or error for non-problematic events
2fill in blank
medium

Complete the code to set the log level to show warnings and above.

Ruby on Rails
Rails.logger.level = Logger::[1]
Drag options to blanks, or click blank then click option'
ADEBUG
BWARN
CINFO
DERROR
Attempts:
3 left
💡 Hint
Common Mistakes
Setting level to DEBUG logs too much information
Setting level to INFO logs less than required
3fill in blank
hard

Fix the error in the code to log an error message with exception details.

Ruby on Rails
begin
  # some code
rescue => e
  Rails.logger.[1]("Error occurred: #{e.message}")
end
Drag options to blanks, or click blank then click option'
Adebug
Binfo
Cerror
Dwarn
Attempts:
3 left
💡 Hint
Common Mistakes
Using info or debug for error messages
Using warn which is less severe than error
4fill in blank
hard

Fill both blanks to create a custom logger that writes to a file and sets the log level to debug.

Ruby on Rails
custom_logger = Logger.new([1])
custom_logger.level = Logger::[2]
Drag options to blanks, or click blank then click option'
A"log/custom.log"
BSTDOUT
CDEBUG
DINFO
Attempts:
3 left
💡 Hint
Common Mistakes
Using STDOUT instead of a file path for file logging
Setting log level to INFO instead of DEBUG
5fill in blank
hard

Fill all three blanks to filter logs for requests with status code 500 or higher and log the path and status.

Ruby on Rails
ActiveSupport::Notifications.subscribe("process_action.action_controller") do |*args|
  name, start, finish, id, payload = args
  status = payload[[1]]
  path = payload[[2]]
  if status.to_i [3] 500
    Rails.logger.error("Error at #{path} with status #{status}")
  end
end
Drag options to blanks, or click blank then click option'
A:status
B:path
C>=
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using string keys instead of symbols for payload
Using equality (==) instead of greater-than-or-equal (>=) for status check