【问题标题】:lines change places in logfile行更改日志文件中的位置
【发布时间】:2019-04-18 23:29:55
【问题描述】:

我使用 logstasher gem 将我的日志转换为 json 对象 然后我在每个日志行之前添加一行, 在开发中一切正常。 在生产中,有时生产线会更换位置。

应该是这样的

{"index":{"_id":"3cc6221b633bd2bd0a95865e9c0c3dfd"}}
{"method":"GET","path":"/public/so_list","format":"*/*","controller":"public","action":"so_list","status":200,"duration":3152.27,"view":246.58,"db":2882.97,"ip":null,"route":null,"request_id":null,"source":"unknown","tags":["request"],"@timestamp":"2018-11-15T18:53:36Z","@version":"1"}
{"index":{"_id":"4b94f72318d88a8d7c1c5e46e5465246"}}
{"method":"GET","path":"/public/ajx_group_items/DY2149","format":"*/*","controller":"public","action":"ajx_group_items","status":200,"duration":118.89,"view":31.89,"db":74.0,"ip":null,"route":null,"request_id":null,"source":"unknown","tags":["request"],"@timestamp":"2018-11-15T18:53:36Z","@version":"1"}
{"index":{"_id":"b1ceb09e59379be7e26bff6b0d91ccd9"}}
{"method":"GET","path":"/public/login","format":"html","controller":"public","action":"login","status":200,"duration":44.24,"view":41.55,"db":1.25,"ip":null,"route":null,"request_id":null,"source":"unknown","tags":["request"],"@timestamp":"2018-11-15T18:53:36Z","@version":"1"}

有时会发生这种情况

第 2 行应该与第 3 行更改位置

{"index":{"_id":"f6ee3d21d6e424652cdca28e9fdff611"}}
{"index":{"_id":"3c5050daede3f29d0402e888eef02046"}}
{"method":"GET","path":"/public/ajx_group_items/DY7100","format":"*/*","controller":"public","action":"ajx_group_items","status":200,"duration":341.08,"view":169.3,"db":157.56,"ip":null,"route":null,"request_id":null,"source":"unknown","tags":["request"],"@timestamp":"2018-11-15T18:53:37Z","@version":"1"}
{"method":"GET","path":"/public/ajx_group_items/DY7000","format":"*/*","controller":"public","action":"ajx_group_items","status":200,"duration":334.34,"view":191.42,"db":129.59,"ip":null,"route":null,"request_id":null,"source":"unknown","tags":["request"],"@timestamp":"2018-11-15T18:53:37Z","@version":"1"}
{"index":{"_id":"f6f59814f6cd45851d529a4efd1d5989"}}
{"method":"GET","path":"/public/ajx_group_items/DY7210","format":"*/*","controller":"public","action":"ajx_group_items","status":200,"duration":305.02,"view":221.0,"db":72.51,"ip":null,"route":null,"request_id":null,"source":"unknown","tags":["request"],"@timestamp":"2018-11-15T18:53:37Z","@version":"1"}

我的初始化器看起来像这样

if LogStasher.enabled?

  LogStasher::ActiveSupport::LogSubscriber.class_eval do

    alias :original_process_action :process_action

    def process_action(event)

      # this creates the first line before each log
      log_extra_line = Logger.new("#{Rails.root}/log/logstasher.log")
      hash = {index: {_id: event.payload[:request_id]}}
      log_extra_line.info(hash.to_json) 

      # this adds the log line
      original_process_action(event)
    end

  end

end

然后我将初始化程序更改为此 假设如果我将两个 arg 都传递给一个函数,然后执行它的工作日志,但我仍然有同样的问题。

if LogStasher.enabled?

  LogStasher::ActiveSupport::LogSubscriber.class_eval do

    alias :original_process_action :process_action

    def process_action(event)
      hash = {index: {_id: event.payload[:request_id]}}
      do_logs(hash, event)
    end

    def do_logs(elastic_hash, original_log)
      # this creates the first line before each log
      log_extra_line = Logger.new("#{Rails.root}/log/logstasher.log")
      log_extra_line.info(elastic_hash.to_json) 
      # this adds the log line
      original_process_action(original_log)
    end

  end

end

我做错了什么?

【问题讨论】:

    标签: ruby-on-rails ruby logging rubygems production


    【解决方案1】:

    这与您的服务器的并发性有关。在本地运行时,Rails 服务器默认运行单个线程。在生产中,它们通常是多线程的。日志行出现乱序,因为它们属于不同的请求。

    查看the rails docs on threading and concurrency 了解更多信息。

    【讨论】:

    • 嗨@Cara McCormack 我正在阅读链接,但我仍然不知道如何解决这个问题
    • @mat's - 这取决于您实际要解决的问题。在多线程系统中通常期望日志行不按顺序出现。如果您需要按顺序跟踪流中的各个请求,我还建议您记录请求 ID 并使用某种日志解析器。否则,您也可以在生产环境中运行单线程服务器,尽管显然会有性能损失。
    • 我真正想做的是在每个日志之前添加一个索引。然后将日志导入elasticsearch。有没有更好的方法来做到这一点?
    【解决方案2】:

    在初始化程序中,我在 elastic_hash 中添加了事件。 新哈希首先具有索引,然后是事件。 然后我做

    log = Logger.new("#{Rails.root}/log/logstasher.log")
    log.info(elastic_hash.to_json)
    

    我只为每个日志创建一行,包括索引和事件 它有效

    在这种情况下,我不需要 gem logsthasher,我相信它也会有不同的工作方式

    【讨论】:

      猜你喜欢
      • 2018-02-07
      • 2015-04-15
      • 2015-07-22
      • 2018-10-12
      • 1970-01-01
      • 2016-07-06
      • 2016-07-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多