5

I am getting this error after upgrading Rails from 3.1.2 to 4.0. When launching my server with rails s I got stuck with the following error

C:/ruby-2.0.0/lib/ruby/gems/2.0.0/gems/railties-4.0.0/lib/rails/commands/server.rb:78:in `start': undefined method `formatter' for #<Log4r::Logger:0x26dd908> (NoMethodError)

I have been on the Log4r site but haven't got any infor;ation about a bug when upgrading Rails.

Does anyone have any idea where this bug comes from. Thank you!

Pol0nium
  • 1,346
  • 4
  • 15
  • 31
  • I started working on a rails applications written recently using rails 4.1.7. I tried to switch to log4r and got the same error. – hshib Dec 07 '15 at 20:18
  • Found issue reported on Rails, and closed blaming on log4r not implement standard logger API: https://github.com/rails/rails/issues/14227 – hshib Dec 07 '15 at 20:27

1 Answers1

6

The method formatter is not defined on Log4r::Logger, but on Log4r::FileOutputter. Therefore I am surprised that is worked before the Rails update. Perhaps that changed between different versions of Log4r.

Please try the following (with adjusted filenames and patters):

require 'log4r'
outputter = Log4r::FileOutputter.new('log4r', filename: 'foobar.log')
outputter.formatter = Log4r::PatternFormatter.new(
  date_pattern: "%FT%T.000Z", pattern: "%d [%l] %m"
)

logger = Log4r::Logger.new('log4r')
logger.outputters = [outputter]

Add this code to config/application.rb or to a new file like config/initializers/logger.rb

spickermann
  • 100,941
  • 9
  • 101
  • 131
  • Sorry for barging in on an old question, but I'm somewhat surprised this should work: Rails wants to access `Rails.logger.formatter` and your code doesn't add the formatter property to the logger, only an outputter. – Marcus Ilgner Jun 15 '14 at 12:37
  • 1
    @ma_il: From the the error description it seems like the implementation has changed between version: In older versions, Rails called `formatter` on the `logger` itself. I newer version it calls `formatter` on `logger.outputer`, perhaps through an delegater in the implementation of `Log4r`. I do not know the root cause of the problem. I saw the question and looked for a likely explanation (the is no `formatter` on `logger`, but on `outputer`) and wrote some code to test that hypotheses. Seems that is was right. If this solution does not fix your problem, I suggest to ask an other question. – spickermann Jun 15 '14 at 21:19
  • Ah, that explains it. Thanks a lot! – Marcus Ilgner Jun 23 '14 at 13:53
  • where to write the above settings – vishB Jul 03 '14 at 09:00
  • @vishB in `config/application.rb` or a file like `config/initializers/logger.rb`. – spickermann Jul 03 '14 at 11:22