ruby – rails 4 log4r server.rb:78:在`start’:undefined方法`formatter’

前端之家收集整理的这篇文章主要介绍了ruby – rails 4 log4r server.rb:78:在`start’:undefined方法`formatter’前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我一直在研究这个话题已经太久了,所以我必须发布这个.我有几个应用程序运行此设置,其中一个完全borks在rails启动(rails s).它们的配置几乎完全相同,但我似乎无法在大海捞针中找到它.有没有人对如何找到这个问题有任何指示?

设置基于:http://blog.mmlac.com/log4r-for-rails/comment-page-1/#comment-1731

当我尝试运行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>'

我如何在application.rb文件中配置log4r:

#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"
    # -----------------------------------------------------------------------------------

我的log4r.yml文件

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

解决方法

看看这篇文章[1],您可能还需要导入FileOutputter.不太确定.

我认为当其他服务器工作正常时,这是一个导入问题.
只是尝试从Log4r导入您可能使用的所有内容,并确保不要在其他地方的记录器上直接调用[1]中提到的函数.

如果这不起作用,请尝试以编程方式设置一个简单的记录器,然后将该记录器移动到.yml,然后将其展开回原来的位置.

希望这对您有所帮助,如果您需要更多帮助,请告诉我

[1] Undefined Method Formatter for Log4r in RAILS 4.0

猜你在找的Ruby相关文章