In rails I want to log some information in a different log file and not the standard development.log or production.log. I want to do this logging from a model class.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can create a Logger object yourself from inside any model. Just pass the file name to the constructor and use the object like the usual Rails
logger:Here I used a class attribute to memoize the logger. This way it won’t be created for every single User object that gets created, but you aren’t required to do that. Remember also that you can inject the
my_loggermethod directly into theActiveRecord::Baseclass (or into some superclass of your own if you don’t like to monkey patch too much) to share the code between your app’s models.