Developing a custom error logger in Ruby
In this guide, let's wind up error handling in Ruby. In the previous guides, we first looked at the basic error handling syntax and followed that up with a better way to manage errors effectively. In this guide, we will see how we can handle errors in a production application.
We are going to create a method called error_logger
, which will be responsible for appending all the errors that occur in our program into a log file. Let's create a file called error_log.txt
. It's an empty file to which we will be adding more data.
This is what our error_logger
method will look like:
def error_logger (e) File.open('error-handling-lessons/error_log.txt', 'a') do |file| file.puts e end end
In this code, we are opening the error_log.txt
file in the append mode and printing the error message into it. This method takes error as its parameter and puts it into the file.
Next, let's begin our main program code:
begin puts nil + 10 rescue StandardError =...