What is the best/easiest way to configure logging for code kept in the lib directory?
相关问题
- 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
- How to specify memcache server to Rack::Session::M
相关文章
- how do I log requests and responses for debugging
- Ruby using wrong version of openssl
- Right way to deploy Rails + Puma + Postgres app to
- AWS S3 in rails - how to set the s3_signature_vers
- Difference between Thread#run and Thread#wakeup?
- 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
There's two ways to go about it:
Assuming your library is self-contained and has a module, you can add a
logger
attribute to your module and use that everywhere in your library code.You then either use an initializer in
config/initializers/
, or anconfig.after_initialize
block inconfig/environment.rb
to initialize your logger, like so:This would still allow you to use your self-contained library from scripts outside of Rails. Which is nice, on occasion.
If using your library without Rails really doesn't make sense at all, then you can also just use
Rails.logger
directly.In either case, you're dealing with a standard Ruby Logger. Also keep in mind that, in theory, the logger may be
nil
.We can use Rails logger directly into the lib, see the following snippet.