6

我一直在研究这个话题太久了,所以我必须发布这个。我有一些应用程序运行此设置,其中一个完全在 rails 启动(rails s)时出现故障。它们的配置几乎完全相同,但我似乎无法在这里大海捞针。有没有人对如何找到这个问题有任何指示?

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

当我尝试运行 rails 时:

=> 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
4

3 回答 3

10

我遇到了同样的问题,所以我创建了一个初始化器来使用一个空的格式化程序方法来扩展 log4r。只需在初始化程序中创建一个名为 log_formatting.rb 的文件并将以下内容粘贴到其中:

class Log4r::Logger
  def formatter()
  end
end

这对我有用。我希望它有所帮助。

于 2013-11-22T21:01:30.770 回答
2

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

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

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

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

[1] RAILS 4.0 中 Log4r 的未定义方法格式化程序

于 2013-10-25T02:57:21.547 回答
0

environment.rb如果您不小心评论/删除了以下行,另一种可能是查看您的文件。

require_relative 'application'
Rails.application.initialize!
于 2018-01-24T00:44:24.697 回答