Question

I have been researching this topic for far too long now, so I have to post this. I have a few applications running this setup and one of them completely borks on rails startup (rails s). They are both configured nearly the exact same, but I can not seem to find the needle in the haystack here. Does anyone have any pointers on how to find this issue?

setup based on: http://blog.mmlac.com/log4r-for-rails/comment-page-1/#comment-1731

when I try to run rails s:

=> Booting WEBrick
=> Rails 4.0.0 application starting in development on http://0.0.0.0:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
Exiting
/Users/chrishough/Placewise/code/ApiDigest/.bundle/ruby/2.0.0/gems/railties-4.0.0/lib/rails/commands/server.rb:78:in `start': undefined method `formatter' for #<Log4r::Logger:0x007f85be89abe8> (NoMethodError)
    from /Users/chrishough/Placewise/code/ApiDigest/.bundle/ruby/2.0.0/gems/railties-4.0.0/lib/rails/commands.rb:78:in `block in <top (required)>'
    from /Users/chrishough/Placewise/code/ApiDigest/.bundle/ruby/2.0.0/gems/railties-4.0.0/lib/rails/commands.rb:73:in `tap'
    from /Users/chrishough/Placewise/code/ApiDigest/.bundle/ruby/2.0.0/gems/railties-4.0.0/lib/rails/commands.rb:73:in `<top (required)>'
    from bin/rails:4:in `require'
    from bin/rails:4:in `<main>'

how I have log4r configured in my application.rb file:

#log4r requirements
require 'log4r'
require 'log4r/yamlconfigurator'
require 'log4r/outputter/datefileoutputter'
include Log4r

module DigestApi
  class Application < Rails::Application
    # Settings in config/environments/* take precedence over those specified here.
    # Application configuration should go into files in config/initializers
    # -- all .rb files in that directory are automatically loaded.

   # -----------------------------------------------------------------------------------
    # assign log4r's logger as rails' logger.
    log4r_config= YAML.load_file(File.join(File.dirname(__FILE__),"log4r.yml"))
    log_cfg = YamlConfigurator
    log_cfg["ENV"] = Rails.env 
    log_cfg.decode_yaml(log4r_config['log4r_config'])

    # # disable standard Rails logging
    config.logger = Log4r::Logger['rails']
    ActiveRecord::Base.logger = Log4r::Logger['sqlserver']

    # #nice for multiple-instance webservers like unicorn
    # #to monitor (re-)starts
    # #log whenever a worker (re-)started
    Log4r::Logger['rails'].info "LAUNCH PUMA WORKER"
    # -----------------------------------------------------------------------------------

My log4r.yml file:

log4r_config:
  # define all loggers:
  loggers:
    - name          : rails
      level         : DEBUG
      trace         : 'true'
      outputters    :
      - console
      - rails_file

    - name          : sqlserver
      level         : DEBUG
      trace         : 'false'
      outputters    :
      - sqlserver_file

    - name          : sqlserver_long_query
      level         : DEBUG
      trace         : 'false'
      outputters    :
      - sqlserver_long_query_file

    - name          : missing_route
      level         : DEBUG
      trace         : 'false'
      outputters    :
      - missing_route_file

  # define all outputters (incl. formatters)
  outputters:
  - type: StdoutOutputter
    name: console
    formatter:
      date_pattern: '%H:%M:%S'
      pattern     : '%d %l: %m'
      type        : PatternFormatter

  - type: FileOutputter
    name: rails_file
    filename: "log/#{ENV}.log"
    trunc: false
    formatter:
      date_pattern: '%Y %m %d %H:%M:%S.%L %z'
      pattern     : '%d %l: %m'
      type        : PatternFormatter

  - type: FileOutputter
    name: sqlserver_file
    filename: "log/sql.log"
    trunc: false
    formatter:
      date_pattern: '%Y %m %d %H:%M:%S.%L %z'
      pattern     : '%d %l: %m'
      type        : PatternFormatter

  - type: FileOutputter
    name: sqlserver_long_query_file
    filename: "log/sql_qry_long.log"
    trunc: false
    formatter:
      date_pattern: '%Y %m %d %H:%M:%S.%L %z'
      pattern     : '%d %l: %m'
      type        : PatternFormatter

  - type: FileOutputter
    name: missing_route_file
    filename: "log/missing_route.log"
    trunc: false
    formatter:
      date_pattern: '%Y %m %d %H:%M:%S.%L %z'
      pattern     : '%d %l: %m'
      type        : PatternFormatter
Was it helpful?

Solution 2

Looking at this post [1] you might have to import the FileOutputter as well. Not really sure.

I think it's an import issue when the other server works fine. Just try to import everything you might possibly use from Log4r and make sure you don't call in [1] mentioned functions directly on the logger somewhere else.

If that does not work, try setting up a simple logger programmatically, then move that logger to the .yml and then expand it back to where it was before.

Hope this helps you, let me know if you need more help

[1] Undefined Method Formatter for Log4r in RAILS 4.0

OTHER TIPS

I was running into the same problem, so I created an initializer to extend log4r with an empty formatter method. Just create a file named log_formatting.rb in initializers and paste the following into it:

class Log4r::Logger
  def formatter()
  end
end

This worked for me. I hope it helps.

The other possible could be to see your environment.rb file if you have accidentally commented/removed the following lines.

require_relative 'application'
Rails.application.initialize!
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top