Best way to add logger for ActiveResource

What's the best way to add a registrar for everyone ActiveResource in rails

?

In my application, I have some methods that call the API through ActiveResource

which sometimes gives a TimeOut error.

So, I just want to log the url

, method(get/post)

and some things related to the url in the magazine, for which he gives TimeOut or any other mistake.

+3


source to share


3 answers


You can define a new logger for the ActiveResource by adding it to yours config/environment.rb

:

 logger = Logger.new('log/active_resource.log', 'daily');ยท
 logger.level = Rails.env.dev? ? Logger::DEBUG : Logger::INFO;ยท
 ActiveResource::Base.logger = logger

      

If you just want to log errors, you can only use them for Logger::Error

:



 logger = Logger.new('log/active_resource.log', 'daily');ยท
 logger.level = Logger::Error
 ActiveResource::Base.logger = logger

      

To have custom exception handling, I recommend copying the original ActiveResource Exception handling file to config/initializers/

and modifying the file to suit your needs. This way you control the behavior of your exceptions, and since there are different types of exceptions, you can customize all of them.

0


source


How do I add a logger for an ActiveResource in rails?

Create a new journal for ActiveResource

 ## config/environment.rb

 logger = Logger.new('log/active_resource.log')
 logger.level = Logger::DEBUG 
 ActiveResource::Base.logger = logger

      

I need to add a custom message



Override exception handlers ActiveResource

with yours. Checkout source

Monkey-patch library functions as shown below

 ## create config/initializers/active_resource_patch.rb

 module ActiveResource
   class TimeoutError
     def to_s
      @message + " my custom message"
     end
   end
 end

      

I changed the timeout error to display with a custom message.

0


source


Not sure what type of custom post you want to add. Maybe you can go for Inheritance Logger:

class CustomLogger < Logger
  def self.error(message)
    super(message  + ' my custom message')
  end
end

#added code from Paulo Fidalgo
logger = CustomLogger.new('log_location', 'daily')
logger.level = Logger::ERROR
ActiveResource::Base.logger = logger

      

0


source







All Articles