I want to have separate logs for my application. I created the following module:
module MyApp
module MyLog
def self.included(base)
base.extend(ClassMethods)
end
module ClassMethods
def logger
@@logger ||= Logger.new("#{Rails.root}/log/#{self.name.underscore}.log")
end
end
end
end
Then in any of my models I can add:
include MyApp::MyLog
and use it as (the log file will appear in .../log/cat.log
):
Cat.logger.info 'test'
I tried using this method included
on Cat
and models Dog
, and I have this result:
Cat.new.logger
Dog.new.logger
If I first try to use my log for the model Dog
, I will have a log file named Dog
( /dog.log
).
How can I set a class variable @@logger
from a module for each class with the correct initialized registrar?
source
share