【问题标题】:Use Log4r as logger in Sinatra App在 Sinatra App 中使用 Log4r 作为记录器
【发布时间】:2011-10-08 15:04:16
【问题描述】:

我想在 Sinatra 1.3.1 应用程序中使用带有日志级别信息、警告和错误的 log4r。输出应该去 requests.log(http 请求)、error.log 和 sinatra.log(其他输出)。

我该如何配置它?

我刚刚花了很多时间在谷歌上搜索相关文章,但没有找到任何东西。感谢您的帮助...

【问题讨论】:

    标签: ruby logging sinatra log4r


    【解决方案1】:

    编写一个替换env['rack.errors']env['rack.logger']的中间件。

    【讨论】:

    • 好的,谢谢。我承认出于懒惰而问我的问题。我已经阅读了它是如何完成的,我想我现在可以自己完成了。
    【解决方案2】:

    我假设您使用的是经典的 sinatra。 (只有 1 个名为 app.rb 的文件,您使用 $ ruby app.rb 运行它)

    1. app.rb

      require 'sinatra'
      
      # load the lib/logger_tool.rb
      Dir[File.join(__dir__, 'lib', '*.rb')].each { |file| require file }
      
      # define the logger as global variable.
      $logger = LoggerTool.get_logger log_file_postfix: ENV['market_id'], 
          name: Sinatra::Application.environment.to_s
      
      get '/' do
        $logger.info "-- in hihihi"
        'hihihi'
      end
      
    2. lib/logger_file.rb

      require 'log4r'
      require 'log4r/yamlconfigurator'
      require 'log4r/outputter/datefileoutputter'
      
      class LoggerTool
        include Log4r
      
        def self.get_logger options
      
          log4r_config= YAML.load_file(File.join(File.dirname(__FILE__), '..', "config", "log4r.yml"))
          temp = log4r_config['log4r_config']
      
          # change the log filename to xx.log (must end with .log)
          # this is optional
          temp["outputters"][0]['filename'] = "my_log_#{options[:log_file_postfix]}.log"
      
          # in test environment, let's rename it as "test.log"
          if options[:name] == 'test'
            temp["outputters"][0]['filename'] = "test"
          end
      
          YamlConfigurator.decode_yaml(temp)
      
          return Log4r::Logger[options[:name]]
        end
      end
      
    3. config/log4r.yml

      log4r_config:
        # define all loggers ...
        loggers:
          - name      : production
            level     : WARN
            trace     : 'false'
            outputters :
            - datefile
          - name      : development
            level     : DEBUG
            trace     : 'true'
            outputters :
            - datefile
          - name      : test
            level     : DEBUG
            trace     : 'true'
            outputters :
            - datefile
      
        outputters:
        - type: DateFileOutputter
          name: datefile
          dirname: "log"
          #filename: "my_app.log"   # here we comment it out. 
          formatter:
            date_pattern: '%H:%M:%S'
            pattern     : '%d %l: %m '
            type        : PatternFormatter
      
    4. 通过$ ruby app.rb 运行sinatra 并访问http://localhost:4567/,您会在log 文件夹中找到一个新创建的日志文件。

      log/my_log_xx_2019_03_27.log

    【讨论】:

      猜你喜欢
      • 2011-10-06
      • 1970-01-01
      • 2011-02-20
      • 2018-07-23
      • 1970-01-01
      • 2012-11-08
      • 1970-01-01
      • 2011-01-18
      • 1970-01-01
      相关资源
      最近更新 更多