title: Using rails logger in rake tasks, rails 5
body: <p>I'm building a ruby on rails app, using rails 5, and I have a number of rake tasks from which I'd like to log information. However, when I attempt to access the logger, I get an error: undefined local variable or method `logger'</p>
  
  <p>I have my rake tasks in lib/tasks, and I've declared a logger in both production.rb and developement.rb</p>
  
  <p>Here's an example task:</p>
  
  <pre><code>namespace :some_namespace do
      desc "a description"
      task :scrape_info do
          include Scraper
          startTime = Time.now
          Rails.logger.tagged("Scraper") { Rails.logger.info "Beginning scrape: #{startTime} " }
          Scraper.scrape_info
          endTime = Time.now
          Rails.logger.tagged("Scraper") { Rails.logger.info "Finished scrape: #{endTime} " }
      end
  end
  </code></pre>
  
  <p>And an excerpt from development.rb where I have the logger defined:</p>
  
  <pre><code> config.logger = Logger.new(STDOUT)
   config.log_level = :info
   config.logger = ActiveSupport::TaggedLogging.new(logger)
  </code></pre>
  
  <p>Any idea how to incorporate the logger into rake tasks? I understand that I could simply use a puts statement to print to stdout, but eventually I'd like to have daily log files, and I would like my rake tasks to be included in those files</p>
  
  <p><strong>ANSWER:</strong>
  Simply add the environment tag given in the answer from @Joel_Blum. Also, in development.rb, the line </p>
  
  <pre><code>config.logger = ActiveSupport::TaggedLogging.new(logger)
  </code></pre>
  
  <p>needs to be:</p>
  
  <pre><code>config.logger = ActiveSupport::TaggedLogging.new(config.logger)
  </code></pre>