I have a Rails 3 application, call it "MyApp". In my config\environments\production.rb file I see such things as
config.log_level = :info
config.logger = Logger.new(config.paths.log.first, 'daily')
config.logger = Logger.new(Rails.root.join("log",Rails.env + ".log"),3,20*1024*1024)
So, questions are focusing on terminology and wtf they mean... (or point me to some site ,I have looked but not found, to explain how this works.)
- MyApp is a module?
- MyApp::Application is a ...? What, a module too?
- MyApp::Application.configure is a method?
- config is a variable? How do I see it in console?
- config.logger is a ???
- config.paths.log.first is a ...??
--in console I can see "MyApp::Application.configure.config.paths.log.first" but don't know what that means or how to extract info from it!?!
Is this too much for one question? :)
I have looked at the tutorial http://guides.rubyonrails.org/configuring.html but it jumps right into what things do.
A six sided question! Oh my. Let's ahem roll.1 Here's hoping I receive 6 times the upvotes for it then? :)
1. MyApp is a module?
Yes, it's a module. It acts as a "container" for all things pertaining to your application. For instance you could define a class like this:
Then if someone else has a
MyFunClass, it won't interfere with your
MyFunClass. It's just a nice way of separating out the code.
2. MyApp::Application is a ...? What, a module too?
MyApp::Application is actually a class, which inherits from
Rails::Application. This does a quite a lot of things, including setting up the
Rails.application object which is actually an instance of
MyApp::Application that you can do all sorts of fun things on like making requests to your application (in a
rails console or
rails c session). This code for instance would make a dummy request to the root path of your application, returning a 3-sized Array which is just a plain Rack response:
You can also get the routes for your application by calling this:
The main purpose of defining
MyApp::Application is not these fun things that you'll probably never use, but rather so that you can define application-specific configuration inside
config/application.rb. Things like what parameters are filtered, the time zone of the application or what directories should be autoloaded. These are all covered in the Configuration Guide for Rails.
3. MyApp::Application.configure is a method?
Indeed it is a method, and it allows you to add further configuration options to your application's configuration after
config/application.rb has been loaded. You've probably seen this used in
config/environments/development.rb or one of the other two files in that directory, but basically they all use the same options as shown in that Configuration Guide linked to earlier.
4. config is a variable? How do I see it in console?
config "variable" is actually a method defined within the code used for
Rails::Application and returns quite simply a configuration object which stores the configuration for the application.
To access it in the console, just use
Rails.application.config. This will return quite a large
Rails::Application::Configuration object for your viewing pleasure.
5. config.logger is a ???
The method you're referring to, I assume, comes from this line in
# Use a different logger for distributed setups
# config.logger = SyslogLogger.new
The method in this example is not
config.logger, but rather
config.logger=, which is referred to as a "setter" method in Ruby-land. The one without the equal sign is referred to as a "getter". This method sets up an alternative logger for the production environment in Rails, which then can be accessed by using
Rails.logger within the console or the application itself.
This is useful if you want to output something to the logs, as you can simply call this code:
Rails.logger.info("DEBUG INFO GOES HERE")
6. config.paths.log.first is a ...?? --in console I can see "MyApp::Application.configure.config.paths.log.first" but don't know what that means or how to extract info from it!?!
Within a Rails application, you can modify the locations of certain directories. And so, this
config.paths method is a way of keeping track of where these directories map to. In my entire Rails life I have never had to use or modify this variable and that can mean either one of two things:
- It's not used often by Rails programmers, or;
- I don't live a very varied life.
Interpret it as you will. My main point is that you're probably never going to use it either.
I hope these help you understand Rails a little more!
1 Terrible dice joke.