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.
相关问题
- Question marks after images and js/css files in ra
- Using :remote => true with hover event
- Eager-loading association count with Arel (Rails 3
- I want to trace logs using a Macro multi parameter
- Is there a way to remove IDV Tags from an AIFF fil
相关文章
- how do I log requests and responses for debugging
- Right way to deploy Rails + Puma + Postgres app to
- AWS S3 in rails - how to set the s3_signature_vers
- how to call a active record named scope with a str
- How to add a JSON column in MySQL with Rails 5 Mig
- “No explicit conversion of Symbol into String” for
- form_for wrong number of arguments in rails 4
- Rspec controller error expecting <“index”> but
A decent option that works for me is to just add a fairly plain class to your
app/models
folder such asapp/models/my_log.rb
then in your controller, or really almost anywhere that you could reference a model's class from within your rails app, i.e. anywhere you could do
Post.create(:title => "Hello world", :contents => "Lorum ipsum");
or something similar you can log to your custom file like thisThe Logging framework, with its deceptively simple name, has the sophistication you crave!
Follow the very short instructions of logging-rails to get started filtering out noise, getting alerts, and choosing output in a fine-grained and high-level way.
Pat yourself on the back when you are done. Log-rolling, daily. Worth it for that alone.
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_logger
method directly into theActiveRecord::Base
class (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.I would suggest using Log4r gem for custom logging. Quoting description from its page: