简体   繁体   中英

Why are there so many Rails loggers, and why are there locks on them?

I've been working on running a Daemon that has access to the rails environment. From searching around a bit, I've found there are two things I need handle when doing so:

  1. Re-initializing a connection to the Rails database
  2. Re-initializing the rails loggers

I understand the need to re-initialize a connection to the database, but not the loggers. I saw this code which will aims to make running a Daemon with Rails a bit easier. It creates new BufferedLogger instances for each of: Rails , ActiveRecord::Base , ActionController::Base , ActionMailer::Base

Two things are unclear to me:

  1. Why are there four different loggers? If I call logger.info from code within a controller vs code within a model, is it not using the same rails logger?
  2. Why exactly must the loggers be re-initialized in the Daemon? If they are not, I noticed that the logger refused to log, saying that there's a lock. But why?

One thing I've found is that there are quite a few people who have a hard time understanding the Rails loggers. If there are some blog posts that cover this topic already, please let me know!

Well I think 4 loggers were introduced in Rails 3. The idea behind it that each component (ActiveRecord, ActionPack, ActionMailer) are not really dependent on each other and it should be possible to use them standalone. Thus the reason why each of them have their own logger object. I think it's in railties gem where all those 4 loggers are basically set to same instance, so calling logger.info on any of them would mean same result. However in theory you could set ActiveRecord logger to something else for example.

As for why loggers need to be reinitialized I don't know...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM