Over the past few days, some users have learned about an error in a railroad crash log email. This problem can arise for several reasons. We’ll cover them below.
How to quickly send a one-line email to an administrative address in Rails?
I am using Rails Logger
to track nasty issues in my application. One of my controllers is doing important work and I need to be aware of errors as soon as possible. When an error occurs, I want my app to send me an email and tell me a log with more details. I am thinking of the following pseudocode:
start ...the rescue logger.error ("An error was detected! The parameter hash is" params.to_s) Email ("[email protected]", "Fatal error in the log. Examine the line" len (logger.error) .to_s)the end
The first obvious place I went to was ActionMailer. However, the documentation and my literature talks about creating a mail template, view and controller, which seems too complicated for what I want: a very short (one-line) email to email. -The address at which the formatting is performed and the address of the response does not matter. This brings me back to my question: how can I quickly send one line?Can’t get emails to the administrative email address in Rails?
Closed
. This question
based on the opinion of
. No response is accepted at this time.
Would you like to improve this question? Update the question so you can answer it with facts and quotes by editing this post.
Closed 4 years ago.
What Is Application Logging?
Before we start improving Rails Logger, let’s get back to the first principles. What are we trying to do? What is Application Logging? We had a good definition in the previous article in this series.
The application log records information about the behavior of your application during execution on more persistent media.
We covered this in Rails. We have the ability to save information about our application to a file and to standard output, which our DevOps can redirect to the desired location from the standard conclusion.
But we can do more.
Utility Method For Logging Exceptions
This example Utility.log_exception
method supports loggingException with sending email notification.
Example of calling Utility.log_exception
:
Definition of Utility.log_exception
:
- Avoid save / capture if there is nothing you can do other than.For example, in a template method, you can call it fromController, but you can also call it from scheduledEmployment. Therefore, it is difficult to say what the correct action should be. AIn a special case,
increment
is called with no arguments: sometimes it isIt makes sense to catch all exceptions, log the exception, and thenLift it up like it was never caught. - If you encounter an exception, consider throwing it awayException because the code could be handled at a different levelthe exception is better.
- Think about how the code will be called, for example B. from a callCreate an HTML request or Ajax or batch job. AllCases have very different requirements for error handling.
- Make sure you understand the order of your salvation clauses. TheseArticle Universe between
begin
andend
gives a good explanation. Mostly the most specificException types first, and then something likesave => e
. - Ruby does not support the concept of “reason” with one exception.So when you catch the exception and want to throw another one againException, then it is important to register the batch of the originalException, otherwise this information will be lost.
- Testing the development and production exception logFashion. You want all exceptions to be printed clearlyregardless of the Rails environment.
- A good way to test error handling is to temporarily increment
ArgumentError
(or any other error) and see what that exception is.processed by both the recorder and the user interface. - Worst-case scenario is to catch the exception and not logSoocommunion. This can make troubleshooting very difficult.
Next article, test failedProcessing ,shows you how to validate your error handling strategy.implemented correctly and how to add rspec unit and functional testsError processing.
GET "/" for 127.0.0.1 was fired on 03/10/2012 at 14:28:14 0100Index Processing HomeController # in HTML format Text template rendered in layouts / app (0.0ms) Rendered layouts / _assets.html.erb (2.0ms) Rendered layouts / _top.html.erb (2.6ms) Rendered layouts / _about.html.erb (0.3ms) Rendered layouts / _google_analytics.html.erb (0.4ms)200 OK completed in 79 ms (views: 78.8 ms | ActiveRecord: 0.0 ms)
method = GET path = / jobs / 833552.json format = json controller = jobs action = show status = 200 duration = 58.33 view = 40.43 dB = 15.26
def my_method_with_error foobar do_something_that_raises foobarSalvation => e Utility.log_exception e, info: "do_something_that_raises is called with # {foobar}"end
utility class def self.log_exception e, args extra_info = args [: information] Rails.logger.error extra_info if extra_info Rails.logger.error e.message st = e.backtrace.join (" n") Rails.logger.error st extra_info || = "" request = args [: request] env = request? request.env: null if env ExceptionNotifier :: Notifier.exception_notification (env, e,: data => {: message => "Exception: # {extra_info}"}). Deliver different ExceptionNotifier :: Notifier.background_exception_notification (e,: data => {: message => "Exception: # {extra_info}"}). Deliver the end the endEnd
Justin Gordon
CEO ShakaCode