3

What is the best way to add a logger for all the ActiveResource in rails?

In my application i have some methods which call the API through ActiveResource which sometimes gives TimeOut error.

So, i just want to log the url, method(get/post) and some url related stuff in logger for which it gives the TimeOut or any other Error.

Paulo Fidalgo
  • 21,709
  • 7
  • 99
  • 115
Salil
  • 46,566
  • 21
  • 122
  • 156

3 Answers3

0

You can define a new logger for ActiveResource by adding this to your 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 the errors you can use it only for Logger::Error:

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

To have a custom exception handling I will recommend to copy the source file of ActiveResource Exception handling to config/initializers/ and change the file according to your needs. This way you are controlling the behaviour of your exceptions and since there are different type of exceptions you'll be able to customize all of them.

Paulo Fidalgo
  • 21,709
  • 7
  • 99
  • 115
  • Yes, but i want one generalize method using which i can log something into it when any error occurs. – Salil Dec 02 '14 at 12:27
  • @Salil I've updated the answer with a way to log only Errors. – Paulo Fidalgo Dec 02 '14 at 13:54
  • Thanks man but i need to add some custom message also, and i don't think it is possible with the above code. So, please let me know how can add custom message too and i'll accept the answer. – Salil Dec 03 '14 at 04:46
0

How to add logger for ActiveResource in rails ?

Instantiate a new logger for ActiveResource

 ## config/environment.rb

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

I need to add some custom message

Override the exception handlers of ActiveResource with yours. Checkout the source

Monkey-patch the library functions like below

 ## create config/initializers/active_resource_patch.rb

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

I have modified the time out error to display with a custom message.

Community
  • 1
  • 1
Siva
  • 7,780
  • 6
  • 47
  • 54
  • Nice, but is there any way so that my page should not crash and only log get saved.Actually we are currently using begin-rescue and using log in rescue block – Salil Dec 03 '14 at 09:12
  • @Salil ActiveResource `raise` lot of theses custom exceptions. I believe you have to comment/patch all those codes to achieve it. Obviously, a tedious task because you cant mute `raise` – Siva Dec 03 '14 at 09:20
0

Not sure of which type of custom message you are trying to add. Maybe you can go for Logger Inheritance :

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
floum
  • 1,149
  • 6
  • 17