lulalala / multi_logger

Create multiple loggers in Rails
MIT License
59 stars 22 forks source link

Just curious: Why are you monckey patching the class instead of the logger instance? #14

Closed AugustoPedraza closed 8 years ago

AugustoPedraza commented 8 years ago

Inspired on you code, I implemented my own version of the multilogger:

def add_logger(name, logger)
  name = name.to_s
  raise "'#{name}' is reserved in #{Rails.logger.class} and can not be used as a log accessor name." if Rails.logger.respond_to?(name)

  Rails.logger.define_singleton_method(name.to_sym) do
    logger
  end
end

add_logger(:yodlee, Log4r::Logger['yodlee'])

And how I'm using it: Rails.logger.yodlee.debug("Querying index!!!")

Thanks in advance!

lulalala commented 8 years ago

Hehe I am not even aware that define_singleton_method exists. What are the benefits of define_singleton_method over monkey patching?

AugustoPedraza commented 8 years ago

The mayor benefit is that I don't need know the class of the logger.

But I'm not sure if that could have some collateral damage...

lulalala commented 8 years ago

Actually I think you got a point. Would you like to do a PR?