Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I log the entire trace back of a Ruby exception using the default Rails logger?

I am working on rails project and I am trying to get exceptions to be logged to the rails log files. I know I can call logger.error $! to get the first line of the exception logged to the file. But, I want to get the entire trace stack logged as well. How do I log the entire trace back of an exception using the default rails logger?

like image 595
Josh Moore Avatar asked Oct 23 '08 03:10

Josh Moore


People also ask

What is backtrace in Ruby?

This blog is part of our Ruby 2.5 series. Stack trace or backtrace is a sequential representation of the stack of method calls in a program which gets printed when an exception is raised. It is often used to find out the exact location in a program from where the exception was raised.

Where can I see rails logs?

In a Rails app, logs are stored under the /log folder. In development mode, the development. log file is used & you see log output on the terminal you're running rails server on.

How do I create a log file in Ruby?

Since the logger library comes with Ruby, there's no need to install any gems or other libraries. To begin using the logger library, simply require 'logger' and create a new Logger object. Any messages written to the Logger object will be written to the log file.


1 Answers

logger.error $!.backtrace 

Also, don't forget you can

rescue ErrorType => error_name 

to give your error a variable name other than the default $!.

like image 164
Ian Terrell Avatar answered Sep 23 '22 00:09

Ian Terrell