I currently have some code that is being repeated over and over, and it looks rather terrible. What’s the best way to use Ruby’s metaprogramming to clean this up?
A recurring theme I have is something like this:
class Object
def some_logger
@some_logger ||= Logger.new("log/some.log")
end
def some2_logger
@some2_logger ||= Logger.new("log/some2.log")
end
end
In theory, I just want to be able to call an arbitrary logger throughout my Rails app and have those logs go to a separate, easily identifiable file. So I can at random will call:
some3_logger.info("Wooohooo!") in a controller or model without having to go back to my initialization code and create it there.
You could pass the logger name as a parameter:
Just include CustomLogger wherever you need and you can do: