【问题标题】:Adding unique id in rails4 logger to identify the request and response在 rails4 记录器中添加唯一 id 以识别请求和响应
【发布时间】:2015-01-07 05:05:41
【问题描述】:

我在工作文件中添加了以下 Rails 记录器。

@@logger = Logger.new "log/rest_client.log"
  @@logger.level = Logger::DEBUG
end


begin
  @@logger.debug "The request #{url}"
  response = RestClient.get '#{url}'
  @@logger.debug "Successful response #{response}"
rescue => e
  @@logger.debug "Failure response #{e.message}"
end

我想用 logger(rest_client.log) 中的唯一 id 来识别请求和响应(成功或失败)。

现在日志是

D, [2014-12-09T14:27:18.576498 #29871] DEBUG -- : The request https://api.bitfinex.com/v1/symbols
D, [2014-12-09T14:27:21.547365 #29874] DEBUG -- :Successful response ["btcusd","ltcusd","ltcbtc","drkusd","drkbtc","th1btc"]

我想要一些对请求和响应都唯一的东西来识别它们属于同一个。

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-4 logging


    【解决方案1】:

    由于您在每一步都控制了日志消息的内容,因此您可以在每次发出请求时自己创建和使用唯一的 id。基于时间戳的 ID 将是唯一的。

    一种可能的解决方案:

    begin
      unique_id = "ID-" + Time.now.strftime("%Y%m%d-%H%M%S")
      @@logger.debug "#{unique_id}: The request #{url}"
      response = RestClient.get '#{url}'
      @@logger.debug "#{unique_id}: Successful response #{response}"
    rescue => e
      @@logger.debug "#{unique_id}: Failure response #{e.message}"
    end
    

    日志:

    D, [2014-12-09T14:27:18.576498 #29871] DEBUG -- : ID-20141209-142718: The request https://api.bitfinex.com/v1/symbols
    D, [2014-12-09T14:27:21.547365 #29874] DEBUG -- : ID-20141209-142718: Successful response ["btcusd","ltcusd","ltcbtc","drkusd","drkbtc","th1btc"]
    

    【讨论】:

      猜你喜欢
      • 2012-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多